You need to sign in to do that
Don't have an account?
Paul 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.
{!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.
"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).
Best regards
Alain
- Acknowledged_By and Completed_By are fields of what object?
- Case_Assignment__c is related to Case with what field?
- 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