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
Marty DorrenMarty Dorren 

how to use fieldsToNull in for loop

Hi,
I need to update a field (not required) to null but I'm getting an invalid field error from the debugger when trying to save:

List<monthly_summary__c> MSUp = [Select Id, Billing__c from monthly_summary__c where Id IN : ListMSDelId];

                for(monthly_summary__c MSUp2 : MSUp) {
                    MSUp2.setFieldsToNull = new string[] { 'billing__c' };
//                    MSUp2.Billing__c = null;
                }
update MSUp;

Any suggestions?  I coundn't find much in the way of sample code to do this.
Thanks
Anoop yadavAnoop yadav
Hi,

Is "setFieldsToNull" a field in your object, then use API Name i.e. __c.
Marty DorrenMarty Dorren
According to:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_concepts_core_data_objects.htm#i1421117

It's an array inside the sobject
Abhishek PokharnaAbhishek Pokharna
Hi Marty,

I think, fieldsToNull is available only in saleforce API (ie, while using salesforce webservice from other platform)
Marty DorrenMarty Dorren
hmmmm... Perhaps barking up the wrong tree...

Simply put, how do I update that field with a null value? <ARGH!>
Abhishek PokharnaAbhishek Pokharna
Simply set the field to null

MSUp2.billing__c = null;
Marty DorrenMarty Dorren
Sadly,
debugging shows that the null function works after I issue it in my loop.  The problem seems to be that the update function will NOT replace with a null value:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CreateBilling caused an unexpected exception, contact your administrator: CreateBilling: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id a1yM0000000WOtjIAG; first error: DELETE_FAILED, Your attempt to delete B-001604 could not be completed because it is associated with the following monthly summaries.: MS-003273 : []: Trigger.CreateBilling: line 278, column 1

Even though null works, update() does not. ARGH 2x
Abhishek PokharnaAbhishek Pokharna
probably, in the trigger you are trying to delete an oject that is master to some other object. Either set cascading deletion ON or delete the children first.
Marty DorrenMarty Dorren
Thanks... don't know why but the error seems intermittent.   I'll check for some rhyme or reason.  In the meanwhile, many thanks to all for your help.
Marty