[SalesForce] how to open the approval/rejection page w/ custom javascript button

How to I open the approval/rejection page for a record with a javascript button?

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 

if({!AND(ISPICKVAL(Repair_Estimate__c.Approval_Status__c, 'Pending-PM Approval'), 
OR( 
AND(ISBLANK(Repair_Estimate__c.Job__c), 
ISBLANK(Repair_Estimate__c.Project__c))))}) 
alert("The job or project needs to be attached to the estimate in order to be approved.") 

else{ 
window.open('/p/process/ProcessInstanceWorkitemWizardStageManager?id={!Repair_Estimate__c.Id}'); 
}

The only thing that isn't working is the else function. I've only got the button to open a single, hardcoded ID record. I get this error:

"Unable to Access Page
The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information. "

Best Answer

You almost had it. The only problem with your button was that the Approval page takes a different type of Id. Its not the id of the record waiting for approval, it needs the id of the ProcessInstanceWorkitem.

Here is the code. I have not tested it, but after any necessary syntax tweaks it should work.

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 

if({!AND(ISPICKVAL(Repair_Estimate__c.Approval_Status__c, 'Pending-PM Approval'), 
OR( 
AND(ISBLANK(Repair_Estimate__c.Job__c), 
ISBLANK(Repair_Estimate__c.Project__c))))})
{ 
    alert("The job or project needs to be attached to the estimate in order to be approved.");
}
else
{
    var result = sforce.connection.query("SELECT Id FROM ProcessInstanceWorkitem WHERE processInstance.TargetObjectId='{!Repair_Estimate__c.Id}'"); 
    var approvals = result.getArray("records"); 

    if(approvals != undefined && approvals.length > 0)
    {
        var approvalID = approvals[0].Id;
        window.open('/p/process/ProcessInstanceWorkitemWizardStageManager?id='+approvalID); 
    }
    else
    {
        alert("There are no approvals pending.") 
    }   
}

Good luck!!

Luis Luciani

Related Topic