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
Applies to: Classic

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")}</p><br /><p>// query the record<br />var qr = sforce.connection.query("SELECT Id, SyncedQuoteId FROM Opportunity where Id='" + "{!Opportunity.Id}" + "'");<br />var records = qr.getArray("records");</p><br /><p>// display the field from the query<br />alert("SyncedQuoteId: " + qr.records.SyncedQuoteId);<br />

This next example updates the opportunity stage.

<br />{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}</p><br /><p>// identify the record<br />var o = new sforce.SObject("Opportunity");<br />o.id = "{!Opportunity.Id}";</p><br /><p>// make the field change<br />o.StageName = "Closed Won";</p><br /><p>// save the change<br />sforce.connection.update([o]);</p><br /><p>//refresh the page<br />window.location.reload();<br />

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.


  1. Divya says:


    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,

    • Terry Miller says:

      Divya, if you’re on the “Create” page your only button options are Save/Cancel and we’re not in control at that point. You’d have to create and add the button to the “Detail” page. If the field values vary from person to person, then you’ll first need to query the object that holds the defaults and set those to variables. Then reference the variables when building out the new URL. See my post at https://www.hometeamconsulting.com/url-hacking-for-salesforce-automation/ to see how to build out the URL string. Hope that helps.

  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.

    • Terry Miller says:

      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 {
      catch(err) {

      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