+ Start a Discussion
MascotteMascotte 

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.
Matthias.
SuperfellSuperfell
see the docs for fieldsToNull.
MascotteMascotte
Thank you SimonF !


onCollectonCollect

Hi,

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

Please let me know.

thanks in advance.

 

MascotteMascotte
Yes.

It is absolutely not userfriendly, but quite simple:

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

Code:
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});

 

onCollectonCollect

Hi,

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)

Else

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

contact.User_ID__cSpecified = True

End If

i would really appreciate any suggestion .

thanks

 

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

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


onCollectonCollect
thank you.
MascotteMascotte
Does it work ??
onCollectonCollect

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.

 

thanks

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.
SuperfellSuperfell
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.
see http://msdn2.microsoft.com/en-us/library/b3h38hb0.aspx
JulsJuls

Hi

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.

 

Brgrds

Js

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