+ Start a Discussion
Alex Waddell 12Alex Waddell 12 

Apex Trigger firing every time

Hello everyone,

I am working on my first trigger and I was able to get the trigger to create a Case from a change in the account object but the only problem is, it is only supposed to create a Case if there isn't already a Case in the status of 'New' or 'On Service' in the Account

When i go to a test account, the trigger fires even when there is a Case that is New. I was wondering if anyone could look at my code and help me find out why this is happening
 
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','On Service')) 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;
    }
 
}

 
Best Answer chosen by Alex Waddell 12
Tad Aalgaard 3Tad Aalgaard 3
This is incorrect.  First, you're trying to see if there aren't any cases in which you wouldn't want to prefix it with "!".  But that isn't the real problem, cases never come back as null.  It will be an empty list.

change

if(myAccount.cases !=null){

to 

if(myAccount.cases.isEmpty()){
 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
This is incorrect.  First, you're trying to see if there aren't any cases in which you wouldn't want to prefix it with "!".  But that isn't the real problem, cases never come back as null.  It will be an empty list.

change

if(myAccount.cases !=null){

to 

if(myAccount.cases.isEmpty()){
 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Because on line 11 it shoule be == and not !=, Here is the corrected code
List<Account> allAccounts = new List<Account>([Select id,Fuzion_Status__c,(select id from cases where status in('New','On Service')) 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;
    }

 
Abdul KhatriAbdul Khatri
Was this helpful?
Alex Waddell 12Alex Waddell 12
Yes, they both were. Thank you!