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
Smita HodiggeriSmita Hodiggeri 

Trigger to check the existing lead and print the existing lead details followed by appending details to the existing description field of lead record.

Hey Everyone,
 Moving from administration to coding here.Your understanding is much apprecaited. :)
So the scenario I am trying to handle with apex is When portential "lead " record will attempeted to create by external party(Zapier) which is triggered by Facebook messenger I am cheacking for "Psid" of the sender which is unique to every user. So for ex: If Anna sends series of messages to Flower'em asking for floral arrangments I would like to capture the whole message as a description in a lead record and later process the info in that description box.
So now, I am all good in preventing the duplicate lead creation when same person sends series of messages.Howver I am not able to print or extract the exitsing description the lead created and append the subsequent messages to the lead's description field. I am wondering how do I go about this.
My code so far.
trigger CheckPsidBeforeCreatingNewLead on Lead (before insert) {

    Lead exLead = null;
    String NewMessageDescription;
    
    for(Lead l : Trigger.new){
        for(Lead l1 : [select ATMTApp_Test1__SenderPsid__c from Lead]){
            if(l.ATMTApp_Test1__SenderPsid__c == l1.ATMTApp_Test1__SenderPsid__c){
                l.addError('This is a duplicate Lead');
                exLead =l1;
                NewMessageDescription = l.Description;
            }
                                    
        }
        if(exLead!=null){
        System.debug(exLead);
    String ExistingLeadId = (String) exLead.get('id');
        System.debug(ExistingLeadId);
        
      String ExistingDescription =[Select Description from Lead where id = '+ ExistingLeadId +'].Description;
        System.debug(ExistingDescription);
        
        
    }
         
    //exLead.set(Description) = exLead.get(Description) +'---------New Message Recevied' +  NewMessageDescription ;
        
}
}
Any help is much appreciated.
Many thanks,
Smita V.H
Smita HodiggeriSmita Hodiggeri
The latest version of the code is 

trigger CheckPsidBeforeCreatingNewLead on Lead (before insert) {

    Lead exLead = null;
    String NewMessageDescription;
    
    for(Lead l : Trigger.new){
        for(Lead l1 : [select ATMTApp_Test1__SenderPsid__c,Description from Lead]){
            if(l.ATMTApp_Test1__SenderPsid__c == l1.ATMTApp_Test1__SenderPsid__c){
                l.addError('This is a duplicate Lead');
                exLead =l1;
               
            }
               
                                    
        }
        
          exLead.Description = exLead.Description + l.Description + 'My hardcoded value';
                update(exLead);
                    System.debug(exLead.Description);
                    System.debug(exLead.Id); 
                   
    }
   
}

In the debug console I can see the appending happening to the description field but it is not getting updated on the lead record.