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
Shivom Verma 34Shivom Verma 34 

Trigger to update a case record when notes and attachments are inserted/updated/deleted

-> i want  to update a case record whenever Notes or Attachments are added/updated/deleted
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shivom,

>> https://help.salesforce.com/articleView?id=000330418&type=1&mode=1

You can try checking the below link that seems to have an implementation in regards to the same that has an implementation of trigger on attachment that fetches the list of cases.

Below is a sample that you can check for quick reference:
 
trigger SetTitleToAttachment on Attachment (after insert) {

String Title;
Id pId;

for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}

List<Case> c=[select Id , Title__c from Case where Id=:pId];

//assuming one record is fetched.
c[0].Title__c=Title;

update c[0];

}

Help Article (https://help.salesforce.com/articleView?id=000330418&type=1&mode=1)

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Shivom Verma 34Shivom Verma 34
Hey anutej 

c[0].Title__c=Title
Can u explain this line
Thanks 
 
Shivom Verma 34Shivom Verma 34
I want to update a checkbox instead of this 
ANUTEJANUTEJ (Salesforce Developers) 
c[0].Title__c=Title this line means there is a custom field on case object and on this field you are setting the title as the title of file being attached, to update the checkbox you might want to change this to a for loop something like below:
 
List<Case> cList=[select Id , Title__c from Case where Id=:pId];
List<Case> toUpdate = new List<Case>();
for(Case c: cList)
{
case temp_case= new case();
temp_case.id=c.id;
temp_case.AttachmentCBox__c= true;
toUpdate.add(temp_case);
}
if(toUpdate.size()>0)
{update toUpdate;}
Please note above code is a sample snippet and you need to modify it to your requirement.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
ANUTEJANUTEJ (Salesforce Developers) 
If the query is addressed can you please close the thread by marking the best answer so that it can be useful to others in the future.

thanks.