+ Start a Discussion
manubkkmanubkk 

Validation Rules not firing at once when inserting from Apex

I have two validation rules on a custom object, and I am trying to display all the failed validation rules at once in my visualforce page.

 

I am using <apex:pageMessages/> to render all the messages at once at the top of the page.

And I am catching the Exception in Apex as follows:

try { insert mycustomobject; } catch(Exception ex) { ApexPages.addMessages(ex); }

 

I entered the criteria in my fields which will cause both the validations to fail, but when I try to save only ONE validation error is displayed, when I fix the erro causing field and save again only then the second validation error is displayed. WHY is it so? when the docs clearly say,  "When one validation rule fails, Salesforce continues to check any additional validation rules on that field or any other field on the page and displays all appropriate error messages at once." 

https://login.salesforce.com/help/doc/en/fields_validation_considerations.htm

 

 

Starz26Starz26

if you are catching the error then I believe that although the rest gets validated, on the one error get captured. Someone will correct me if I am wrong here please....

 

This is not exactly what you are looking for but if you want to display the validation messages near the fields, here is a good article on jQuery: http://th3silverlining.com/2010/03/02/visualforce-form-validation-enhanced/

manubkkmanubkk

Thanks for the reply Starz26

I wondered about the same issue and found that it is possible for all errors to get captured in a single catch, like the following example taken from here

 

Account[] accts = new Account[]{new Account(billingcity = 'San Jose')};
try {
    insert accts;
} catch (System.DmlException e) {
    for (Integer i = 0; i < e.getNumDml(); i++) {
        // Process exception here  
    
        System.debug(e.getDmlMessage(i)); 
    }
}

 I tried the above with multiple validation fails, but still it returns Exception for one validation failure.

 

Also, the link you provided is for client side validation using Javascript, but I am wanting to do it server side, thanks for your help.

Starz26Starz26

It may not be pretty but I am curious if you can capture each DMLException and put it in a custom list, then display that custom list on the VF page. This would bypass the messages section of the page....

 

It appears the Error Messages are in a map and not a list, therefore you will have to create the list yourself.

manubkkmanubkk

Thats the problem, I am not able to get the list of all the exceptions. Even though DMLException has a method to get the list of all the errors generated by a dml statement, it returns the error of only one validation (when there are actually more validations which have failed).

 

The visualforce pageMessages tag just makes life easier to display custom error messages in a page, but its of no use if I cannot get all the error messages on apex in the first place.

 

On the other hand if I use the Salesforce application to save the object, it displays all the failed validations together!

 

Validation Rules is a great feature, but I am trying to avoid doing a custom implementation of a feature which already exists. I must be missing something?, Any Salesforce gurus out there who can help me with this problem?

Starz26Starz26

So you cannot do something lke this?

 

String[] lMSG = New String[]{};

try {
    insert accts;
} catch (System.DmlException e) {
    for (Integer i = 0; i < e.getNumDml(); i++) {
        // Process exception here  
        lMSG.add(e.getDmlMessage(i));
        System.debug(e.getDmlMessage(i)); 
    }
}

 

manubkkmanubkk

I tried that, as I said earlier, but it returns only one message, ie. the list size is 1.

 

To clarify, here are the steps to simulate:

1. Create a custom object ('MyCustomObject__c')

2. Create one custom field ('MyCustomField1__c' Text(50))

3. Create another custom field ('MyCustomField2__c' Text(50))

4. Create a validation rule on the first field (ISBLANK('MyCustomField1__c' )

5. Create a validation rule on the second field (ISBLANK('MyCustomField2__c' )

6. Now, in apex code initialize the 'MyCustomObject__c' leaving the 'MyCustomField1__c' and 'MyCustomField2__c' blank and insert the object surrounded by try/catch.

The DML insert statement should return 2 validation failures in the exception thrown, since both the fields are blank. But it only shows the first field validation error. However, this works in Salesforce Application, saving from the Salesforce UI, shows the errors for both the validations.

Vincent ScuorzoVincent Scuorzo
Did you ever find a solution to this? I'm having the same issue right now and can't seem to catch more than 1 exception despite having 2 validation rules failing simultaneously. There must be some way to access all of the errors.
Alvin Zhou 36Alvin Zhou 36
If you really want to show the real errors, there is one way by using the Database.update(),  Database.insert(). Then you can mark it to be partially succeed. The returning type is the SaveResult. Using the SaveResult, you can have access to the Error class. In there you can get all the clean error message there. In this way, you can throw an error message using your custom excetpion handler. 
By doing the above, it should work.