DEV Community

Rachel Soderberg
Rachel Soderberg

Posted on • Edited on

Update Salesforce Fields With A Button Click (Salesforce Classic)

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.

Create A Button

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:

Standard Objects

  1. Click Setup and in the left toolbar find and expand Build > Customize > {Your Object}.
  2. Under {Your Object} click "Buttons, Links, and Actions".
  3. Click New Button or Link.

Custom Objects

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

From this point on, the steps should be the same for both Standard and Custom objects:

  1. Fill in the Label and the Name should auto-populate. Include a Description if desired.
  2. 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.
  3. "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.
  4. Select "OnClick JavaScript" for the Content Source.
  5. Save your new button.

Update a Field With JavaScript

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 am not well-versed in JS, so I apologize if any of my terminology is wrong and welcome any corrections!)

  1. If you saved and closed your button, open it back up from the Buttons, Links, and Actions section of your object and click Edit.
  2. The first required step is adding a reference to the toolkit at the top of the function Content Editor: {!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}.
  3. Instantiate a new SObject, passing in the name of your Object.
  4. Set your new SObject.id to your Object.Id field.
  5. 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.
  6. End the file with the following:result = sforce.connection.update([Your SObject]);
  7. Check Syntax to ensure your JavaScript is functionally correct, then Save.
  8. Add your new button to the Page Layout of your object, and click it to test!

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]);
Enter fullscreen mode Exit fullscreen mode

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 or LinkedIn and say hello!

Top comments (1)

Collapse
 
kritikgarg profile image
kritikgarg • Edited

๐Ÿ‘๐Ÿ‘ Rachel, this is a fantastic article! The use of JavaScript to update Salesforce fields is a game-changer for business processes, making it easy to update multiple records quickly and efficiently. It's a great time-saver, especially for repetitive tasks.

Along with this, I also visited the article Lightning Object Creator.It provides an efficient method of creating objects and fields in Salesforce using a spreadsheet or CSV file. It's a great tool for simplifying the data import process and reducing manual effort.๐Ÿ˜ƒ๐Ÿ‘