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
Sahil YadavSahil Yadav 

Wants to update an existing lead record based on new lead record

Hello,
I had a scenario where When user creates a Lead we need to first check that Is this an Existing Lead or not.Based on the data on New Lead record page.
If that data matches with the existing record then just update the record based on the field values that are provided and DO NOT CREATE THE NEW LEAD.
If that data does not matches with the existing records then just CREATE NEW LEAD
 
trigger LeadTrigg on Lead (before insert){
    switch on Trigger.operationType {
        when  Before_Insert{
            Set<String> leadFname = new Set<String>();
            for(Lead l : trigger.new){
                leadFname.add(l.firstname);
            }
            if(leadFname.size()>0 && leadFname != null){
                List<Lead> leadList = [Select id, firstname,email from Lead where firstname IN:leadFname];
                Map<String ,Lead> mapNameWiseAccount = new Map<String,Lead>();
                For(Lead le: leadList)
                {
                    mapNameWiseAccount.put(le.firstname ,le);
                }
                
                For(Lead le : trigger.new)
                {
                    if(mapNameWiseAccount.containsKey(le.firstname))
                    {
                        if(le.email != null){
                            leadList[0].email = le.email;
                            le.Id = leadList[0].id ;
                            update leadList;
                            
                        }
                    }
                }
                
            }
            
            
            
        }
        
    }
}

SO from the code its updating the email field values for an existing recoord but at the same time its creating a new record as well and i needed to restrict the creation of a new record .
Any Suggestion would be helpfull!!!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sahil,

You do this by using merge functionality.

Refer the below link have solution for similar kind of ask.
https://salesforce.stackexchange.com/questions/293825/create-a-trigger-to-merge-leads-if-the-email-already-exists-on-a-lead

If this helps, please mark it as best answer.

Thanks!!
Gracie MarshGracie Marsh
Although converted Leads cannot be updated by design, you can export, modify converted leads externally in Excel, delete the existing leads and then re-import them with changes back into Salesforce as new records.
walgreenslistens (https://www.walgreenslistens.win/)