1

Currently I have an approval process, that locks the opportunity and the record but the user, still can submit Notes & Attachments after. Is there a way to lock those as well?

3

As you can see there is an Idea for this.

What you can do however is the following:

  1. Add a custom field to your object called is_locked__c.
  2. Upon entry to the approval process, use a field update to set the value to true
  3. Upon exit (approved,rejected,revoked), use a field update to set to false
  4. Write a before insert trigger on Attachment that interrogates the parentId via SOQL to see if the parent object is locked. If yes, use addError to prevent attachment for insertion. Do the same for Note.

No. 4 isn't the best user interface as what you might want is to disable the actual Attach a File button. This is harder as you'll need to use a VF page and a custom component that replicates the Notes and Attachments related list + buttons.

You might want to revisit the requirement to see if you can tolerate notes and attachments added while a record is in an approval process

The trigger would look something like this

trigger AttachmentVerification on Attachment (before insert) {
  Set<ID> oParentIdSet = new Set<ID>();

  // Step  1 - get list of parent Oppo ids
  for (Attachment a : Trigger.new)
    if (a.parentId.getSobjectType().getDescribe().getName() == 'Opportunity')
      oParentIdSet.add(a.parentId);  // only attachments under Oppos are interesting

  // Step 2 - Get those Oppos, if any 
  Map<ID,Opportunity> oIdToOppoMap = new Map<ID,Opportunity> (
     [select id, is_locked__c from Opportunity where id IN: oParentIdSet]);

  // Step 3, revisit Attachments to see if any parents are locked
  for (Attachment a: Trigger.new)
    if (oIdToOppoMap.containsKey(a.parentid) && oIdToOppoMap.get(a.parentId).is_locked__c)
     a.addError('You can\'t add an attachment to an Opportunity in an approval process');   
}
11
  • I love your answer, I just dont know how to do number 4 yet. if I am doing opportunity what field am I using Marketing_Material_Request__c – Metawaa Feb 19 '15 at 20:37
  • Oh wow thank you very much, so this basically says , if the opportunity is in Pending mode, and the Is_locked_c = true, then "You cant add an attachment .." – Metawaa Feb 19 '15 at 23:08
  • not sure what you mean by 'pending' but if the approval process entry criteria set is_locked__c to true and reset to false upon exit from approval process, then the attachment can't be uploaded – cropredy Feb 19 '15 at 23:13
  • I think I got it , Ill do these and if something happened Ill message again , if you don't mind @crop1645 – Metawaa Feb 19 '15 at 23:21
  • Do I just add the trigger from here, or Do I need to create VF Page and component to? @crop1645 – Metawaa Feb 20 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.