Create checkbox filled with default value true

custom-fielddefault-value

So obviously I can create a new checkbox field that defaults to true – but is there a way to actually "insert" a field's default value retroactively into all old records on creation of that field? Or is there no way around updating all the old records manually?

The reason behind this is that records often hold implicit values and over time, as new possibilities get added, a field gets added to reflect that. For example you would create "Is Active" where all old records have always been active and now you suddenly can change this – so a checkbox is introduced (but this is true for every other type of field as well).

SQL actually does this if you make a field NOT NULL but in Salesforce this does not seem to work (and you cannot make checkbox fields mandatory anyway as NULL is not allowed there). Any ideas on this one?

Best Answer

Salesforce does not have a OOTB solution for this. Your best bet would be to run a script like below in the Anonymous window and update the new field en masse.

List<sobj> objList = [SELECT Id, new_field__c FROM sobj LIMIT 10000];
List<sobj> objListoUpdate = new list<sobj>();
for(sobj rec: objList) {
    rec.new_field__c = true; //or false
    objListoUpdate.add(rec);

update objListoUpdate;
Related Topic