1

I have a Lookup(Campaign) field Consumer_Newsletter__c set up on the Lead object. The Lookup(Campaign) field populates the Campaign Name on the Lead record.

I'm trying to put together Apex code that (1) updates the Consumer_Newsletter__c value; then (2) fires a managed package class et4ae5.triggerUtility.automate() on the Lead. The one consideration is I have Workflows running which update the Lead values when inserted/updated.

Can someone help me troubleshoot what I have so far?

trigger Trig_Lead on Lead (after insert, after update) {   
    static boolean hasCampaign = false;
    if(trigger.isUpdate&&trigger.isAfter){
      for(lead l : trigger.new){
        if(l.consumer_newsletter__c != null){
          hasCampaign = true;
        }
      }
    }
    if(hasCampaign==true){
      et4ae5.triggerUtility.automate('Lead');
    }
}
  • Hi Omar, welcome to the community. What exactly is the issue you're facing - you say "troubleshoot," so is there a compilation or runtime error you currently are trying to debug? I see right away that you're not actually performing any DML operations in your after trigger – Brian Mansfield Jul 16 '15 at 19:26
0

The code you posted appears to not meet either requirement you listed:

  • the Consumer_Newsletter__c value on Lead is never set
  • et4ae5.triggerUtility.automate() is never called on a Lead - you are passing the literal String 'Lead' to this method, and no actual reference.

Additionally, any changes this trigger made would not be "saved," as you never update the records - this is necessary in an after trigger.

I've updated your code to pass the correct Lead records to the package method (and removed an unnecessary variable, hasCampaign); I also added a DML statement so that the changes are pushed to the database. If you provide how the value of Consumer_Newsletter__c should be determined, I can help you add that logic in as well.

trigger Trig_Lead on Lead (after insert, after update) {   
    if (trigger.isUpdate && trigger.isAfter) {
      for (Lead l : trigger.new) {
        // we will set l.Consumer_Newsletter__c here
        if (l.Consumer_Newsletter__c != null) {
          et4ae5.triggerUtility.automate(l);
        }
      }
      update trigger.new;
    }
}
  • Thanks Brian. To explain, I have a FormAssembly web form writing signups to a custom object, then some workflow business rules writing them to the Lead/Campaign objs (ie Lead insert + add to Campaign). I was getting false positives when testing the Apex and running SOQL debugging (the consumer_newsletter__c value updates asynchronously, and not when the triggerUtility method was fired). I'm a newbie to Apex, so thanks for pointing out my errors ie String 'Lead'. One question - should the trigger be defined for a List or a Set of ids? As-is, the code would call for every Lead. Thanks! – Omarr Jul 16 '15 at 20:32
  • @Omarr - Brian's trigger is properly bulkified; Trigger.new is a list and will execute on all Leads in the batch. A more interesting question is whether managedPackage et4ae5.triggerUtility.automate() can handle a bulk update request with a list of leads-- because if it does SOQL/DML, then it will be doing SOQL/DML inside of a for loop which is bad – cropredy Jul 17 '15 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.