Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am using validation with help of standard error messages on a field. Is there a workaround to manipulate the standard text displayed on error message. ???

share|improve this question
1  
If you're using a validation rule, you can just change the error message configured. To contextualise it, you can catch the exception in your controller and add a custom Apex PageMessage –  techtrekker May 15 '13 at 8:00
    
No, I am not using validation rule.I am doing this on Standard field "Reply Body". This field is required by default. So without any validation rule sales force throws error when it is submitted as empty. And I want to manipulate text content of this error. –  Jaimal May 15 '13 at 8:08
    
Check the field in your controller before your DML statement and add your own error message if blank. –  DavinC May 15 '13 at 8:40
    
Thanks, this was useful. On checking the field value in controller and displaying it on page with <messages /> tag, I have end up with 2 error messages. One is from controller and other is the standard. How can we remove the standard one??? –  Jaimal May 15 '13 at 9:35
    
Some javascript (jquery perhaps) to hide the standard error message? Think standard error messages have a peculiar class (pbError or such?) –  techtrekker May 15 '13 at 10:04
show 1 more comment

1 Answer

Removing the duplicates - I built a bit of infrastructure to do this. There may be simpler solutions

In a class called Util, three methods:

  public static void addVFPageMessage(ApexPages.Severity sev, String msg, Exception e) {  
if (!Util.isExceptionAlreadyInVFMsgContext(e))
    ApexPages.addMessage(new ApexPages.Message(sev,msg, 
                            (e != null && 
                             (e.getTypeName() == 'System.DmlException' ||
                              e.getTypeName() == 'MyException'
                             )
                                 ? Util.friendlifyException(e.getMessage()) 
                                 : Util.showException(e)) 
                      ));

}

//  ---------------------------------------
//  friendlifyException
//  ---------------------------------------
public static String friendlifyException(String errMsg) {

  String     res         = errMsg;
  String     sysErrPrefix  = 'FIELD_CUSTOM_VALIDATION_EXCEPTION,';
  Integer sysErrPrefixIndex  = errMsg.lastIndexOf(sysErrPrefix);
  if (sysErrPrefixIndex != -1)  // Strip out user unfriendly message prefix
      res = errMsg.subString(sysErrPrefixIndex + sysErrPrefix.length());
    String     myErrPrefix  = 'MyException';
  Integer myErrPrefixIndex  = errMsg.lastIndexOf(myErrPrefix);
  if (myErrPrefixIndex != -1)  // Strip out user unfriendly message prefix
      res = errMsg.subString(myErrPrefixIndex + myErrPrefix.length());  
  Integer stackTraceIndex    = res.indexOf('Class.');
  if (stackTraceIndex != -1)   // Strip out the stack trace at end of some messages
    res = res.subString(0,stackTraceIndex);
  return res;
}

  //  ------------------------------------------
  //  showException
  //  ------------------------------------------
public static String showException(Exception e) {
 return e != null ? e.getTypeName() + ' ' + e.getMessage() + ' line:' + e.getLineNumber() + '\n' + e.getStackTraceString() : '';  

}

and in a controller, I do the following:

   // ----------------------------------------------------------------------------------------
// EXTENDED METHOD  - save          : Save as VF page 
// ----------------------------------------------------------------------------------------
public PageReference  save() {

 PageReference resPg;
 try{
    update this.oppo;        
}
    catch(DmlException e){
      Util.addVFPageMessage(ApexPages.Severity.ERROR,'[OCE-03] Save failed, reason: ',e);
    }

 return resPg;

}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.