After writing the post about creating records with a button, it made sense to extend that thought with manipulating record with a 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 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. 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

    1. 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 http://hometeamconsulting.com/url-hacking-for-salesforce-automation/ to see how to build out the URL string. Hope that helps.

  2. 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. 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.

    1. 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. 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

Your email address will not be published.

*