Salesforce Custom Buttons: No Mess, No Fuss


jasongabler

After trying a few methods, I found the following to be the cleanest, easiest way to add an Apex-executing custom button to a Salesforce detail page.  Essentially, you’re using Force.com’s AJAX library to make a function call from a VisualForce page to an Apex method. If you’re not familiar with Javascript, you might be doing some educated copying and pasting.  Knowing some simple JSON notation will be very helpful.  It might sound cumbersome, but this pattern really is quite quick and easy.

I like to create my Apex method first. This way I’ve defined what I need from the button’s page before writing any JavaScript.

 global class exampleClass { WebService static String exampleMethod(String id, String name, String accountId) { String result = '{"type":"url", "data":"/'+accountId+'"}'; try { List contacts = [SELECT Id FROM Contact WHERE Id = :id]; if (contacts.size() < 1) { result = '{"type":"msg", "data":"Unable to find contact."}'; }…

View original post 528 more words