Update Records with JavaScript Buttons for Salesforce

create salesforce record
Create a Salesforce Record with a Button
October 24, 2013
salesforce multi-picklist
Importing into a Salesforce Multi-Picklist
May 12, 2014

Update Records with JavaScript Buttons for Salesforce

update records with javascript button

After writing the post about creating records with a button, it made sense to extend that thought with manipulating record with a JavaScript button click.

Again, I’m not a JavaScript expert but here are some of the tricks I’ve learned to help automate tasks within Salesforce. Let’s take a look at some of the possibilities:

With all of these examples you’ll need to create your JavaScript button so start by doing that. My examples below are intended to be simple examples for you to adapt to your specific needs.

In this first example, I query the record and display the results in an alert window:

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

// query the record
var qr = sforce.connection.query("SELECT Id, SyncedQuoteId FROM Opportunity where Id='" + "{!Opportunity.Id}" + "'");
var records = qr.getArray("records");

// display the field from the query
alert("SyncedQuoteId: " + qr.records.SyncedQuoteId);

This next example updates the opportunity stage.

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

// identify the record
var o = new sforce.SObject("Opportunity");
o.id = "{!Opportunity.Id}";

// make the field change
o.StageName = "Closed Won";

// save the change
sforce.connection.update([o]);

//refresh the page
window.location.reload();

Hope this helps you update records with ease.

Terry Miller
Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.

7 Comments

  1. Divya says:

    Hi,

    I am trying to create a button called ‘ Load Defaults’ in opportunity create page which when clicked populates fields with default values defined for every user in another object. I’m not sure how to load values in a form on click of button. Can you please help with it?

    Thanks in advance,
    D

  2. Michele McGeoy says:

    Hi Terry:
    This is really helpful. Any hints on how I could return the user to their home page after the data has been updated?

    Thanks, Michele

  3. Drew Burks says:

    Do validation rules run when doing this? I have a JS button to update the stage, but it does nothing and returns no errors on records where I’d like to have a validation pop up.

    • First, sorry for the delay in my response. Validation rules should run. I’m speculating here but it would make sense to me that if a validation rule is triggered, the update will fail. However, my code sample isn’t trapping for that so no notification is showing. I’ve not tried this but try this and see if it gets you closer to what you’re wanting:


      try {
      sforce.connection.update([o]);
      }
      catch(err) {
      alert(err.message);
      }

      I’m not sure if that will work or not but worth a try.

  4. Ignacio says:

    Good web site! I really love how it is easy on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made.
    I have subscribed to your feed which must do the trick!
    Have a nice day!

Leave a Reply