+ Start a Discussion
Shamus Kelley 4Shamus Kelley 4 

Control/lock Custom OnClick Button while record is within approval process

How can I modify this code to prohibit custom button from functioning while record is within the approval process? See image for code detail. Also fire alert "Cannot use button while record is within the approval process"

thank you-

User-added image
@Karanraj@Karanraj
You can query the ProcessInstance object to check whether the record is under approval process or not
ProcessInstance approvalCheck = [Select Id From ProcessInstance WHERE TargetObjectId =: Id AND Status = 'Pending' Limit 1];
Then check the object instance in the if condition to proceed further
 
Morgan MarcheseMorgan Marchese
Hi Shamus,

Unless I'm mistaken it looks like you're already doing something similar? You already have a string check and an alert that pops up if Approvals are required. Is this not your goal? If they match the string check you doIt() but if it doesn't match the string check you generate an alert upon pressing the button.

If you're looking for a separate check, then I would think your best option is a checkbox (Boolean) that gets automatically set to True via workflow/lightning process builder after all necessary approvals are complete.

Then add to your // check opp section:
Boolean approval = "{!Opportunity.Checkbox_Field_Here__c}";
if (approval == true) {
doIt();
}
else{
alert("Cannot use button while record is within the approval process")
}