Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created November 9, 2016 23:08
Show Gist options
  • Save douglascayers/a709fe7bd293191f8361389015ca3dfb to your computer and use it in GitHub Desktop.
Save douglascayers/a709fe7bd293191f8361389015ca3dfb to your computer and use it in GitHub Desktop.
Salesforce custom JavaScript button example that updates record and displays errors.
{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
try {
var formToUpdate = new sforce.SObject( "Company_Form_Opp__c" );
formToUpdate.id = "{!Company_Form_Opp__c.Id}";
// ... snip ...
var result = sforce.connection.update( [ formToUpdate ] );
if ( result[0].getBoolean( "success" ) ) {
location.reload( true ); // refresh page
} else {
var errors = result[0].errors;
var errorMessages = "";
for ( var i = 0; i < errors.length; i++ ) {
errorMessages += errors[i].message + '\n';
}
alert( errorMessages ); // display all validation errors
}
} catch ( ex ) {
alert( ex ); // display any javascript exception message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment