Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I'm having problems with one of our custom validation rules that forces me to have 'blank' value in a custom field based on certain conditions.

When editing the field manually through the interface, I just erase the content of the text box and it saves fine. But with apex code, how can I put the 'blank' value inside the field? Assigning null to the field does not seem to make it work. I checked the developer console for the logs and null is correctly being assigned to my property, but the update still throws an exception with a message like :

FIELD_CUSTOM_VALIDATION_EXCEPTION, CustomField; must be blank: [CustomField__c]

Here is how I assign null in the apex code :

customObject.CustomField__c = null;

update customObject;

Assigning zero does not work either, as the validation rule needs the field to be 'blank'. Is there a hidden trick which allows me to do this through apex?

Here is the validation formula. If it evaluates to true, there is an error :
AND( ISPICKVAL(CustomStatusField ,'Custom Value') , !ISBLANK(CustomField__c )))
So the validation basically is: When CustomStatusField is equal to 'Custom Value'CustomField__c must be blank.

1 Answer

0 votes
by (32.1k points)
edited by

Setting the field to null in apex code should work. There might be another trigger which tried to set another value. Which is causing the problem.

To learn in-depth about Workflow in Salesforce, sign up for an industry-based Salesforce Course!

Browse Categories