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
Tanushree Singh 11Tanushree Singh 11 

AND and OR in one 'if' Statement

I have a trigger which is working absolutely fine. It tries to update an opportunity field (Stage) when the Resigned Date is changed and the Fund date is deleted. It is as below:

trigger OpportunityUpdResigned on Account ( after update) {
    set<Id> setAccountId = new set<Id>();
    for(Account a : Trigger.new){
        if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && a.Fund_Date__c == null)
        {
            setAccountId.add(a.Id);
        }
    }
    List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
    for(Opportunity objOpp: lstOpportunity)
    {
        if(objOpp.StageName == 'Closed Lost'){
                objOpp.StageName = 'Installed';
            objOpp.Closed_Lost_Reason__c = null;
        }
    }
    update lstOpportunity;
}

Now, there is  one more condition to be added. It is to check if the Assets Under Management field is also blank. So, when I am trying to change the if condition like this:

for(Account a : Trigger.new){
        if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && (a.Fund_Date__c == null ||a.Assets_Under_Management__c))
        {
            setAccountId.add(a.Id);
        }
    }

it's not helping to achieve the business result.
Andrew GAndrew G
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && (a.Fund_Date__c == null ||a.Assets_Under_Management__c==null))

you appear to have missed the test for null on the Assets under management field

Regards
Andrew
Tanushree Singh 11Tanushree Singh 11
Even after adding the test for null, it's not working
Andrew GAndrew G
ok, low on coffee , but it looks correct.
Only suggestion for the minute is to break the test into two or three and test each element individually to determine which is not allowing the whole to execute properly - running debug logs in the back end
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && a.Fund_Date__c == null)  {
  System.debug("**DEBUG - old code works');
  If( a.Assets_Under_Management__c==null) {
    System.debug("**DEBUG - assets is null');   
//...code continues
and then
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c )  {
  System.debug("**DEBUG -field change detected');
  If( a.Fund_Date__c == null || a.Assets_Under_Management__c==null) {
    System.debug("**DEBUG - Or statement works');   
//...code continues

because looking at it, it appears fine.  My only thought is that one of the individual tests (perhaps the new one) is not evaluating correctly.

Regards
Andrew
 
Tanushree Singh 11Tanushree Singh 11
Finally this worked:

trigger OpportunityUpdResigned on Account ( after update) {
    set<Id> setAccountId = new set<Id>();
    for(Account a : Trigger.new){
        if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c )
        {
            if (a.Fund_Date__c == null ||a.Assets_Under_Management__c ==null){
            setAccountId.add(a.Id);
            }
        }
    }
    List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
    for(Opportunity objOpp: lstOpportunity)
    {
        if(objOpp.StageName == 'Closed Lost'){
                objOpp.StageName = 'Installed';
            objOpp.Closed_Lost_Reason__c = null;
        }
    }
    update lstOpportunity;