Monday, June 10, 2013

Java Script for custom buttons in salesforce

Java Script for custom buttons in salesforce

The AJAX toolkit includes built-in support for invoking Apex through anonymous blocks or public webService methods.  
To do so, include the following lines in your AJAX code:

<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>

Execute anonymously via sforce.apex.executeAnonymous (script). This method returns a result similar to the API's result type, but as a JavaScript structure.
Use a class WSDL. For example, you can call the following Apex class:

global class myClass { 
  webService static Id makeContact(String lastName, Account a) { 
        Contact c = new Contact(LastName = lastName, AccountId = a.Id); 
        return c.id; 
    }
}

By using the following JavaScript code:

var account = sforce.sObject("Account");
var id = sforce.apex.execute("myClass","makeContact",
                             {lastName:"Smith",
                              a:account});

The execute method takes primitive data types, sObjects, and lists of primitives or sObjects.
To call a webService method with no parameters, use {} as the third parameter for sforce.apex.execute. For example, to call the following Apex class:

global class myClass{ 
   webService static String getContextUserName() {
        return UserInfo.getFirstName();
   }
}

Use the following JavaScript code:

var contextUser = sforce.apex.execute("myClass", "getContextUserName", {});
Note:- If a namespace has been defined for your organization, you must include it in the JavaScript code when you invoke the class. For example, to call the above class, the JavaScript code from above would be rewritten as follows:
var contextUser = sforce.apex.execute("myNamespace.myClass", "getContextUserName", {});
Sample code:-

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

These are Javascript Wrappers around the Force.com API, which allow you to make Calls to your apex code, enable you for javascript remoting etc.

These javascripts are built in and cannot be modified, but you can view them by typing it directly in your browser

Connection.js : https://ap1.salesforce.com/soap/ajax/25.0/connection.js
Apex.js : https://ap1.salesforce.com/soap/ajax/25.0/apex.js

replace the ap1 instance with your server instance.

Custom buttons can be of other type like URL, Visualforce page. After summer 13 release we also global actions with us and by help of them now it is very easy to create new record with some required  fields. Enjoy coding..