0

I have this trigger

trigger LeadTriggers on Lead (after insert) {
    if(Trigger.isAfter && Trigger.isInsert) LeadAfterInsertHandler.process(Trigger.new, Trigger.newMap);
}

But I want it to only trigger when the field a in leads is equals true. Is that possible? I can't added here if(Trigger.isAfter && Trigger.isInsert && lead.a == True)

Thanks!

2 Answers 2

2

You can't do that in the trigger itself. It may work if you only insert 1 record at a time but it won't work for bulk insert of records. You need to move that logic into your LeadAfterInsertHandler method and check that field for each of the records separately. Alternatively you can do the check within the trigger before you call your method and eliminate some records from processing:

trigger LeadTriggers on Lead (after insert) 
{
    if(Trigger.isAfter && Trigger.isInsert) 
    {
        Map <Id, Lead> leadsMap = new Map <Id, Lead> ();

        for (Lead leadRecord : trigger.new)
        {
            if (leadRecord.a == true)
            {
                leadsMap.put(leadRecord.Id, leadRecord);
            }
        }

        LeadAfterInsertHandler.process(leadsMap.values(), leadsMap);
    }
}

I haven't tested this but it should give you an idea of how to do it.

2

Why not just check the status of lead.a inside of the process method?

Ideally when using this kind of trigger framework you don't want this qualifying criteria to be in the trigger, so you should put it in process. Somewhere in there it'll be doing a loop (guessing at the parameter names here):

void process(List<Lead> newList, Map<Id, Lead> newMap)
{
  for(Lead l : newList)
  {
    if(!l.a)
    {
      // skip processing
      continue;
    }

    // processing
  }
}

This is just an example but should give you an idea of how to go about this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .