You need to sign in to do that
Don't have an account?
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
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/
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
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.
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.
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?
So you cannot do something lke this?
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.
By doing the above, it should work.