anusha(salesforce developer)

Thursday, 15 September 2016

Calling Apex method from a Custom Button

Calling Apex method from a Custom Button

To call Apex method from Custom Button, follow the below steps

1. Create a custom button and enter the following code

Syntax:

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

sforce.apex.execute("Class_Name","Method_Name",{parameter_Name:"value"});
location.reload(true);

Code:
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

var r = confirm("Record will not be able to edit after sending it to Approval Process.\nAre you sure want to send it for approval process?");
if(r == true)
{
    sforce.apex.execute("memberApproval","callApproval",{localId:"{!Member__c.Id}"});
    alert("Sent it for Approval");
}


2. Create a apex class with following code

Syntax:

global class ClassName {
    webservice static void methodName(DataType variable) 
                …………………        
                …………………
                …………………
    }
}

Code:

global class memberApproval {
    webservice static void callApproval(Id localId) 
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitted for Approval');
                req1.setObjectId(localId);
                Approval.ProcessResult res = Approval.Process(req1);                  
    }
}

Output:


No comments:

Post a Comment