function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
devsalesforce27devsalesforce27 

execute custom button through javascript

Hey All , 

 

I have created a custom button to send the user record for approval. This button is working fine when it is pressed first time. I want that when it is pressed again it should give an alert message "User record already in approval process". I know it is very easy but I am new to programming so pls help out in this.apex class and javascript is below:-

 

Apex class:

 

global class SAPRequest1 {

WebService static void SendApprovalRequest(Id AccountId ) {

// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(AccountId);
// submit the approval request for processing
Approval.ProcessResult result = Approval.Process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());

}
}

 

 

Javascript:

 

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 
sforce.apex.execute("SAPRequest","SendApprovalRequest", {id:"{!Account.Id}"}); 
window.alert("Record sent for approval" );

 

 

 

:) :) :) :) :):) :) :) :) :)

I am not sure how would you check if record is in approval process or not but you can work around that by creating a custom field on the object called In_Approval_Process__c (Checkbox), and edit your approval process to set this field when the record is submitted for approval and clear it after approval is complete. Hide this field from all page layout so no one can see it and edit it.

 

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

 

var isLocked = {!sObject.In_Approval_Process__c};

if(isLocked){

    alert("Record is already in approval process");

}

else{

sforce.apex.execute("SAPRequest","SendApprovalRequest", {id:"{!Account.Id}"}); 
window.alert("Record sent for approval");

}

 

Let me know if this would help you?

 

Thanks!

devsalesforce27devsalesforce27

Hey Ketan , 

 

Thanks for your response. The can be one of the ways but , right now , We are not looking for this . I am trying to figure out some other way , if I find , will keep you posted .

 

Thanks