Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apex AddError to SObject field

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.

like image 201
Jeff Avatar asked Dec 09 '12 04:12

Jeff


People also ask

How do I get sObject field in Apex?

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.

How do I assign a sObject in Apex?

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.

How do I get a sObject name?

You can use getGlobalDescribe which returns map of Map<String,SObjectType> SObjectName and related SObjectType. SObjectType currentType = Schema. getGlobalDescribe(). get('Account');


1 Answers

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.

like image 103
Egor Volk Avatar answered Sep 27 '22 23:09

Egor Volk