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
sneha walisneha wali 

updating the old value

we have lead record with name,phone num,email id and lead source. now if we create one more lead record with same name, phone num, email id but with different lead source, how to update the old lead source value with new lead source value using trigger retaining all the other information?
Sujit Anil NirkheSujit Anil Nirkhe

You can do it with Lead trigger.

In before insert, search for all existing leads with same name, and later compare its other fields. If this comes to true, update source on existing record and add this lead to to  be updated list, and for record to be discarded, use .addError() method to truncate its insertion.

Here I guess you may need to chack in trigger .new also for duplication existance.

sneha walisneha wali
hi Sujit Nirkhe 18,
Thanks for the reply.If you dont mind can you plz explain with code?

I have written in this way.

public class LeadUpdate {
    public static void Checkvalidationinsert(List<Lead> newList, Map<id, Lead> newMap, List<Lead> oldList, Map<id, Lead> oldMap){
        Set<String> leadph = new Set<String>();
        for(Lead l : newList){
            leadph.add(l.Phone);
        }
        
        List<Lead> ld = [SELECT Id,LeadSource,Phone FROM Lead where Phone IN : leadph];
        if(ld.size()>0){
            for(Lead l : newList){
                ld.LeadSource = l.LeadSource;
                 newList[0].addError('Phone number exist');
              }
        }
    }
        
}

But I am getting this error

Error: Compile Error: Initial term of field expression must be a concrete SObject: List<Lead> at line 11 column 17