Salesforce.com: Call Apex webservice method from custom button

Many times we have a requirement to call a apex method from a custom button from standard pagelayout. Below is simple way to do this.
1.)Create a global class with a webservice method and call that from custom button using javascript onClick event.
Create a global class
global class OutboundEmails {

WebService static void SendEmailNotification(string id) {

//create a mail object to send a single email.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

//set the email properties
mail.setToAddresses(new string[] {'EmailAddress here'});
mail.setSenderDisplayName('SF.com Email Agent');
mail.setSubject('A new reminder');
mail.setHtmlBody('an object with ID='+ id + ' is just clicked on.');

//send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

}
}
And have a custom button with display type as “Detail Page Button” and Behaviour type as “Execute JavaScript”, content source as “onClick javascript”.
And the code for javascript
{!REQUIRESCRIPT(“/soap/ajax/10.0/connection.js”)}
{!REQUIRESCRIPT(“/soap/ajax/10.0/apex.js”)}
sforce.apex.execute(“OutboundEmails”,”SendEmailNotification”, {id:”{!Case.CaseNumber}”});


Source  : http://www.saasanalogy.com/call-apex-webservice-method-from-custom-button/http://www.saasanalogy.com/call-apex-webservice-method-from-custom-button/

Comments

Popular posts from this blog

Salesforce.com: Expression Operators in Salesforce lightning Components

Custom Calendar on VisualForce Page

Salesforce.com: Scheduled Apex