0

I have created a trigger(after update) on Lead which will fire on status change and create a contact and custom object record.

Issue is that trigger is working but creating around 7 records of contact and custom object , i am not able to debug this , any help would be helpful.

Trigger:

Trigger CreateContact on Lead (after insert, after Update) {
    if(Trigger.isInsert) {
        Lead2TriggerHandler.createContact(Trigger.new);
    }
        if(Trigger.isUpdate) {
        Lead2TriggerHandler.createContact(Trigger.new);
    }
}

Class:

public class LeadTriggerHandler {
    public static void createContact(List<Lead> leads) {
        List<Application__c> app = new List<Application__c>();
        List<Contact> contact = new List<Contact>();
           for(Lead acc : leads){
            if(acc.Status=='Interviewed'){
            Contact con = new Contact(LastName = acc.lastname,
                                     FirstName= acc.FirstName,
                                     RecordTypeId='someid');
            contact.add(con);
        }
        insert contact;
        }
        for(Lead acc : leads){
            if(acc.Status=='Interviewed'){
            Application__c a = new Application__c(Name = acc.Lead_Program_Name__c,
                                                 Program__c= acc.Program__c,
                                                 Last_Name__c= acc.LastName,
                                                 First_Name__c= acc.FirstName,
                                                 Program_Session__c= acc.Program_Session__c);
            app.add(a);
        }
        insert app;
        }
    }
}
0

Firstly, I see some issues with your code:
1. Why do you loop the leads list twice? One loop is enough and you can do all tasks inside loop.
2. Your code will create records every time it is updated and status is currently Interviewed. I guess, you want to create Contact and Application only when lead is inserted or status is changed to Interviewed. For that you need to compare new and old value of the record.

Finally, to answer you question, it can happen that trigger runs multiple times, when e.g workflow rule updates the currently updated record. One option to solve this is to set up some static variable flag that will tell you that you are running in cycles. This static variable is valid through the whole transaction, so you need to make sure it does not intefere with your other code.

Here is code with my updates:

Trigger

Trigger CreateContact on Lead (after insert, after Update) {
    if(Trigger.isInsert) {
        Lead2TriggerHandler.createContact(Trigger.new, null);
    }
    if(Trigger.isUpdate) {
        Lead2TriggerHandler.createContact(Trigger.new, (Map<Id,Lead>) Trigger.oldMap);
    }
}

Class:

public class LeadTriggerHandler {
    public static Boolean firstRunComplete = false;

    public static void createContact(List<Lead> newLeads, Map<Id,Lead> oldLeadsMap) {
        if(LeadTriggerHandler.firstRunComplete) return;

        LeadTriggerHandler.firstRunComplete = true; 

        List<Application__c> apps = new List<Application__c>();
        List<Contact> contacts = new List<Contact>();
        for(Lead acc : leads){
            if(acc.Status=='Interviewed' && (oldLeadsMap == null || oldLeadsMap.get(acc.Id).Status != 'Interviewed')){
                Contact con = new Contact(LastName = acc.lastname,
                                         FirstName= acc.FirstName,
                                         RecordTypeId='someid');
                contacts.add(con);

                Application__c a = new Application__c(Name = acc.Lead_Program_Name__c,
                                                     Program__c= acc.Program__c,
                                                     Last_Name__c= acc.LastName,
                                                     First_Name__c= acc.FirstName,
                                                     Program_Session__c= acc.Program_Session__c);
                apps.add(a);
            }
        }
        insert contacts;
        insert apps;


    }
}
  • I tried your code , but getting an error on this line: – Harry_G Feb 14 at 9:56
  • if(acc.Status=='Interviewed' && (oldLeadsMap == null || oldLeadsMap.get(lead.Id).Status == 'Interviewed')) – Harry_G Feb 14 at 9:56
  • Method does not exist or incorrect signature: void get(Schema.SObjectField) from the type Map<Id,Lead> – Harry_G Feb 14 at 9:57
  • sorry there should've been .get(acc.Id) instead of .get(lead.Id) – Jakub Mozucha Feb 14 at 10:49
  • now it should be correctly edited – Jakub Mozucha Feb 14 at 10:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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