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 

Urgent: Approval process through javascript (custom button)

Hey All , 

 

We are using SAP as our ERP . I have created a custom button in salesforce.com on Account Record namely , "SAPRequest" . Whenever user clicks on this button, record should be submitted for approval (WE DONT CARE ABOUT THE APPROVERS , IT SHOULD JUST BE SUBITTED FOR APPROVAL ) . I created a apex class and javascript for this below:

 

Apex class:

 

global class SAPRequest {

WebService static void SendApprovalRequest(string id) {

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

}
}

 

 

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" );

 

 

But when I click the SAPRequest Button , I get this error in dialogue box:

 

A problem with OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:client',
faultstring:'System.DMLException:Process failed.First
exception on row 0,first error :REQUIRED_FIELD_MISSING,
missing required field;[]

class.SAPRequest.SendApprovalRequest: line 9, column 1',}

 

Please help me in fixing this . I will really be grateful to you guys..

 

Regards

Best Answer chosen by Admin (Salesforce Developers) 
klamklam

You need to set the Id of the object submitted for approval:

 

req.setObjectId(id);

All Answers

klamklam

You need to set the Id of the object submitted for approval:

 

req.setObjectId(id);

This was selected as the best answer
devsalesforce27devsalesforce27

Hey Klam , 

 

Thanks for the response . i actually figured out this same thing with in 2 hrs of posting the problem. But , Do you have any idea that what logic is to bee added to javascript custom button that if user is pressing the SAP REQUEST button it shoudl give alert message " request already in approval ". I am not getting the logic behind this . 

 

Regards

CushtyCushty
Hi,
Does anyone know how to create a test class for the above class?