Adding an error on a dynamic referenced field

Is there a way to add an error on a dynamically referenced field? Let’s take this simple example:

opp.get('Name').addError('Invalid name');

This is not working, I’m looking for a similar minded way.



Sadly I have to report the answer to your question is no. For me this is the final peace of Dynamic Apex to complete the ability to build truly 100% dynamic Apex libraries and solutions.

I’ve searched for workarounds and can find none, the addError methods on SObject use a highly specialised way to reference the field which is to hard code the field reference ahead of the method. Here is the Idea Exchange idea to upvote!

This method is highly specialized because the field identifier is not actually the invoking object—the sObject record is the invoker. The field is simply used to identify the field that should be used to display the error.

Source : Link , Question Author : jpmonette , Answer Author : Andrew Fawcett

Leave a Comment