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
CRM_PeteCRM_Pete 

Need to reset a number field to zero on ALL contacts

I have a fairly complex Flow that updates a number field on all contact records, but first I need to zero out that field for ALL contacts.
Is there a bit of code I can run before running my flow?
I currently do it as a first step in my flow, but often enough I end up with lock errors.
I'm open to suggestions.
Thank you!
Best Answer chosen by CRM_Pete
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try the below code in devconsole to reset all contacts number field to zero.
List<contact> conlist = new List<contact>();

for(contact con :[Select id, Numberfield__c from contact where NumberField__c>0]){
con.Numberfield__c =0;

conlist.add(con);
}

update conlist;

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try the below code in devconsole to reset all contacts number field to zero.
List<contact> conlist = new List<contact>();

for(contact con :[Select id, Numberfield__c from contact where NumberField__c>0]){
con.Numberfield__c =0;

conlist.add(con);
}

update conlist;

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
CRM_PeteCRM_Pete
Thank you @Ankalah!
That's exactly what this amateur copy-paste developer needed!  :)
For anyone (not technical enough) finding this handy little gem, the unfiltered update will fail if a single record fails a validation rule, and probably any other restriction added after data was created.