2

I need a way to lock an Idea and its comments from being editable. I have a time-based workflow already set up to change a custom field after 30 days. The custom field is called voting status with options of "Open" and "Locked". I'm thinking of two options:

Create a trigger to lock the record down after the field change similar to how it locks after an approval process is locked.

Or create a trigger that makes the record read-only after the field change.

Any ideas or examples on how to achieve this? I've been searching the API documentation and found the FOR UPDATE statement, but I haven't been able to implement anything.

Example so far:

Trigger IdeaRecordLock on Idea (after update, after insert) { for (Idea a : Trigger.New) { if (a.Voting_Status__c == 'Locked') { List lockemSockem = [SELECT Id FROM Idea WHERE Voting_Status__c = 'Locked' FOR UPDATE]; } } }

2
  • Do you want the record lock as in an approval process lock/ do you want it read only and dont care about the lock
    – Rao
    Sep 19, 2014 at 16:17
  • FOR UPDATE is intended to be used to lock records only during the context of the running process of a trigger or class. This prevents other users, triggers or classes from modifying those records during the running context/instance of that trigger or class. Its not intended for use as means of permanently locking the records that are returned.
    – crmprogdev
    Sep 19, 2014 at 18:15

2 Answers 2

1

It has been possible to lock records by Id in Apex since Winter16 (late 2015).

If you're interested in not using apex for everything, I've also created an Apex Plugin for Process Builder that can do this (doesn't help with Ideas because they're not in PB, but useful for other locking scenarios):

0

Unless you write a validation rule and check for all the fields you want to keep locked using ISCHANGED(), you will have lock a record with apex. Not to mention the validation rule would unable to scale as you added new fields to the object.

You can submit a record for approval via apex and it will lock it. I've worked in a scenario where I had to use apex to lock and unlock a record. As long as you have an approval process setup on the object you want to lock you should be able to use the SF documentation below: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

2
  • 1
    approval process cannot be created on Ideas, so the second part of the solution won't work.
    – Rao
    Sep 19, 2014 at 23:03
  • Darn, I didn't know that. If you want to use the second option, you can write an approval process in all code. See the 3rd response by Sonam_SFDC: developer.salesforce.com/forums/… Sep 22, 2014 at 19:29

You must log in to answer this question.

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