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
Kajal Singh 36Kajal Singh 36 

Creating a trigger which will copy the data from Leads Object to a custom Lead Object...After Insert.Please help me out to write a trigger. Thanks in advance!!!

Best Answer chosen by Kajal Singh 36
RituSharmaRituSharma
Your trigger logic will be like below:

List<CustomLead__c> customLeadsList = new List<CustomLead__c>();
for(Lead ld: trigger.new) {
    CustomLead__c customLead = new CustomLead__c();
    customLead.Name = ld.Name; //map other needed fields same way
    customLeadsList.add(customLead);        
}
insert customLeadsList;

All Answers

RituSharmaRituSharma
Use flow or process builder instead of trigger. Declarative ways are always better.
ShirishaShirisha (Salesforce Developers) 
Hi Kajal,

Greetings!

In order to achieve this the custom Lead Object should have the relationship with the Standard Lead Object then you can create the Trigger on Lead Object after insert as suggested in the below link:

https://developer.salesforce.com/forums/?id=906F00000008zfdIAA

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Kajal Singh 36Kajal Singh 36
Hi Ritu,

Could you please give me instructions to create a process builder to take all the data from standard lead to custom lead object....
Kajal Singh 36Kajal Singh 36
Hi Shrisha,

i already tried this trigger this is not working for me...
Any other suggestion..
RituSharmaRituSharma
Create a process builder on lead object. Refer this URL for details - https://trailhead.salesforce.com/en/content/learn/modules/business_process_automation/process_builder
Kajal Singh 36Kajal Singh 36
Thanks Ritu. I know how to create process builder but copy the data from standard lead to custom lead.. Idk how to do this?
RituSharmaRituSharma
Use Create a Record action and then set each field of custom lead by maping to standard lead field.
Kajal Singh 36Kajal Singh 36
Do you have any idea about how to write trigger to copy the data from standard lead to custom lead....???
RituSharmaRituSharma
Your trigger logic will be like below:

List<CustomLead__c> customLeadsList = new List<CustomLead__c>();
for(Lead ld: trigger.new) {
    CustomLead__c customLead = new CustomLead__c();
    customLead.Name = ld.Name; //map other needed fields same way
    customLeadsList.add(customLead);        
}
insert customLeadsList;
This was selected as the best answer
Kajal Singh 36Kajal Singh 36
Why i am getting an error on line 4?
Invalid Loop

public class LeadTriggerHelper {
    public static void insertLead(List <Lead__c> leadList) {
         list<Lead__c> LeadsList = new List<Lead__c>();
    for(Lead ld: leadlist) {
    Lead__c Lead = new Lead__c();
    Lead.Name__c = ld.Name;
    Lead.City__c = ld.City;
    Lead.State__c = ld.State;
    Lead.Lead_Status__c = ld.Status;
    Lead.Email__c = ld.Email;
    Lead.Pincode__c = ld.PostalCode;
    Lead.Country__c = ld.Country;
    Lead.OwnerId = ld.OwnerId;
   
    
    LeadsList.add(Lead);        
}
insert LeadsList;
}

}
RituSharmaRituSharma
Line no. 4 is for(Lead ld: leadlist). It's throwing error because of incompatible type. leadList is a list of Lead__c type but ld is of Lead type.
Kajal Singh 36Kajal Singh 36
So What can i write?
 
Kajal Singh 36Kajal Singh 36
If i correct this so all my field mapping value throw an errror
RituSharmaRituSharma
Use below code:

    public class LeadTriggerHelper {
        public static void insertLead(List <Lead> leadList) {
            List<Lead__c> newLeadsList = new List<Lead__c>();
            for(Lead ld: leadlist) {
                Lead__c newLead = new Lead__c();
                newLead.Name__c = ld.Name;
                newLead.City__c = ld.City;
                newLead.State__c = ld.State;
                newLead.Lead_Status__c = ld.Status;
                newLead.Email__c = ld.Email;
                newLead.Pincode__c = ld.PostalCode;
                newLead.Country__c = ld.Country;
                newLead.OwnerId = ld.OwnerId;
                newLeadsList.add(newLead);        
            }
            insert newLeadsList;
        }
    }
Kajal Singh 36Kajal Singh 36
Error is created when i try to test the lead..


Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Lead, please contact Customer Support.

Reason: Apex trigger ObjTrigger caused an unexpected exception, contact your administrator: ObjTrigger: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Email]: [Name, Email]: Class.LeadTriggerHelper.insertLead: line 17, column 1
    Lead Capture Page: Not available.

Record Information:

    assigned_date size= = 05 nov
    assigned_to = kajol
    bhk_size = 3
    budget_range = 5788990
    city = bangalore
    email_id = aprajita@gmail.com
    encoding = UTF-8
    encoding = UTF-8
    lead_id = 99
    lead_name = apraita
    lead_source = acre
    lead_status = In progress
    location = marathali
    mobile_no = 23456789
    oid = 00D2z0000008jUc
    purchase_time = 4pm
    remark = noy
    remark_by = gyy
    remind_date = 7 nov
    retURL = http://www.google.com
RituSharmaRituSharma
Share your exact code.
Kajal Singh 36Kajal Singh 36
Apex Code

public class LeadTriggerHelper {
        public static void insertLead(List <Lead> leadList) {
            List<Lead__c> newLeadsList = new List<Lead__c>();
            for(Lead ld: leadlist) {
                Lead__c newLead = new Lead__c();
                newLead.Name__c = ld.LastName;
                newLead.Phone_No__c = ld.Phone;
                newLead.City__c = ld.City;
                newLead.State__c = ld.State;
                newLead.Lead_Status__c = ld.Status;
                newLead.Email__c = ld.Email;
                newLead.Pincode__c = ld.PostalCode;
                newLead.Country__c = ld.Country;
                newLead.OwnerId = ld.OwnerId;
                newLeadsList.add(newLead);        
            }
            insert newLeadsList;
        }
    }
--------------------------------------------------------------------------------------------

Trigger Code

trigger ObjTrigger on Lead (after insert) {
  LeadTriggerHelper.insertLead(Trigger.new);
    
}
 
RituSharmaRituSharma
Set the value of Name field as well.
Kajal Singh 36Kajal Singh 36
How can i do this.. Actually i am new to salesforce. so idk how to do this
RituSharmaRituSharma
I can't see the schema in your org so can't say surely. But you will need to write something like:

newLead.Name = ld.LastName;
Kajal Singh 36Kajal Singh 36
Api name of name is Name__c
RituSharmaRituSharma
Every object has a name field. If you have custom name field as well, you will need to set both like below:
newLead.Name = ld.LastName;
newLead.Name__c = ld.LastName;
Kajal Singh 36Kajal Singh 36
this gives an error... This field is not writable
Kajal Singh 36Kajal Singh 36
I got this error.. What does it mean?

Apex script unhandled trigger exception by user/organization: 0054K0000028T7V/00D2z0000008jUc
Source organization: 00D4K000002TIc6 (null)
ObjTrigger: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Email]: [Email]

Class.LeadTriggerHelper.insertLead: line 18, column 1
Trigger.ObjTrigger: line 2, column 1
RituSharmaRituSharma
As per error emssage, you need to set the value of Email field. So try like below:

newLead.Email = ld.Email;
RituSharmaRituSharma
Please mark my answer as the best answer if it was helpful for you. This would help others in future.
Kajal Singh 36Kajal Singh 36
Thank you so much ritu.. But this code is not working for me..  You have any other idea to do this...?
RituSharmaRituSharma
Unfortunately, I would not be able to help with respect to field mapping since I can't access your org. You would need to analyse the schema and see what all fields need to be mapped.