function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishal RamchandraVishal Ramchandra 

when the "Type" on Account is updated to Customer - Direct, it should create a case record with below details

when the "Type" on Account is updated to Customer - Direct, it should create a case record with below details
Hi Folks,
Please help me out below schenario and what is the best way to do this one

Thanks!!


Account "AB Corp" with "Type" = Prospect
It has say 4 Contacts under it with below details:
C1 --> Mailing Country = India
C2 --> Mailing Country = US
C3 --> Mailing Country = India
C4 --> Mailing Country = Aus

Now, when the "Type" on Account is updated to Customer - Direct, it should create a case record with below details:
Case Name = 'Contact case creation'
Case Account = 'AB Corp'
Case Description = 
India: C1, C3
US: C2
Aus: C4record with below details
Hi Folfs,
Please help me out below schenario and what is the best way to do this one

Thanks!!


Account "AB Corp" with "Type" = Prospect
It has say 4 Contacts under it with below details:
C1 --> Mailing Country = India
C2 --> Mailing Country = US
C3 --> Mailing Country = India
C4 --> Mailing Country = Aus

Now, when the "Type" on Account is updated to Customer - Direct, it should create a case record with below details:
Case Name = 'Contact case creation'
Case Account = 'AB Corp'
Case Description = 
India: C1, C3
US: C2
Aus: C4
varun kumar 212varun kumar 212
Hi Vishal,

Please Try the Below code. It worked for me.


trigger AccountTrigger on Account (After Update) {
    if(trigger.isAfter && trigger.isUpdate){
        Set<Id> accIds = new Set<Id>();
        for(Account acc : trigger.new){
            if(acc.Type != trigger.oldMap.get(acc.Id).Type && acc.Type == 'Customer - Direct')
                accIds.add(acc.Id);
        }
        if(accIds.size()>0){
            List<Case> updateCaseList = new List<Case>();
            List<Account> accList = [Select Id,Name,Type,(select Id,Name,MailingCountry from contacts) from Account where Id In: accIds];
            if(accList.size()>0){
                for(Account a : accList){
                    Map<String,String> conMap = new Map<String,String>();
                    String description;
                    if(a.contacts.size()>0){
                        for(contact con : a.contacts){
                            if(con.MailingCountry != null)
                            {
                                if(conMap.get(con.MailingCountry) != null){
                                    String contactNames = conMap.get(con.MailingCountry)+','+con.Name;
                                    conMap.put(con.MailingCountry,contactNames);
                                }else{
                                    conMap.put(con.MailingCountry,con.Name);
                                }
                            }
                        }
                        System.debug(conMap);
                        if(conMap != null)
                        {
                            for(String mailingCountry : conMap.keyset()){
                                if(description != null && description != ''){
                                    description =  description+'\n'+mailingCountry+':'+conMap.get(mailingCountry);
                                }else{
                                    description = mailingCountry+':'+conMap.get(mailingCountry);
                                }
                            }
                        }
                        case c = new case();
                        c.AccountId = a.Id;
                        if(description != '')
                            c.Description = description;
                        updateCaseList.add(c);
                    }
                }
            }
            if(updateCaseList.size()>0){
                try{
                    insert updateCaseList;
                }catch(Exception e){
                    System.debug(e.getMessage());
                }
            }
        }
    }
}
Vishal RamchandraVishal Ramchandra
Hi Varun,
Thanks a lt for your effort but case record has been not created. It's not work for me.
I have created account AB Corp and under 4 contact as well as. once update the Type Customer - Direct then case record not created .
Please check and let me know

Thanks!!!   


 
Kiran ChodavadiyaKiran Chodavadiya
Try This code. it helps to create new case on update Account type.
===============================================
public class CreateCasesOnUpdation {
    public static void CreateCases(List<Account> UpdatedAccount) {
        List<Case> NewCases = new List<Case>();
        
        for (Account acct :UpdatedAccount) {
            if (acct.Type == 'Customer-Direct') {
                Case VarCase = new Case();
                VarCase.FieldName = '';
                VarCase.FieldName = '';
                NewCases.add(Varcase); 
            }
        }
        insert NewCases;
    }
}

============================================================
============================================================

trigger CreatecasesTrigger on Account (After update) {
    CreateCasesOnUpdation.CreateCases(Trigger.New);
}
prashanth boeniprashanth boeni
@varun kumar 212's  code gonna works fine just add the required fields of case object status and origin

                        case c = new case();
                        c.AccountId = a.Id;
                        if(description != '')
                        c.Description = description;
                        c.Status = 'New';
                        c.Origin = 'Web';
                        updateCaseList.add(c);
 
Vishal RamchandraVishal Ramchandra
Hi Varun and Kiran,
sorry about that i was not updated earlier as per your response 
I have created class and trigger both as per your code but case record is not created as per condition . 


Now, when the "Type" on Account is updated to Customer - Direct, it should create a case record with below details:
Case Name = 'Contact case creation'
Case Account = 'AB Corp'
Case Description = 
India: C1, C3
US: C2
Aus: C4


 
Vishal RamchandraVishal Ramchandra
Hi Folks,
@Kiran Chodavadiya
code not woke for me. can you check

@varun kumar 212
i cfeated same trigger but it still not working

Can any one Please help me out above query.