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
Alex Waddell 12Alex Waddell 12 

Help Inserting Case from Apex Trigger

Hello everyone,

I am trying to write a trigger that checks to see if a case is open for an account. If there is no case open then i want it to open a case with certain criteria. I have gotten the code to not throw any errors right up into the end. I was wondering if anyone can help me polish off my last 2 lines of code so that the Case will be Inserted.
trigger caseCheck on Account (After update) {
    for(Account myAccount : Trigger.new){
    List<Case> openCase =[Select id
                          From Case
                          Where (Accountid = :myAccount.Id 
                                 AND
                                 Status IN('New','Open')) ];    
        System.debug(openCase.size() + ' Open case(s) found');
 
        if(openCase.isEmpty()){
            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';
            }  
        Case.add(c);
        Insert Case;       
    }
}

I am reveiving the following errors;
Variable does not exist: c
Variable does not exist: Case
 
Best Answer chosen by Alex Waddell 12
Wilfredo Morillo 20Wilfredo Morillo 20
You need to use the trigger.oldMap and compare the old value with the new:
trigger caseCheck on Account (After update) {
        //Get all account and cases.
  

  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;
    }
 
}

All Answers

Vinod Chokkula 3Vinod Chokkula 3
you are trying to access C, outside if { }, in which it is initialized and a list collection is required to insert all the new cases

Here is the updated trigger:

trigger caseCheck on Account (After update) {
    List<Case> casesToUpdate= new List<Case>(); 
    for(Account myAccount : Trigger.new){
    List<Case> openCase =[Select id
                          From Case
                          Where (Accountid = :myAccount.Id 
                                 AND
                                 Status IN('New','Open')) ];    
        System.debug(openCase.size() + ' Open case(s) found');
 
        if(openCase.isEmpty()){
            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';
            casesToUpdate.add(c);
            }  
        
        Insert casesToUpdate;       
    }
}
 
Wilfredo Morillo 20Wilfredo Morillo 20
Try this: 
*** Make sure there is a custom field named Division in cases.
trigger caseCheck on Account (After update) {
        //Get all account and cases.
  List<Account> allAccounts = new List<Account>([Select id,(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){
    
        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;
    }
 
}

Let me know if it worked for you.

wil, 
 
Alex Waddell 12Alex Waddell 12
Thanks guys, I appreciate both of your methods

Can i ask one last thing, what if i only wanted this trigger to fire when a field on the Account called "Fuzion_Status__c" is changed from "Initial Phone call" to anything else (It's a picklist field)? where would i add that in my code to make that work?
Wilfredo Morillo 20Wilfredo Morillo 20
You need to use the trigger.oldMap and compare the old value with the new:
trigger caseCheck on Account (After update) {
        //Get all account and cases.
  

  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;
    }
 
}
This was selected as the best answer
Alex Waddell 12Alex Waddell 12
Thank you that makes sense,

I have one last issue with this code, it seems like my SOQL querie isn't working properly because when i go to a test account that already has a case created that fits the criteria, it is still creating a new Case

Any ideas on why that might be?
Kalyani GadeKalyani Gade
ABC Firm needs to make the service desk more productive. They have an idea to create Case Solutions as a reference guide for customers and service agents if any case resolution is accepted by the customer.

Create a custom object ‘Case Solution’ with fields Case Description, Resolution.
- Create a custom field ‘Accept Solution’ as a checkbox on Case object.
- Create a custom field ‘Resolution’ as a text area on Case Object.
- Whenever a case has the status ‘Closed’ and its ‘Accept Solution’ checkbox is selected; a new Case Solution record should be created.

please share code for this problem statement .it helps alot..