How to assign 'blank' to a Decimal field in Salesforce

marco-fiset picture marco-fiset · Mar 13, 2012 · Viewed 9.5k times · Source

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 developper 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 ?

EDIT : 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.

EDIT #2 : I finally solved my problem. The above code worked as expected but there was another sneaky trigger that caused the field to be updated to a non-null value, thus making the validation rule fail. God I hate extensive use of triggers for business logic !

Answer

naomi picture naomi · Mar 13, 2012

I have just tried this, and setting the field to null in apex code works for me. Is it possible that the error message is coming from a different validation rule? (Someone might have cut and pasted the wrong error message in when they created the validation rule ... ) Or maybe a workflow or trigger is running and trying to set the field to a non-null value?