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
Harry GHarry G 

Trigger creating multiple records where i need only one?

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) {
        LeadTriggerHandler.createContact(Trigger.new);
    }
        if(Trigger.isUpdate) {
        LeadTriggerHandler.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;
        }
    }
}

 
Best Answer chosen by Harry G
Harry GHarry G
Hi Khan,

Thank you for looking into this.

However i have changed my code a bit , but now i am geeting an error while saving the code:

Class:
 
public class LeadTriggerHandler {
    public static Boolean firstRunComplete = false;

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

        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(lead.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;

        LeadTriggerHandler.firstRunComplete = true;
    }
}
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);
    }
}

Error:

Method does not exist or incorrect signature: void get(Schema.SObjectField) from the type Map<Id,Lead>
 

All Answers

Waqar Hussain SFWaqar Hussain SF
Trigger CreateContact on Lead (after insert, after Update) {
	list<Lead> LeadsToProcess = new list<Lead>();
    if(Trigger.isInsert) {
		for(lead l : trigger.new){
			if(l.Status=='Interviewed')
			LeadsToProcess.add(l);
		}
    }
	if(Trigger.isUpdate) {
		for(lead l : trigger.new){
			if(l.Status=='Interviewed' && trigger.oldMap.get(l.Id).Status != 'Interviewed')
			LeadsToProcess.add(l);
		}
    }
	if(LeadsToProcess.size() > 0)
	LeadTriggerHandler.createContact(LeadsToProcess);
}


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;
        }
    }
}

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harry,

Greetings to you!

You are inserting records inside the 'for loop'. Performing a DML operation inside a loop isn't a good idea.

Change your code as below:
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);
        }
        }
        if(contact.size()>0){
            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);
        }
        }
        if(app.size()>0){
            insert app;
        }
    }
}

Also, you might have a workflow rule that is running a field update.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Harry GHarry G
Hi Khan,

Thank you for looking into this.

However i have changed my code a bit , but now i am geeting an error while saving the code:

Class:
 
public class LeadTriggerHandler {
    public static Boolean firstRunComplete = false;

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

        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(lead.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;

        LeadTriggerHandler.firstRunComplete = true;
    }
}
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);
    }
}

Error:

Method does not exist or incorrect signature: void get(Schema.SObjectField) from the type Map<Id,Lead>
 
This was selected as the best answer
SEKAR RAJ.SEKAR RAJ.
Hi Harry,

You have added DML operation inside the For Loop. Due to that only the contact and custom object records are creating multiple times.
Rest of that, everything seems to be great.
Trigger :
Trigger CreateContact on Lead (after insert, after Update) {
    if(Trigger.isInsert) {
        LeadTriggerHandler.createContact(Trigger.new);
    }
        if(Trigger.isUpdate) {
        LeadTriggerHandler.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);
           }
       }
     if(contact.size()>0){
           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);
            }
        }
     if(app.size()>0){
         insert app;
     }

 
    }
}

Thanks,
SEKAR RAJ
Harry GHarry G
Hi Khan and Sekar,

Thanks for your suggestion , i tried that , but that didn't resolve the issue , as i was not comapring  new and old value of the record.

With the above code that i have given it's working.

Thanks,
Harry
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harry,

You don't need to change your trigger code. I told you to change the code of the handler class only.

And if you don't have a workflow rule that is updating a field, this code will work fine.
 
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);
        }
        }
        if(contact.size()>0){
            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);
        }
        }
        if(app.size()>0){
            insert app;
        }
    }
}

Regards,
Khan Anas