Skip to content

Instantly share code, notes, and snippets.

@walters954
Created August 24, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walters954/8e4168c123159d99b9df59e0f08ab9d1 to your computer and use it in GitHub Desktop.
Save walters954/8e4168c123159d99b9df59e0f08ab9d1 to your computer and use it in GitHub Desktop.
Salesforce custom button code to increase a number field by 1 which can be uses to fire off a process builder. Confirmation from the users and system admin users have been added.
//I am assuming this code allows for the JS SOQL calls
//Check the resources section for SF documentation
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
//Yes or no popup dialog box to confirm from the users
var answer = confirm("Are you sure you want to create a opportunity?")
//Check if system admin and yes was selected on modal box
if (answer && '{!$Profile.Name}'== 'System Administrator'){
//Select the current account records and put it into the record variable
var records = sforce.connection.query("SELECT Id, Process_Builder_Trigger__c FROM Account Where id = '{!Account.Id}'");
//The records object come with other garbage so we need to only pull out the account we are working with
//Pull the account object out of the records variable and set it to accountRec
var accountRec = records.getArray('records')[0];
//Increase accountRec Process_Builder_Trigger__c
//JS sees the number field as a string so this will make sure that is a number - Number(accountRec.Process_Builder_Trigger__c)
accountRec.Process_Builder_Trigger__c = Number(accountRec.Process_Builder_Trigger__c) + 1;
//Update accountRec object
sforce.connection.update([accountRec]);
//Reload the page
window.location.reload();
}
@kldeb
Copy link

kldeb commented Dec 18, 2017

It's a good idea to check the result before reloading the page.

      var result = sforce.connection.update([oppRec]);
      if (result[0].success === "true") {
        window.location.reload();
      } else {
        alert(
          "An Error has Occurred. Error: \r\n" +
          result[0].errors.message
        );
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment