+ Start a Discussion
zachzach 

Not getting an error when updating non-nillable to null...

Hi, I've built a page that updates the lead object & when I try to update the Company or LastName fields with null, it doesn't throw an error in the saveResult... It doesn't update to null & the data stays the same, but it's not throwing an error.  Just wondering if this is a known issue and what I should do, otherwise, if it is.

Thanks,
-Zach

Here's a code snippet if you need it:

Code:
function saveLead() {
  
 var updateArray = new Array();
 var _value = leadSql[0];
 _value.set('Company',document.getElementById("editCompany").value);
 _value.set('Street',document.getElementById("editStreet").value);
 _value.set('City',document.getElementById("editCity").value);
 _value.set('State',document.getElementById("editState").value);
 _value.set('PostalCode',document.getElementById("editZip").value);
 _value.set('Country',document.getElementById("editCountry").value);
 _value.set('DUNS_Number__c',document.getElementById("editDUNS").value);
 _value.set('Location_Type__c',document.getElementById("editLocationType").value);
 _value.set('Servicing_Division__c',document.getElementById("editServicingDivision").value);
 _value.set('LeadSource',document.getElementById("editLeadSource").value);
 _value.set('Salutation',document.getElementById("editSalutation").value);
 _value.set('FirstName',document.getElementById("editFirstName").value);
 _value.set('LastName',document.getElementById("editLastName").value);
 _value.set('Phone',document.getElementById("editPhone").value);
 _value.set('Fax',document.getElementById("editFax").value);
 _value.set('Lead_Group__c',document.getElementById("editLeadGroup").value);
 _value.set('Title',document.getElementById("editTitle").value);
 _value.set('Email',document.getElementById("editEmail").value);
 _value.set('Status',document.getElementById("editLeadStatus").value);
 _value.set('Call_Wrap_Code__c',document.getElementById("editCallWrap").value);
 _value.set('Invalid_Prospect_Reason_Code__c',document.getElementById("invalidProspectReason").value);
 _value.set('Do_Not_Contact__c',document.getElementById("editDoNotContact").value);
 _value.set('Description',document.getElementById("editDescription").value);
 updateArray.push(_value);

 var updateVal = sforceClient.Update(updateArray);
 //alert(updateVal);
 if(updateVal[0].success == true) {
  alert("Your changes have been saved");
 } else {
  alert("There was a problem with the data you entered.  Please edit and try again.");
 }
}

 

SuperfellSuperfell
use the fieldsToNull array.
zachzach
I'm not sure what you mean by that...

I'm not actually trying to set it to null.  All I want to do is be able to catch errors when fields don't update and if someone tries to set a non-nillable field to null, I want to catch that error and let them know.  If I try to set a non-nillable filed to null right now, the saveResult update comes back as successful, but the data doesn't change.  I guess I could just validate before submission & skip it altogether.
SuperfellSuperfell
Because you can't set a field to null by setting it to null, you have to explicitly use the fieldsToNull array to set a field value to null. If you do this, then the save will return a failure for that row if you try and set a non-nullable field to null. See the docs on fieldsToNull.
Ron HessRon Hess
snippet for your review, note the variable bean is a Dynabean for a custom object with a field Description__c

Code:
var elementid = "Description"+lkid;
var desc = document.forms['editPage'].elements[elementid].value;
if ( desc != null ) { 
  bean.set("Description__c",desc); 
} else { 
  bean.fieldsToNull.push('Description__c');
}
bean.save();

 

zachzach
Awesome, thanks guys.