3

I'm new to Salesforce and even newer to apex, so I'm struggling a bit here.

My goal is to lock down an Opportunity once a sales order has been generated on my company's side. My current idea is to have a custom read-only "Locked" field within the Opportunity object. I want to use the SOAP API to update this value and then have an Apex Trigger (after update) set all the fields in the Opportunity to read-only.

Where I am getting slowed down is in writing an Apex trigger to set fields to read-only, currently I only have the boilerplate code

trigger lockOpportunity on Opportunity (after update) {
    for (Opportunity myOpp : Trigger.new){
        myOpp.setAllMyFieldsToReadOnly;
    }
}

I can get as far as setting specific values, along the lines of myOpp.Description = "opp description"; but I don't know how to access field properties to set them to read-only. Or, if there's a way to lock down the entire object in a single call, that would be even better.

Thanks.

2
  • Have you looked at using using a work flow to change the record type/page layout which has all the fields read only? While this isn't true read only if the user can access the api (dataloader) it may fit your use case.
    – Girbot
    Commented Jul 9, 2014 at 19:36
  • I actually did not know about that alternative. I'll have to look into it, thanks!
    – Andrew
    Commented Jul 10, 2014 at 14:17

1 Answer 1

5

You can't set fields to Read-Only for a specific record. There are two possible solutions and neither of them require code.

  1. Add a validation rule to the Opportunity object that has criteria something like

    Locked__c && !ISCHANGED(Locked__c) && ISCHANGED(LastModifiedDate)

  2. Create a page layout that has all the fields marked as read-only and then have a record type called Locked that has the read-only layout assigned to it. When the Locked field is checked, then have a workflow rule update the record type.

In most cases you should really use both. The validation rule is great for ensuring that it's locked in the myriad alternative ways to edit records, i.e. list view mass edit and data loader. The page layout will improve the end user experience by not letting them do a huge number of edits only to block them on save (in other words, wasting their time).

Also, in most cases you'll need to adjust the criteria for the workflow rule to allow edits by certain profiles, i.e. finance teams and system administrators.

5
  • 1
    Just to be careful, you might include "&& !ISNEW()" You could use a trigger that would compare the LMD of the Trigger.old record to now() - because the LMD of trigger.new is not set yet at the time your trigger fires - but that's so counter-intuitive to how users work, and validation rules (even those implemented via trigger) result in many frustrated users due to hitting error messages instead of just seeing that the fields are not editable. I would do both of Daniel's solutions together, instead of picking one or the other. Commented Jul 9, 2014 at 19:56
  • I'll second @DavidSchach, user's hate this sort of validation rule since without the page layout lock down they can make a ton of edits which then get discarded when they try and save. The validation rule is critical to ensure all gaps are covered, but the page layout technique will do wonders to ensure a good user experience. Commented Jul 9, 2014 at 21:48
  • I've also added a Visualforce page to the standard layout explaining why the user can't edit any fields. Commented Jul 10, 2014 at 2:00
  • This is very good information, thank you. I wasn't even aware of the existence of workflows/validation rules. Like I said, I'm very new to Salesforce. I have some more research to do.
    – Andrew
    Commented Jul 10, 2014 at 14:19
  • Just an add-on that I have found users like, when creating a locked record type and page layout (with validation rules on the side), I remove the edit button from the page layout to help users remove the thought of editing. Commented Jul 4, 2015 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .