function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

addError() to a dynamic referenced field

Im stuff trying to use the field.addError() method to post validation errors on my VF page.

The standard absolute method call works fine e.g.. customObj.fieldName.addError('myString');

BUT I need to use this with fields I dynamic reference on a generic SObject.

I've tried genericObj.get('fieldname').addError('myString'); but this validates to a method does not exists error


Can anyone help me, please?


Best Regards


Jerun JoseJerun Jose
obj.get('fieldName') will get you the value in 'fieldName' for the current object. It does not point to the field itself.

To use the addError, you will need to apply it on the field. Although I am not absolutely sure of how, I would start by using field tokens.

Thank you Jerun Jose.

That is of cause true. 

Does anyone know how to get a field token for a field from a SObject? Preferable by using the fields API name 


Hi Jerun Jose

Thanks again.

The code below shows how I tried to use the gotten SObjectField token, but this still don't give kind of field reference I need. The addError returns an no such method error. But I guess this also makes sense as the reference needs to be to a specific object instance to be able to show the error message at the right field on the page.


Map<String, Schema.SobjectField> fields  =obj.getSObjectType().getDescribe().fields.getMap();
fields.get(fieldNameString).addError('error text');

Any other suggestions?

 Best Ronni

Jerun JoseJerun Jose
Right. I should have thought of that.

Can you try something in line with Dynamic visualforce?

Basically, you can do AccountObj['AccountNumber'] in VF to get the account number for the account record denoted by AccountObj . Wondering if that can be extended for this purpose.

I don't think it is possible to add the error from the VF XML. Actually I also think that obj[fieldname] is just the same as the obj.get(fieldname) in APEX. It really puzzles me if this isn't possible with dynamic field references.

Srinivasa Chary TaduriSrinivasa Chary Taduri
Use acct.addError('Dynamic Field API Name', 'error in name field');