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
DritterDritter 

else if statement not working

We have a map which compares any updates on an account to what was previously there, and then returns true or false. During testing, I'm able to get to return true through the first If statement (line 31), but I can't see to get it to return anything for the Else If statement (line 37). Any ideas?
 
public without sharing class accountServices{
   Map <Id, Account> oldAccs;
   Map <Id, Account> newAccs;
   
	public accountServices (Map <Id, Account> newTriggerAccs, Map <Id, Account> oldTriggerAccs) {
           oldAccs = oldTriggerAccs;
           newAccs = newTriggerAccs;
       	   system.debug ('Old accounts' + ' ' + oldtriggerAccs);
           system.debug ('New accounts' + ' ' + newTriggerAccs);
   }
   
	public static boolean isChanged (account newacc, account oldacc) {
           Account oldAccs = oldacc;
           Boolean isChanged = (oldAcc.program_of_interest__c == null && newacc.program_of_interest__c != null) ||
               (oldAcc.program_of_interest__c != null && newacc.program_of_interest__c == null) ||
               !(oldAcc.program_of_interest__c.equals(newacc.program_of_interest__c));
			if (!ischanged) {
               isChanged = (oldacc.expected_test_date__c == null && newacc.expected_test_date__c != null) ||
                   (oldacc.expected_test_date__c != null && newacc.expected_test_date__c == null) ||
                   !(oldacc.expected_test_date__c == newacc.expected_test_date__c);
               system.debug ('Result 3' + ' ' + isChanged);
           }
           else if (!ischanged) {
               isChanged = (oldacc.expected_prep_start_date__c == null && newacc.expected_prep_start_date__c != null) ||
                   (oldacc.expected_prep_start_date__c != null && newacc.expected_prep_start_date__c == null) ||
                   !(oldacc.expected_prep_start_date__c == newacc.expected_prep_start_date__c);
               system.debug ('Result 4' + ' ' + isChanged);
           }
           
       return isChanged;
   }

........transformation code

 
DritterDritter
Sorry, my lines were off. It's line 17 and 23. 
Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know if it works.
public without sharing class accountServices{
   Map <Id, Account> oldAccs;
   Map <Id, Account> newAccs;
   
	public accountServices (Map <Id, Account> newTriggerAccs, Map <Id, Account> oldTriggerAccs) {
           oldAccs = oldTriggerAccs;
           newAccs = newTriggerAccs;
       	   system.debug ('Old accounts' + ' ' + oldtriggerAccs);
           system.debug ('New accounts' + ' ' + newTriggerAccs);
   }
   
	public static boolean isChanged (account newacc, account oldacc) {
           Account oldAccs = oldacc;
           Boolean isChanged = (oldAcc.program_of_interest__c == null && newacc.program_of_interest__c != null) ||
               (oldAcc.program_of_interest__c != null && newacc.program_of_interest__c == null) ||
               !(oldAcc.program_of_interest__c.equals(newacc.program_of_interest__c));
			if (!ischanged) {
               isChanged = (oldacc.expected_test_date__c == null && newacc.expected_test_date__c != null) ||
                   (oldacc.expected_test_date__c != null && newacc.expected_test_date__c == null) ||
                   !(oldacc.expected_test_date__c == newacc.expected_test_date__c);
               system.debug ('Result 3' + ' ' + isChanged);
           }
           else if (ischanged) {
               isChanged = (oldacc.expected_prep_start_date__c == null && newacc.expected_prep_start_date__c != null) ||
                   (oldacc.expected_prep_start_date__c != null && newacc.expected_prep_start_date__c == null) ||
                   !(oldacc.expected_prep_start_date__c == newacc.expected_prep_start_date__c);
               system.debug ('Result 4' + ' ' + isChanged);
           }
           
       return isChanged;
   }

Best Regards,
-Vivek
DritterDritter
Thanks. I had to change the else if to just if and it worked.