Updating Salesforce Records Using JavaScript

Sharing is Caring

Over the last few years, Salesforce has made some really significant changes that allow JavaScript to increasingly be used. By using Salesforce’s AJAX toolkit it’s possible to do an incredible amount of customization to a standard Salesforce Page Layout without ever having to write any apex.

Off the top of my head, I can think of dozens of use cases that could be use JavaScript to create Salesforce records or do some other sort of simple automation. For example, fields that aren’t on the page layout can be updated or child records could be created. For example a task could be created to Call a Contact in the future or an Email Welcoming a New Customer could be sent.

The AJAX Toolkit is a JavaScript wrapper around Salesforce’s SOAP API. As you probably know, JavaScript doesn’t have any type checking whatsoever. If you are already familiar with the SOAP API and have done some JavaScript previously, you should have very few problems.

The AJAX Toolkit should be used on really small amounts of data because the SOAP Api has a lot of overhead which can result in really slow performance. I strongly recommend using it sparely as it’s really difficult to debug, and the error messages aren’t always obvious.

To provide a really simple but good example, we’re going to add a Custom Button to the Opportunity Page Layout which will mark an Opportunity as Closed Won.

Step 1: Open up Salesforce and go to Setup.

Step 2: Navigate to Customize -> Opportunities -> Button, Links and Actions

Step 3: Click the “New Button Or Link” button

Step 4: Give the Button a Meaningful Name and Description.

I’ve used “Mark As Won” as my label and let Salesforce automatically fill in the DeveloperName, and for a description I’ve just left it as blank. Make sure to set the Display Type to “Detail Page Button”. The Behavior should be set to “Execute JavaScript”

To take advantage of the AJAX toolkit, we need to include the connection library which provides all of the necessary DML functions.

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

By itself, the connection library is pretty useless. It’s not actually doing anything, so let’s continue. Since we’re adding the javascript to a button, we don’t need to actually do any querying. If we were going to query, we would need to do something like this:

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

var soqlQuery = "SELECT Id,Name from Opportunity LIMIT 1"; 
var opps = sforce.connection.query(soqlQuery).getArray('records'); 
var opp = opps[0];

Make sure that the Id is always included in any SOQL queries, because the Soap API doesn’t automatically return it. I’ve learned this the hardway and spent quite a bit of time trying to debug why something wasn’t working to only realize I forgot to query the Id.

Also note that the sforce.connection.query() method must always be converted into an array for us to get anything useful from it. Again, in the example we’re doing today we don’t actually need to do any querying.

In the large text area enter the following javascript. Take note of the sforce.SObject method which takes in a string of what we’re updating. Also note that I’m using a merge field to get the Id of the object and that sforce.connection.update takes in an array of objects.

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

var opp = new sforce.SObject("Opportunity");
opp.Id = "{!Opportunity.Id}";
opp.StageName = "Closed Won";

//update the opp
result = sforce.connection.update([opp]);

if (result[0].getBoolean("success")) {
	log("opp.with id " + result[0].id + " updated");
} else {
	log("failed to update opp." + result[0]);
}

One major thing to keep in mind is that validation rules will execute, and it’s a really poor user experience not to at least reload the page if the operation was successful and the user has clicked the okay button.

Click the Save button and add it to a page layout. Test that the button works by clicking it on an Opportunity.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.