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

I am having problems with one of our validation rules, which forces me to have an "empty" value in a custom field based on certain conditions.

When editing a field manually through the interface, I simply delete the contents of the text field, and it saves a fine. But with apex code, how can I put an "empty" value inside a field? Assigning a null field does not work. I checked the development console for logs and null is correctly 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] 

This is how I assign null in vertex code:

 customObject.CustomField__c = null; update customObject; 

Assigning zero does not work either, since the validation rule requires the field to be "empty". Is there a hidden trick that allows me to do this through the top?

EDIT: Here is a validation formula. If it is true, an error occurs:

 AND( ISPICKVAL(CustomStatusField ,'Custom Value') , !ISBLANK(CustomField__c ))) 

So the validation is basically this: when CustomStatusField is equal to 'Custom Value' , CustomField__c should be empty.

EDIT No. 2: I finally solved my problem. The above code worked as expected, but another vile trigger appeared that caused the field to be updated to a value other than zero, which resulted in a denial of the validation rule. God, I hate the widespread use of triggers for business logic!

+4
source share
1 answer

I just tried this and setting the field to null for apex code works for me. Is it possible that the error message comes from another validation rule? (Someone might have cut and pasted the wrong error message when they created a validation rule ...) Or maybe a workflow or trigger is working and trying to set the field to a value other than zero?

+6
source

Source: https://habr.com/ru/post/1401196/


All Articles