You need to sign in to do that
Don't have an account?
Tiia Tirkkonen 18
How to unlock a record that is in an approval process?
I have a record of the Incident object that is automatically submitted for approval upon creation. The approval process locks the record, but I would like to unlock it right away.
Now in my trigger I have:
trigger IncidentBeforeTrg on Incident__c (before insert, before update) {
But this does not seem to be doing anything. What am I missing?
Now in my trigger I have:
trigger IncidentBeforeTrg on Incident__c (before insert, before update) {
for(Incident__c newInc:trigger.New){
if(Trigger.isUpdate && Approval.isLocked(newInc.Id)){
Approval.unlock(newInc.Id);
}
}
}But this does not seem to be doing anything. What am I missing?
If you dont want to lock the records,
Why dont you set the initial submit actions to unclock the record, or do you have a criteria for unlocking them
Hope it helps
you can unlock it using apex since winter 16..
But To enable this feature, from Setup, enter Process Automation Settings in the Quick Find box, then click Process Automation Settings. Then, select Enable record locking and unlocking in Apex. Have you done that?
What is approval process for?
You can unlock the record in the final approval or reject actions
Regards
RD
RD@SF: I need to unlock the record when it enters the approval process, so it can be edited by anyone also while in the process. Unlocking it at approval or rejection is not sufficient.
Why would you allow the approving record to be allowed to be edited. In case you have multiple approving steps
Step 1 needs a value say 'A' to be true.
Step 2 has another criteria.
if you allow the record to be edited.In between the steps the value A might be edited to false.
Which fails the concept.
Hope you get the idea.
I am just trying to figure out what you want to achieve.
RD
what will be an apex test class for the code below
@Tiia this is your code below, I just modifed it basd on your explanation. If the below is right.. what will be the code coverage (apex test class to use)
trigger IncidentBeforeTrg on Incident__c (after update, after insert ) {
for(Incident__c newInc:trigger.New){
if(Approval.isLocked(newInc.Id)){
Approval.unlock(newInc.Id);
}
}
}
Can you please help.
Thanks,
sanjana
All,
I just got done solving this issue the other day. Here is what you need to do!
1.) in all steps of approval process update a field to a value. In my case I was updating a status. In all these status updates set 're-evaluate workflow rules'
2.) Create new workflow rule to fire on 'true' anytime record is created or updated.
3.) Create field update to update a date time field with now(). This will cause force fire of trigger on object.
4.)write trigger onAfterUpdate to unlock record.
5.) please note once a record is locked you cannot perform dml statements on an object as checking for record lock occurs during system validations in order of the execution which happens before trigger execute.
need more info just ask as this is working for me.
I am working on a similar issue, though am not equiped with the same competencies and so require further guidance.
If I understand correctly, the workaround is to create a workflow rule to update a time field with now(), which is then leveraged by a trigger.
As I have yet to learn Apex, am I out of luck for the time being, or is it possible to copy/paste some modular code and adapt it to my needs?
---
My use case is: A custom object used by multiple departments must enter an approval process for a subsection of stakeholders. While approving, the object must remain unlocked save for specific fields (which I can achieve with Object Manager -> Data Validations & the Status field you mentioned at the beginning).
Thanks!
*In the trigger check for the condition of the updated field and call a future method with the unlock record code.
*this ensures two transactions and it works!
public with sharing class UnlockRecord {
@future public static void Unlock(List<Id> requests) {
for(Id accountId :requests){
try{
Approval.UnlockResult unlockResult = Approval.unlock(accountId);
}
catch(exception ex){
system.debug('Exception*****'+ex);
}
}
}
}