6

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. ???

6
  • 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 May 15, 2013 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, 2013 at 8:08
  • Check the field in your controller before your DML statement and add your own error message if blank. May 15, 2013 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, 2013 at 9:35
  • Some javascript (jquery perhaps) to hide the standard error message? Think standard error messages have a peculiar class (pbError or such?) May 15, 2013 at 10:04

1 Answer 1

2

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;

}

You must log in to answer this question.

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