2

Is there a way to send (if any) the Validation Error text to such a button, shown below? I would put it at the "Error" text at the end, if possible. I cannot find a reference to this in my searches.

{!REQUIRESCRIPT ("/soap/ajax/13.0/connection.js")} 
/*                                                                                       THREE REFERENCE CHANGES if copying to new button     */
var formid = "{!Company_Form_Opp__c.Id}"; 
var userid = "{!$User.Id}"; 
var FormToUpdate = new sforce.SObject("Company_Form_Opp__c"); 

var padyear = {!YEAR(TODAY())}; 
var padmonth = {!MONTH(TODAY())}; 
if( padmonth.toString().length == 1 ) { padmonth = '0'+padmonth } 
var padday = {!DAY(TODAY())}; 
if( padday.toString().length == 1 ) { padday = '0'+padday } 

if(        "{!Company_Form_Opp__c.BILLCHG_Accounting_Signature__c }"       != '' ) { 
alert("Already signed!"); 

} else { 

FormToUpdate.set("Id", formid); 
FormToUpdate.set(             "BILLCHG_Accounting_Signature__c"      , userid); 
FormToUpdate.set(             "BILLCHG_Accounting_Signature_Date__c"       , padyear + '-' + padmonth + '-'+ padday ); 

var Result = sforce.connection.update([FormToUpdate]); 

if(Result[0].getBoolean("success")) { 
function redirect() { parent.frames.location.replace("/{!Company_Form_Opp__c.Id}"); } 
redirect(); 
} else { 
alert("Error"); 
} 

}
4
  • You could proxy the update to a web service method in apex class which wraps the update in a try catch and returns the exception owing to any validations as a string error message. Commented Apr 18, 2013 at 19:46
  • 1
    So, it currently sounds like there is no "replace the 'error' reference with abcde" answer. Urgh. I'm going to sit on this a couple days and pray. Seems like 80-90% of what I WANT to do in SF requires me to be 10x the "developer" (hah) that I currently am. :-/
    – AMM
    Commented Apr 18, 2013 at 20:41
  • Can you say in business language what you're trying to accomplish here? My initial thought is that Visualforce/Apex might be better suited to your scenario.
    – greenstork
    Commented Apr 18, 2013 at 20:56
  • If they click the button, and a Validation Error is returned, I'd simply like to show the text returned by the Validation Error.
    – AMM
    Commented Apr 18, 2013 at 21:02

1 Answer 1

6

The errors are populated on the result object returned by the sforce.connection.update method. http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_results.htm

I couldn't find any details about the structure of the result object, I just used my browser's developer console to examine the properties to discover the error messages.

Below is an example that displays to user via javascript alert any error messages set via triggers, apex code, or validation rules from a call to sforce.connection.update:

{!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

}
3
  • Holy heck... I cannot wait to take this for a spin and see if it works out. Thanks so much!
    – AMM
    Commented May 30, 2013 at 14:17
  • I think it is the same as (or is close to at least) the API access. Here's the update() docs which specifies that a SaveResult array is returned which looks like the same structure you observed. Commented Jul 16, 2013 at 14:49
  • I tried that code and although the data structure seems to be correct, "errors[i].message" was undefined for me. "errors.message" returned the error message, though.
    – georg w.
    Commented Apr 23, 2014 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .