fieldsToNull in Salesforce API Call

fieldsToNull in Salesforce API Call

While doing API Calls in Salesforce, in fieldsToNull, an Array of one or more field names can be passed for whose value you want to explicitly set to null.

When used with update() or upsert(), you can specify only those fields that you can update and that have the nillable property. When used with create(), you can specify only those fields that you can create and that have the nillable or the default on create property.

For example, if specifying an ID field or required field results in a runtime error, you can specify that field name in fieldsToNull. Similarly, if a picklist field has a default value and you want to set the value to null instead, specify the field in fieldsToNull.

To reset a field value to null, you add the field name to the fieldsToNull array in the sObject. You cannot set required fields (nillable is false) to null.

Sample Code:

sObject objContact = new sObject();  
objContact.type = "Contact";  
// Set the value of LastName to null  
objContact.fieldsToNull = new String[] { "Phone", "FirstName", "Email" }; 

Leave a Reply