Update Salesforce Fields With A Button Click (Salesforce Classic)
Tags: salesforce, button
Published: 2019-09-23 20:07:56

---
title: Update Salesforce Fields With A Button Click (Salesforce Classic)
published: true
description: Salesforce fields can be updated with a simple click of a button! I walk the reader through the steps of implementing a button-click field update.
tags: Salesforce, Button
series: Salesforce SOAP API Services
---

<i>Before I begin, I would like to note that soon the information in this post will be outdated. Salesforce is in the process of phasing out Salesforce Classic and moving everyone to Lightning Experience, and with this they are phasing out JavaScript buttons and actions. I plan to release a follow-up post that goes into the process of rebuilding your JavaScript buttons using the Lightning Custom Components, their new preferred method of performing the same tasks, as I go through the process of updating them in my company's org.</i>

<h1>Create A Button</h1>
Salesforce has two different types of objects - Standard and Custom. Standard objects come with Salesforce straight out of the box while Custom objects are created by an org's administrator. Below are the steps to create a new button for both types of objects:

<b>Standard Objects</b>
<ol>
<li>Click Setup and in the left toolbar find and expand Build > Customize > {Your Object}.</li>
<li>Under {Your Object} click "Buttons, Links, and Actions".</li>
<li>Click New Button or Link.</li>
</ol>

<b>Custom Objects</b>
<ol>
<li>Click Setup and in the left toolbar find and expand Build > Create > Objects.</li>
<li>Find and click {Your Object} in the list of Custom objects.</li>
<li>Scroll down to the Buttons, Links, and Actions section and click New Button or Link.</li>
</ol>

From this point on, the steps should be the same for both Standard and Custom objects:
<ol>
<li>Fill in the Label and the Name should auto-populate. Include a Description if desired.</li>
<li>Choose your desired display type. This may be a link within a Custom field, a button on your Object Detail page, or a button that will show up on a Related List.</li>
<li>"Execute JavaScript" is your selection for the behavior, which will trigger a warning for compatibility issues with Lightning Experience. We are going to ignore this for now, and update later when we switch to Lightning.</li>
<li>Select "OnClick JavaScript" for the Content Source.</li>
<li>Save your new button.</li>
</ol>

<h1>Update a Field With JavaScript</h1>
You've got a new button all set up and ready to go - now all that's left is adding the functionality to update a field. If you are familiar with JavaScript this will be a breeze, but you'll still be okay if not! <i>(I am not well-versed in JS, so I apologize if any of my terminology is wrong and welcome any corrections!)</i>

<ol>
<li>If you saved and closed your button, open it back up from the Buttons, Links, and Actions section of your object and click Edit.</li>
<li>The first required step is adding a reference to the toolkit at the top of the function Content Editor:
<i>{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}</i>.</li>
<li>Instantiate a new SObject, passing in the name of your Object.</li>
<li>Set your new SObject.id to your Object.Id field.</li>
<li>Update the SObject.FieldName of each field you want to make a modification to. You can use the Field Type and Insert Field dropdowns to find specific fields from your objects.</li>
<li>End the file with the following:</i>result = sforce.connection.update([Your SObject]);</i></li>
<li>Check Syntax to ensure your JavaScript is functionally correct, then Save.</li>
<li>Add your new button to the Page Layout of your object, and click it to test!</li>
</ol>

The above steps may have been confusing on their own, so here is a sample from one of the buttons on my org's Custom RMA object:

      {!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
           
      var rma = new sforce.SObject("RMA__c");
      rma.id = "{!RMA__c.Id}";
      rma.Printed__c = new Date().toISOString();
      rma.Printed_By__c = "{!User.Name}";
      
      result = sforce.connection.update([rma]);


I hope this brief introduction to updating Salesforce fields on button click has inspired you to add some extra functionality to your Salesforce workflows. Keep an eye open for my Lightning Experience buttons update in the coming weeks!

---

If you'd like to catch up with me on social media, come find me over on [Twitter](https://twitter.com/RachSoderberg) or [LinkedIn](https://www.linkedin.com/in/rachelsoderberg/) and say hello!