You need to sign in to do that
Don't have an account?
Caleb_Sidel
addError to a field on an SObject?
Has anyone successfully used addError on a field dynamically on an SObject?
Something like the below in concept (note the below doesn't work because get(FieldName) returns the value in that field, not the field itself...
Account a = [Select fields FROM Account WHERE stuff];
SObject obj = (SObject)a;
obj.get('FieldName__c').addError('Something or other');
Thank you,
Caleb
this would solve your issue.
obj.<field name>.addError('<error text>');
let me know if this helps you!
Baskaran
True, that works if I know the field name... but what if I wanted to something like
Map<String,String> fieldNameToError;
for(String fieldName : fieldNameToError.keySet)
obj.get(fieldName).addError(fieldNameToError.get(fieldName));
In the above case fieldName isn't known. It's a contrived example, in my actual use case I have two objects a parent and a child and they share some field names. I want if the child record errors on a field name that they share to have an error on the parent on the same field. Currently I'm doing something similar to
Map<String,String> fieldNameToError;
for(String fieldName : fieldNameToError.keySet)
if(fieldName.equalsIgnoreCase('Field1__c')) obj.Field1__c.addError(fieldNameToError.get(fieldName));
else if(fieldName.equalsIgnoreCase('Field2__c')) obj.Field2__c.addError(fieldNameToError.get(fieldName));
else if...
else obj.addError(fieldNameToError.get(fieldName));
But if someone adds a new field...code change :(
Thanks for any help :)
This is now available in Salesforce Winter ’21 Release, check the link (https://releasenotes.docs.salesforce.com/en-us/winter21/release-notes/rn_apex_trackErrors.htm) for detail
Thanks,
Meer Zaman