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
NeckNeck 

My if statement doesn't read by a compiler, please help

Good day. In my first tab the if statement is not running while in the second tab the if statement runs well.
 
FIRST TAB  
Trigger CaseUpdate on Case(Before Insert, Before Update, Before Delete, After Insert,
                             After Update, After Delete, After Undelete){
    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            Set<Id> newIds = new Set<Id>();
            Set<Id> oldIds = new Set<Id>();
            for(Case newLooper : Trigger.new){
                newIds.add(newLooper.AccountId);
            }
            for(Case oldLooper : Trigger.old){
                oldIds.add(oldLooper.AccountId);
            }
            List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);
            List<Case> oldList = new List<Case>([Select Id, Status from Case where AccountId=:oldIds]);
            List<Account> accList = new List<Account>([Select Id, Last_Updated_Cases__c from Account where Id=:newIds]);
            Case newCase = new Case();
            Case oldCase = new Case();
            Account accountObject = new Account();
            for(Case oldCaseLooper : oldList){
                oldCase = oldCaseLooper;
            }
                //System.debug('ID 1: ' + oldCase.Id);
            for(Account accountLooper : accList){
                accountObject = accountLooper;
            }
                //System.debug('ID 2: ' + accountObject.Id);
            for(Case newCaseLooper : newList){
                newCase = newCaseLooper;
                if(oldCase.Id == newCase.Id &&
                 oldCase.Status != newCase.Status){
                    accountObject.Last_Updated_Cases__c = newCase.CaseNumber;
                    update accountObject;
                }
            }
        }
    }                                
}
 
 
SECOND TAB
Trigger CaseUpdate on Case(Before Insert, Before Update, Before Delete, After Insert,
                             After Update, After Delete, After Undelete){
    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            Set<Id> caseId = new Set<Id>();    
       
        for(Case caseIdLooper : Trigger.new){
            caseId.add(caseIdLooper.AccountId);
        }      
     
        //Calling the Account Obj to get the Last updated cases field
        List<Account> accountObject = new List<Account>([Select Id, Last_Updated_Cases__c from Account
                                                       where Id=:caseId]);
       
        Case oldListOfcase = new Case();
        Case newListOfCase = new Case();
        Account accObject = new Account();
       
        for(Account accLooper : accountObject){
            accObject = accLooper;
        }
       
        for(Case oldCaseLooper : Trigger.old){
            oldListOfCase = oldCaseLooper;
        }
        System.debug('ID: ' + oldListOfCase.Id);
        for(Case newCaseLooper : Trigger.new){
            newListOfCase = newCaseLooper;
            if(oldListOfCase.Id == newListOfCase.Id &&
               oldListOfCase.Status != newListOfCase.Status){
               accObject.Last_Updated_Cases__c = newListOfCase.CaseNumber;
                   update accObject;
            }
        }
    }
  }                                
}
Best Answer chosen by Neck
ankit bansalankit bansal
Hi Nick,
For the code in your first tab I assume you just update the status on the case and save it, since account has not changed -
List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);
List<Case> oldList = new List<Case>([Select Id, Status from Case where AccountId=:oldIds]);
Both the newList and oldList will give back the same case with the same status and your if condition-
if(oldListOfCase.Id == newListOfCase.Id &&
               oldListOfCase.Status != newListOfCase.Status){
}

where you check if status is different will never be satisfied. Why this code -
List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);
List<Case> oldList = new List<Case>([Select Id, Status from Case where AccountId=:oldIds]);

is giving the same result is because in essence you are querying the same record from the database.

The code in second tab is working because you are using context variables which will have both the case with the new status valu and the case with the old status value.

 

All Answers

ankit bansalankit bansal
Hi Neck,
I am assuming you are talking about the if condition where you are updating the account.Last_Updated_Cases__c , as per me the first code will not work in the scenario when you update the case and clear the account field. What will happen is that the -
List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);

will return an empty list and since you are iterating over newList-
//System.debug('ID 2: ' + accountObject.Id);	
            for(Case newCaseLooper : newList){	
                newCase = newCaseLooper;	
                if(oldCase.Id == newCase.Id &&	
                 oldCase.Status != newCase.Status){	
                    accountObject.Last_Updated_Cases__c = newCase.CaseNumber;	
                    update accountObject;	
                }	
            }

and newList will be empty your if condition will never get executed.
NeckNeck

Hi Ankit,


Yeah you are right but, I used System.debug to ensure that my newList returns a value, and it works it gives me a value because of the AccountId of the child object. How come my newList gives no value to my if condition? or How come my newList returns no value though I used System,Debug to check if my newList is empty? Can you elaborate to me?


Thank you,
Neck

Abdul KhatriAbdul Khatri
Hi Neck,

I am little cofuse with your reference of FIRST and SECOND TAB and also see that the same trigger with the same name have different source code, how is that possible. Are you saying that you created two triggers with the same name and different code for two different TABS. Please pardon my understanding but I am really not following here to suggest you any solution.

From my perpective, it doesn't matter which TAB you are working off, it will only trigger one of those triggers. Therefore only one seems to be working.
ankit bansalankit bansal
Hi Nick,
For the code in your first tab I assume you just update the status on the case and save it, since account has not changed -
List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);
List<Case> oldList = new List<Case>([Select Id, Status from Case where AccountId=:oldIds]);
Both the newList and oldList will give back the same case with the same status and your if condition-
if(oldListOfCase.Id == newListOfCase.Id &&
               oldListOfCase.Status != newListOfCase.Status){
}

where you check if status is different will never be satisfied. Why this code -
List<Case> newList = new List<Case>([Select Id, CaseNumber, Status from Case where AccountId=:newIds]);
List<Case> oldList = new List<Case>([Select Id, Status from Case where AccountId=:oldIds]);

is giving the same result is because in essence you are querying the same record from the database.

The code in second tab is working because you are using context variables which will have both the case with the new status valu and the case with the old status value.

 
This was selected as the best answer
NeckNeck

Hi Abdul,

Yeah it's not about the tabs, I just created two tabs for the revision of code. My goal is to re-execute my second tab's code in another way which you will on first tab's code. So again my concern is, how come the if condition in my first tab's code is not working? 

 

NeckNeck

Hi Ankit,

 

Oh I see, I was assuming that newList and oldList will give me different value because of Trigger.new and Trigger.old. The conclusion is I must use the Trigger Context.

 

Thank you,
Neck