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
Shubham Joge 9Shubham Joge 9 

I am new to salesforce .I have a trigger in Sandbox that updates a checkbox field to true if an attachment is attached to an Lead record. please help me with code to write trigger and helper seprately

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
 String tempParentId;
    Set<Id> setParentId = new Set<Id>();
    List<Lead> Leadlst = new List<Lead>();
    
 for (ContentDocumentLink cdl : trigger.new ) {
            tempParentId = cdl.LinkedEntityId;
     
            if (tempParentId.left(3) =='a0X') {
                System.debug('Debug : found a0X');
                System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                setParentId.add(cdl.LinkedEntityId);
            }
        }
    Leadlst = [select Id , HasAttachment__c from Lead where Id IN :setParentId];
     
     For(Lead L : Leadlst)
     {
        L.HasAttachment__c = True;
     }

     update Leadlst;
}
 
sachinarorasfsachinarorasf
Hi Shubham,
If you want to write the helper class separately you just need to write the script and call the script in the trigger.
Trigger:
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
    if(trigger.isAfter && trigger.isInsert){
        ContentDocumentLinkController.UpdateLeadRecord(trigger.new);
     }
}
Apex Class:
Public Class ContentDocumentLinkController{
    public static void UpdateLeadRecord(List<ContentDocumentLink> contentDocLink){
       if(contentDocLink != null && contentDocLink.size()>0){
           String tempParentId;
           Set<Id> setParentId = new Set<Id>();
           List<Lead> Leadlst = new List<Lead>();
           for(ContentDocumentLink cdl : contentDocLink){
               tempParentId = cdl.LinkedEntityId;
               if(tempParentId.left(3) =='a0X') {
                  System.debug('Debug : found a0X');
                  System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                  setParentId.add(cdl.LinkedEntityId);
                }
            }
            Leadlst = [select Id , HasAttachment__c from Lead where Id IN :setParentId];
            For(Lead L : Leadlst){
              L.HasAttachment__c = True;
            }
           update Leadlst;
      }
   }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com (http://www.sachinsf.com" style="text-decoration:none)

Shubham Joge 9Shubham Joge 9
Thankyou @sachinarorasf 
it help me to understand how to write helper class , but code is not updating the checkbox when any attachement is attached to the lead object , will you please help me why this is happening .
sachinarorasfsachinarorasf
Hi Shubham,
You can use the below piece of code. Here I have not checked the left three place value. I just fill the Ids in a set. And query the lead with the set  Id then it will retrieve lead record only. 
Public Class ContentDocumentLinkController{
    public static void UpdateLeadRecord(List<ContentDocumentLink> contentDocLink){
       if(contentDocLink != null && contentDocLink.size()>0){
           Set<Id> setParentId = new Set<Id>();
           List<Lead> Leadlst = new List<Lead>();
           for(ContentDocumentLink cdl : contentDocLink){
                  System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                  if(cdl.LinkedEntityId != null)
                  setParentId.add(cdl.LinkedEntityId);
            }
            Leadlst = [select Id , HasAttachment__c from Lead where Id IN :setParentId];
            if(Leadlst != null && Leadlst.size() >0 ){
               For(Lead L : Leadlst){
                   L.HasAttachment__c = True;
               }
              update Leadlst;
           }
      }
   }
}
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com