You need to sign in to do that
Don't have an account?
Shubham 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;
}
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;
}
If you want to write the helper class separately you just need to write the script and call the script in the trigger.
Trigger: Apex Class:
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)
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 .
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. 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