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
Samantha ChuaSamantha Chua 

Retrieve Case Object from Attachment Parent ID

Hi,

I would like to know how I can retrieve a Case Object based on the Attachment Parent ID (Trigger). Been trying to do so but not making any progress. Would appreciate some advice on how I can go about doing so~!

Cheers
Best Answer chosen by Samantha Chua
Bhanu MaheshBhanu Mahesh
Hi Samantha,

Standard Salesforce functionality does not allow any modification to Note and Attachment object.

Best Approach to create Attachment trigger can be: Create trigger from Developer console.

Need to click New: Apex Trigger: Then you can select SObject as "Attachment" form dropdown.

Refer the below article
https://help.salesforce.com/apex/HTViewSolution?id=000181538&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000181538&language=en_US)

Try below code
 
trigger caseInformation on Attachment(before insert){
	Set<Id> caseIds = new Set<Id>();
	List<Case> caseList = new List<Case>();
	for(Attachment att : trigger.new){
		if(att.ParentId != null && String.valueof(att.ParentId).startsWith('500')){
			caseIds.add(att.ParentId);
		}
	}
	if(!caseIds.isEmpty()){
		caseList  = [SELECT Id,Subject FROM Case WHERE Id IN : caseIds];
	}
	System.debug('case List ' + caseList);
}

Mark this as "SOLVED" if your query is answered.

Thanks & Regards,
Bhanu Mahesh Gadi