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
Mike Reynolds 6Mike Reynolds 6 

How can I add a condition to this trigger?

Hello, 

I need to make my trigger fire only when the case description is not null. is this the right way to approach this? I've really new to APEX and it's really throwing me for a loop. 

trigger descUpdate on Case (before update) {    
    case[] cases = Trigger.new;
    if(case.description !=null)
    caseDescUpdate.descUpdate(cases);
}

Thanks!

-Mike
Best Answer chosen by Mike Reynolds 6
George AdamsGeorge Adams
Then use:
trigger descUpdate on Case (before update) {    
    List<Case> casesToUpdate = new List<Case>();

    for (Case c : Trigger.new) {
        if(c.description != null && c.description.trim() != '') {
            casesToUpdate.add(c);
        }
    }

	casecaseDescUpdate.descUpdate(casesToUpdate);
}

I'm assuming the class performs an update operation somewhere after "//more lines like the one above"?

All Answers

George AdamsGeorge Adams
trigger descUpdate on Case (before update) {    
    List<Case> casesToUpdate = new List<Case>();

    for (Case c : Trigger.new) {
        if(c.description !=null){
            //perform your actions here
            casesToUpdate.add(c);
        }
    }

    update casesToUpdate;
}

 
MandyKoolMandyKool
Seems correct :)

You might want to check for blanks as well. You can do this by using below code.

if(case.description != null && case.description.trim() != ''){
      //You code
}
Mike Reynolds 6Mike Reynolds 6
Thanks George,
When you say "perform your actions here" i'm looking to call the public class caseDescUpdate. Can I still do that if I use your approach?
George AdamsGeorge Adams
Yes, you can. Just call it in that IF loop. Updated with MandyKool's improvement:
 
trigger descUpdate on Case (before update) {    
    for (Case c : Trigger.new) {
        if(c.description != null && c.description.trim() != '') {
            caseDescUpdate.descUpdate(c);
        }
    }
}

Not sure what you're trying to achieve, but to bulkify you may need to build a list within that IF loop, then pass the list to caseDescUpdate (which will need to be modified to accept a list).
 
Mike Reynolds 6Mike Reynolds 6
What I'm really trying to do is remove strings that are in an email signiture. This is what the class looks like:
 
public class caseDescUpdate {
    public static void descUpdate(case[] cases) {
        for (case b :cases){
            b.description = b.description.replace ('"The information in this electronic mail ("e-mail") message may contain information that is confidential and/or privileged, or may otherwise be protected by work product or other legal rules.', ''); 
           //more lines like the one above
        }
    }
}

 
George AdamsGeorge Adams
Then use:
trigger descUpdate on Case (before update) {    
    List<Case> casesToUpdate = new List<Case>();

    for (Case c : Trigger.new) {
        if(c.description != null && c.description.trim() != '') {
            casesToUpdate.add(c);
        }
    }

	casecaseDescUpdate.descUpdate(casesToUpdate);
}

I'm assuming the class performs an update operation somewhere after "//more lines like the one above"?
This was selected as the best answer
Mike Reynolds 6Mike Reynolds 6
Thank you both so much!