+ Start a Discussion

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.');
// 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());






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.




var isLocked = {!sObject.In_Approval_Process__c};


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



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



Let me know if this would help you?




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 .