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
Cameron BumsteadCameron Bumstead 

Checkbox marked true after attachment is attached on opportunity

Hello,

I want a checkbox to be marked true after a contract/document is attached to a standard opportunity record. I want the field called "Contract_Attached__c" to be marked true once something is attached to the opportunity. I will make the field read-only so they cannot manually mark the field true. Let me know if someone can help!
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You need to write a trigger on Attachment object and then query the opportunity record using the ParentId field on Attachment and update the checkbox. Try this
trigger UpdateCheckbox on Attachment (after insert)
{
	Set<Id> setOpportunityId = new Set<Id>();
	List<Opportunity> lstOpp = new List<Opportunity>();
	for(Attachment att: trigger.new){
		if(att.ParentId.startsWith('006'))
			setOpportunityId.add(att.ParentId);
	}
	for(Opportunity opp: [Select Id, Contract_Attached__c from opportunity where Id IN:setOpportunityId]){
		opp.Contract_Attached__c = True;
		lstOpp.add(opp);
	}
	if(lstOpp.size()>0)
		update lstOpp;
}