4

Question

Is there any way to determine which validation-rule caused an error on a POST to REST API?

Background

I have a customObject A__c which has many Validation Rules. Also, I'm trying to insert/update values by REST API.

As expected, validation rules work fine after posting invalid data, returning a json object like this:

[
{
"fields":[
],
"message":"Name should start with XXXX",
"errorCode":"FIELD_CUSTOM_VALIDATION_EXCEPTION"
}
] 

I need to know which validation rule exception was thrown. In addition, I can't use the message attribute for this because the app is translated into many languages.

I looked at the official errors doc, but there was nothing there.

Any idea?

Update:

I'm developing a mobile app, this app has a form to complete and post the object A__c. I need to know which fields have error when I post in order to highlight them and show the error message on each invalid field. The object validation rules are created and updated in salesforce,so I'm not able to validate in client-side.

  • Can you create your own response and attach the validation rules 'ID' to the response, something like jsonResponse = '{"response": {"validation_Id": "validation_ID", "message": "validation error"}}'; res.responseBody = blob.valueOf(jsonResponse); – Rao Jun 21 '13 at 18:53
  • I think that I can not modify REST API response – Martin Borthiry Jun 21 '13 at 19:30
  • How about modifying the validation rule error messages to give each a unique ID? This would be visible in the regular interface as well, though. – Daniel Hoechst Jun 21 '13 at 19:50
  • @MartinBorthiry Is this because your app will not support (or) why can you not modify the response? I am sorry if I am missing something obvious just clarifying – Rao Jun 21 '13 at 20:07
  • 1
    Hmmm, this seems like a problem that might not have a solution, would you mind sharing why you need to know which validation rule it was? Perhaps there is another way to solve your problem – Daniel Blackhall Jun 22 '13 at 2:13
3

The response will return the API Name of the field in the "fields" array of the response. But only if the Error Location of the Validation Rule is configured correctly.

The Field needs to be selected from the picklist when creating (or modifying) the validation rule. This is shown in the screenshot below

Validation rule on a Field

When I violate the example validation rule above, my json response is:

[ {
    "fields" : [ "BillingCity" ],
    "message" : "This Billing City is a bad choice, please select another.",
    "errorCode" : "FIELD_CUSTOM_VALIDATION_EXCEPTION"
} ]

So as long as the Error Location on each Validation Rule is specified as 'Field', you will be able to place the error message next the right field in your mobile app.

| improve this answer | |
  • Thx Daniel for your reply. Excellent approach. This detail resolves the half of my problem. now, I only need to identify which rule was violated. – Martin Borthiry Jun 24 '13 at 12:03
  • You're welcome @Martin. I'd still like to help with this, but I can't understand why you need to know which rule it was? Isn't knowing the field sufficient – Daniel Blackhall Jun 24 '13 at 21:18
1

I would generally recommend the use of Apex REST API rather than the standard web service API's when doing external integrations. That way you can avoid a lot of the errors by doing things like satisfying the validation rules with with default values.

You can also trap and return your own errors from server side.

| improve this answer | |
  • Thanks Steven for your suggestion, But I've already have a complete system working on REST. I think it will be over budget change the whole app to SOAP. – Martin Borthiry Jun 23 '13 at 16:46
  • I think Steven was suggesting exposing your own apex class as a webservice.. Then you can try a database.insert and you can look at the results of that and interpret any validation failures into a rest response which your front end can interpret – Doug B Jun 23 '13 at 20:21
  • I've updated my post with a link to the Apex REST api documentation. – Steven Herod Jun 24 '13 at 6:49
0

The easiest way to find out which validation rule has been violated is to get this information from message (of course I noticed that you are not going to use this field because of translations). You can use simple convention in validation rule messages and translations i.e.

Amount must be greater than 10 [Code=VR10]

On REST client side you expect error pattern therefore you can get it and perform suitable action for that error code.

| improve this answer | |
  • Thank you for your reply. That is my current approach. But, It doesn't work since validation rules are dynamically created after mobile app was installed. – Martin Borthiry Jun 22 '13 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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