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
Qihao GuoQihao Guo 

How to create a case in apex class

Hi, It is my first time to use apex and I want to know if I want to use apex to create a case how should I do that?
Best Answer chosen by Qihao Guo
VinayVinay (Salesforce Developers) 
Hi Qihao,

Check below sample example.
 
trigger caseCheck on Account (After update) {
  List<Account> allAccounts = new List<Account>([Select id,Fuzion_Status__c,(select id from cases where status in('New','Open')) from account where id in :Trigger.new]);
   List<Case> newCases = new List<Case>();
    
    for(Account myAccount :allAccounts){
    Account oldAccount = trigger.oldMap.get(myAccount.id);
    if(oldAccount.Fuzion_Status__c == 'Initial Phone call' && myAccount.Fuzion_Status__c != 'Initial Phone call'){
        if(myAccount.cases !=null){
            Case c = new Case();
            c.Accountid = myAccount.Id;
            c.Type = 'ICM';
            c.Origin = 'SHIP';
            c.Division__c = 'Case Management';
            c.Status = 'New';
            c.RecordTypeId = '01236000000OJLq';
            newCases.add(c); 
        }
     }
        
    }
    if(!NewCases.isEmpty()){
        insert newCases;
    }
 
}

Please mark as Best Answer if above information was helpful.

Thanks,

All Answers

VinayVinay (Salesforce Developers) 
Hi Qihao,

Check below sample example.
 
trigger caseCheck on Account (After update) {
  List<Account> allAccounts = new List<Account>([Select id,Fuzion_Status__c,(select id from cases where status in('New','Open')) from account where id in :Trigger.new]);
   List<Case> newCases = new List<Case>();
    
    for(Account myAccount :allAccounts){
    Account oldAccount = trigger.oldMap.get(myAccount.id);
    if(oldAccount.Fuzion_Status__c == 'Initial Phone call' && myAccount.Fuzion_Status__c != 'Initial Phone call'){
        if(myAccount.cases !=null){
            Case c = new Case();
            c.Accountid = myAccount.Id;
            c.Type = 'ICM';
            c.Origin = 'SHIP';
            c.Division__c = 'Case Management';
            c.Status = 'New';
            c.RecordTypeId = '01236000000OJLq';
            newCases.add(c); 
        }
     }
        
    }
    if(!NewCases.isEmpty()){
        insert newCases;
    }
 
}

Please mark as Best Answer if above information was helpful.

Thanks,
This was selected as the best answer
Qihao GuoQihao Guo
What is Fuzion_Status__c, I don't have that variable in Account entity.
Qihao GuoQihao Guo
Hi there, Could you tell me where does fuzion status come from and how to set it up. As the attachment, I have create the object called fuzion status but it still says variable does not exit. Thanks
VinayVinay (Salesforce Developers) 
Above is just sample code and 'Fuzion_Status__c' will not be in your org, kindly make changes with fields existing in your org.

Thanks,