Has anyone found a way of adding a dynamic field level error to an sobject?
I would like to do something like
mySobjectRec.get('fieldname').addError('my error message');
I realise that mySobjectRec.fieldname__c.addError('my error message') works.
In this code, we have used the schema class to get the list of sObjects and fields in APEX. This list is later displayed with the help of Map and SelectOptions in the next set of code snippet. The schema class has the getGlobalDescribe() method which help us to get the schema of our entire salesforce org.
To create an sObject, you need to declare a variable and assign an sObject instance to it. The data type of the variable is the sObject type. The following example creates an sObject of type Account with the name Acme and assigns it to the acct variable.
You can use getGlobalDescribe which returns map of Map<String,SObjectType> SObjectName and related SObjectType. SObjectType currentType = Schema. getGlobalDescribe(). get('Account');
Unfortunately it's not possible to do it. 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With