Monday, November 14, 2016

How to execute Apex from Custom button or Javascript ? Give Example.


It is possible using Ajax toolkiit.
1global class myClass {
2    webService static Id makeContact (String lastName, Account a) {
3        Contact c = new Contact(LastName = lastName, AccountId = a.Id);
4        return c.id;
5    }
6}
we can execute above method from javascript like :
1{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
2{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
3var account = sforce.sObject("Account");
4var id = sforce.apex.execute("myClass" "makeContact",
5{lastName:"Smith", a:account});
To call a webService method with no parameters, use {} as the third parameter for sforce.apex.execute .
Also, you can use the following line to display a popup window with debugging information:
sforce.debug.trace=true;

No comments:

Post a Comment