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

Update problem when a field becomes null

Hi !

I create an object and specify a particular Field.
For exemple I create an Opportunity, specifying the description field.
The object is well created and the description field is set to a value.

When I update this object, I can change the value of the field without any problem.
But if the new value is null (typicaly an empty Textbox), the field is not set to null on update,
containing the same value as before the update !!!

I tried this:
if (DescriptionBox.Text.Length == 0)
    MyOpportunity = null;

Doesn't work :(

Any idea about my problem ?
Thank you in advance.
see the docs for fieldsToNull.
Thank you SimonF !



Were you able to finally figure this out? i have a similar issue.

Please let me know.

thanks in advance.



It is absolutely not userfriendly, but quite simple:

Each time you want to erase a field, add the fieldname to the string[] table:

Opportunity Opp = new Opportunity();

Opp.FieldsToNull = new string[10]; // 10 or any integer...
Opp.FieldsToNull[0] = "Description";
Opp.FieldsToNull[1] = "CustomField__c";

Sforce.update(new sObject(){Opp});




thanks for the quick response.

ok. i am retrieving custom fields from contact object. And if my custom fields are empty its gives me following error.

nullable object must have a value.

I  am using following code to prevent the program from crashing. But its not working.

If contact.User_ID__c.Value = False Then

contact.User_ID__c = ""

contact.User_ID__cSpecified = True

cmd.Parameters.AddWithValue("@UserId", contact.User_ID__c)


cmd.Parameters.AddWithValue("@UserId", contact.User_ID__c)

contact.User_ID__cSpecified = True

End If

i would really appreciate any suggestion .



you MUST use the "fieldsToNull" field of your object :)

contact.fieldToNull = new string[1];
If contact.User_ID__c.Value = False Then
contact.fieldsToNull[0] = "User_ID__c";
End If

thank you.
Does it work ??

thanks for trying to help me again.

No but i did not work.

if my custom field is empty , ie userid (doble) is empty i am getting following error. nullable object must have a value.

how can i prevent my program from crashing if there is a empty field coming in from sales force.



Griffin WarburtGriffin Warburt
I'm trying to solve the same issue in VB.NET.

The field I am working with is account.NumberOfEmployees. My program takes the existing number of employees, increases it by 1 and updates the account. However, if the field is null "nullable object must have a value."

I want to use an If Then statement to determine if the field's value is null and set it to 1 if it is.

I haven't finished looking into it yet, but if anyone knows the answer immediately I'm sure future people will find it helpful.
You probably want to read up on the .NET Nullable<T> type, as that's the wrapper object that .NET 2.0 uses for scalar values in web service that can be nil.


I am having the same problem.  Have you got a soultion for it?  If you do, please tell me how you overcome it.

Thank you.




Griffin WarburtGriffin Warburt
I don't know if I ever solved this issue.  I was developing a demo and decided just to advise the demo audience that the program would throw an error if the field was empty.
Have you already tried these approaches?
if x is nothing then
if x = vbnullstring then