Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
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 Jul 9 at 19:36
    
I actually did not know about that alternative. I'll have to look into it, thanks! –  Andrew Jul 10 at 14:17
add comment

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
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. –  DavidSchach Jul 9 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. –  Ralph Jul 9 at 21:48
    
I've also added a Visualforce page to the standard layout explaining why the user can't edit any fields. –  Daniel Hoechst Jul 10 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 Jul 10 at 14:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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