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 SolodyankinAlex Solodyankin 

How can I change this code to trigger the latter IF statements if the first one is false? I also need it to not simply stop at the first one if it is true.

I had some help putting a version of the below code together and am not sure why the code doesn't fire the 2nd and 3rd If statement when the first statement is false (having a null value in either the in the HS Electives field), even if the 2nd and/or 3rd are true. The 2nd and 3rd only fire when the first statement is true. I considered using Else If statements, but that stops the code from progressing when the first statement is true.

The goal of this code is to populate all of the 3 multi-select picklist electives desired fields into a single field, regardless of which of the 3 fields have values in them.
 
trigger UpdateAllElectivesMSPV1 on Case (before update) {
    for(Case c : Trigger.new) {
        

        if(!String.isBlank(c.S1_High_School_Electives__c)){
            List<String> pickValuesHS = c.S1_High_School_Electives__c.split(';');
            String pickValuesHSStr ='' ;
            for(String HSstr : pickValuesHS){
                         pickValuesHSStr = pickValuesHSStr + '\n' + HSstr;
            }
c.Electives_Desired__c =  pickValuesHSStr.abbreviate(500);


       if(!String.isBlank(c.Middle_School_Elec__c)){
            List<String> pickValuesMS = c.Middle_School_Elec__c.split(';');
            String pickValuesMSStr ='' ;
            for(String MSstr : pickValuesMS){
                         pickValuesMSStr = pickValuesMSStr + '\n' + MSstr;
          }


              c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500);

 if(!String.isBlank(c.S1_Elementary_School_Elective_Selection__c)){
            List<String> pickValuesES = c.S1_Elementary_School_Elective_Selection__c.split(';');
            String pickValuesESStr ='' ;
            for(String ESstr : pickValuesES){
                         pickValuesESStr = ESstr;            
         }


              c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500) + '\n' + pickValuesESStr.abbreviate(500);
        }
}
}
}
}

 
Best Answer chosen by Alex Solodyankin
Maharajan CMaharajan C
Hi Alex,

Try the Below code :

trigger UpdateAllElectivesMSPV1 on Case (before update) {
    for(Case c : Trigger.new) {
        String pickValuesHSStr ='' ;
        String pickValuesMSStr ='' ;
        String pickValuesESStr ='' ;
        if(!String.isBlank(c.S1_High_School_Electives__c)){
            List<String> pickValuesHS = c.S1_High_School_Electives__c.split(';');
            //String pickValuesHSStr ='' ;
            for(String HSstr : pickValuesHS){
                         pickValuesHSStr = pickValuesHSStr + '\n' + HSstr;
            }
            //c.Electives_Desired__c =  pickValuesHSStr.abbreviate(500);
        }

       if(!String.isBlank(c.Middle_School_Elec__c)){
            List<String> pickValuesMS = c.Middle_School_Elec__c.split(';');
            //String pickValuesMSStr ='' ;
            for(String MSstr : pickValuesMS){
                         pickValuesMSStr = pickValuesMSStr + '\n' + MSstr;
          }
            //c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500);
       }


        if(!String.isBlank(c.S1_Elementary_School_Elective_Selection__c)){
            List<String> pickValuesES = c.S1_Elementary_School_Elective_Selection__c.split(';');
            //String pickValuesESStr ='' ;
            for(String ESstr : pickValuesES){
                         pickValuesESStr = ESstr;            
         }
            //c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500) + '\n' + pickValuesESStr.abbreviate(500);
        }
        
        c.Electives_Desired__c   = (pickValuesHSStr!=''?pickValuesHSStr.abbreviate(500)+'\n':'') + (pickValuesMSStr!=''?pickValuesMSStr.abbreviate(500)+'\n':'') + (pickValuesESStr!=''?pickValuesESStr.abbreviate(500):'');

}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Alex,

Try the Below code :

trigger UpdateAllElectivesMSPV1 on Case (before update) {
    for(Case c : Trigger.new) {
        String pickValuesHSStr ='' ;
        String pickValuesMSStr ='' ;
        String pickValuesESStr ='' ;
        if(!String.isBlank(c.S1_High_School_Electives__c)){
            List<String> pickValuesHS = c.S1_High_School_Electives__c.split(';');
            //String pickValuesHSStr ='' ;
            for(String HSstr : pickValuesHS){
                         pickValuesHSStr = pickValuesHSStr + '\n' + HSstr;
            }
            //c.Electives_Desired__c =  pickValuesHSStr.abbreviate(500);
        }

       if(!String.isBlank(c.Middle_School_Elec__c)){
            List<String> pickValuesMS = c.Middle_School_Elec__c.split(';');
            //String pickValuesMSStr ='' ;
            for(String MSstr : pickValuesMS){
                         pickValuesMSStr = pickValuesMSStr + '\n' + MSstr;
          }
            //c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500);
       }


        if(!String.isBlank(c.S1_Elementary_School_Elective_Selection__c)){
            List<String> pickValuesES = c.S1_Elementary_School_Elective_Selection__c.split(';');
            //String pickValuesESStr ='' ;
            for(String ESstr : pickValuesES){
                         pickValuesESStr = ESstr;            
         }
            //c.Electives_Desired__c = pickValuesHSStr.abbreviate(500) + '\n' + pickValuesMSStr.abbreviate(500) + '\n' + pickValuesESStr.abbreviate(500);
        }
        
        c.Electives_Desired__c   = (pickValuesHSStr!=''?pickValuesHSStr.abbreviate(500)+'\n':'') + (pickValuesMSStr!=''?pickValuesMSStr.abbreviate(500)+'\n':'') + (pickValuesESStr!=''?pickValuesESStr.abbreviate(500):'');

}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Alex SolodyankinAlex Solodyankin
Thank you so much Maharajan! 

That is very interesting how you were able to use !=''? multiple times within one field update.
Is that basically creating an If not blank then populate the following value?