You need to sign in to do that
Don't have an account?
NPoulos
APEX Validation: Attach File before setting to Closed Won
Hello all:
I would like to require that a user upload an attachment to opportunities before being able to set them to Closed - Won. I am unsure how to implement this in APEX.
Any guidance would be greatly appreciated.
Thanks.
You could have a Boolean (Checkbox) field on Opportunity.
Write a trigger on Attachment which updates the relevant Opportunity to set this checkbox, when the User uploads an attachment.
Then have a validation rule on Opportunity, which prevents a Stage change to Closed-Won unless this Checbox is checked.
Definitely a great idea, thanks.
Are there any examples of APEX code referencing an Attachment? Or anything else similar I could glean from? I'm a little lost on the syntax around this.
Again, thanks for your help!
Here's something to get you started ....
trigger AttachmentAfter on Attachment (after insert) {
String oppKeyPrefix = Opportunity.SObjectType.getDescribe().getKeyPrefix();
Id[] oppIds = new Id[]{};
for(Attachment att : trigger.new)
{
if(att.ParentID != null && ((String)att.ParentId).startsWith(oppKeyPrefix))
oppIds.add(att.ParentId);
}
Opportunity[] oppsToUpdate = new Opportunity[]{};
for(Opportunity opp : [Select Id, Name, Attachment__c from Opportunity where Id in :oppIds]){
opp.Attachment__c = true;
oppsToUpdate.add(opp);
}
if(oppsToUpdate != null && !oppsToUpdate.isEmpty())
Database.update(oppsToUpdate);
}
You can also approach this the other direction:
This is a real-time solution, as opposed to the other suggestion, which, as it stands, could be fooled by attaching a file then deleting it... or if field security was not configured correctly, could be bypassed entirely through an import or data loader operation.