5

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.

2 Answers 2

7

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.

-1

how about

mySobjectRec.getSObject('fieldname').addError('error message');

What I am looking at is:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm

I haven't tried it so not sure if it will work, let me know.

1
  • 1
    Thanks for the reply, but it looks like the getSObject gets the related SObject and errors with "Invalid relationship" as my field is not a lookup or master/detail.
    – Jeff
    Commented Dec 9, 2012 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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