function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Paul GintherPaul Ginther 

Field Update using Button and Java ScriptButton with Error Trap

I have two buttons that trigger an update on two fields each.  Acknowledged updates the Status to "Acknowledged" and the Completed updates the Status field field to "Completed".  Once the status is updated, a Workflow rule then runs to update the other fields (Acknowledged_By, Acknowledged_On, Completed_By and Completed_On). I am using Java in each button to perform this task.  Here is the code that manages the Completed update:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var ca = new sforce.SObject('Case_Assignment__c');
ca.id = "{!Case_Assignment__c.Id}";
ca.Status__c = "Completed";
result = sforce.connection.update([ca]);
location.reload(true);

I need to know if there is any way to create an error trap in this code to stop the update and throw an error if the user clicks this button before the Acknowledged_By and Acknowledge_On fields have been populated.  I tried managing this with a validation rule, but the validation runs before the Workflow rule so the criteria in the Validation never evaluates to TRUE (Acknowledged_By = '' and Completed_By <> '')  Any ideas would be greatly appreciated.
Alain CabonAlain Cabon
Hi Paul,

"I am using Javascript." ... otherwise you will surprise the world.

You have even written some Ajax code.

Here are the examples you are looking for:  https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_more_samples.htm

You want to "throw" errors (not to "catch" or trap them according to me).
//create an example account
var account = new sforce.SObject("Account");
account.Name = "my new account";
var result = sforce.connection.create([account]);

if (result[0].getBoolean("success")) {
  log("new account created with id " + result[0].id);
  account.Id = result[0].id;
} else {
  throw ("failed to create account " + result[0]);
}

//now delete the example account
var delResult = sforce.connection.deleteIds([account.Id]);
if (delResult[0].getBoolean("success")) {
  log("account with id " + result[0].id + " deleted");
} else {
  log("failed to delete account " + result[0]);
}

Best regards

Alain
Paul GintherPaul Ginther
Thank you Alain. I appreciate the information. I will endeavour to be more linguistically accurate when posing questions in the future 😀.
Alain CabonAlain Cabon
Hi Paul,
  1. Acknowledged_By and Completed_By are fields of what object?
  2. Case_Assignment__c is related to Case with what field?
  3. Your two buttons are on a Case layout?
The problem with a javascript control is that the error is just a javascript pop-up alert.
You cannot activate the standard error message of Salesforce with the javascript button.

Throwing an error in the javascript code (throw (ie:"failed to create account ")); is just useful for ... another javascript code (the caller for example).

If you want to show the standard error message on the layout of a case activated by a click on a button, you need to call a visualforce page (for the refresh and auto-run only) wich will create the Case_Assignment__c with its extension of standard Apex controller (only if the values Acknowledged_By and Completed_By of the linked Case are not empty) but it is not easy like a simple javascript code because of the indirection for writting the error message on the calling screen using the Apex controller.

https://developer.salesforce.com/forums/?id=906F00000008xbOIAQ

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

http://salesforce.stackexchange.com/questions/122214/refresh-page-after-onclick

http://salesforce.stackexchange.com/questions/72505/check-the-current-field-value-before-updating-in-custom-button

https://developer.salesforce.com/forums/?id=906F0000000kIp9IAE

We can try to write a solution from the previous links ( + the URL hacks afterwards ...)

Best regards
Alain