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
D0TD0T 

Default value for a field type check box not working

I've just deployed the Birthday reminder code and modified it based on our needs. I've tested the APEX code and everything works fine - however we have 9000 clients and would like the default value for the checkbox to be "checked". I've editted the custom field, however the value on the client page still remains unchecked, am I doing something wrong?

 

Thanks

A.Y

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

I think some records are failing due to data validation, update method behaviour is all or none so if any record is failing validation it will stop all. SO you need to use DataBase.update it will let valid records to be updated.

 

List<YourObject__c> listObj = new List<YourObject__c>();

for(YourObject__c obj : [Select id from YourObject__c where CheckBoxFieldAPIName__c = false Limit 1000])
{
obj.CheckBoxFieldAPIName__c = true;
listObj.add(obj);

}

Database.SaveResult[] lsr = Database.update(listObj, false);

 

All Answers

Shashikant SharmaShashikant Sharma

Is this happening with new records as well or only with existing records

 

D0TD0T

I didnt check new, but existing not being updated. 

D0TD0T

New cleints - works fine.

Shashikant SharmaShashikant Sharma

Thats what I wanted to know , solution is you need to run a script for old clients where you have to update this field value. You can update them using dataloader as well. 

D0TD0T

How can I do it with Data loader or a Script?

 

Thanks,

A.Y

Shashikant SharmaShashikant Sharma

Run this is debug log , Use API Name with name space

List<YourObject__c> listObj = new List<YourObject__c>();

for(YourObject__c obj : [Select id from YourObject__c])
{
obj.CheckBoxFieldAPIName__c = true;
listObj .add(obj);

}

update listObj;

 

 

D0TD0T

Thank you,

 

Object is contact and API name is Send_Birthday_Email__c, I tried replaceing it but getting an error - Error on column 1

Shashikant SharmaShashikant Sharma

use this

 

List<Contact> listObj = new List<Contact>();

for(Contact obj : [Select id from Contact where Send_Birthday_Email__c = false])
{
obj.Send_Birthday_Email__c = true;
listObj.add(obj);
}

update listObj;

 If you have name space then change this

obj.Send_Birthday_Email__c = true;

to

obj.NameSpaceName__Send_Birthday_Email__c = true;

D0TD0T

Still getting this error.

 

Error: Compile Error: unexpected token: 'List.' at line 1 column 0

Shashikant SharmaShashikant Sharma

are you copying it to system logs and executing it.

D0TD0T

Log Status : Too Many DML ROW 1001

 

any idea?

Shashikant SharmaShashikant Sharma

You need to run this for 1000 records in one time

 

List<YourObject__c> listObj = new List<YourObject__c>();

for(YourObject__c obj : [Select id from YourObject__c where CheckBoxFieldAPIName__c = false Limit 1000])
{
obj.CheckBoxFieldAPIName__c = true;
listObj.add(obj);

}

update listObj;

 As you were saying you have 9000 records , so you need to run it 9 times.

Shashikant SharmaShashikant Sharma

I think some records are failing due to data validation, update method behaviour is all or none so if any record is failing validation it will stop all. SO you need to use DataBase.update it will let valid records to be updated.

 

List<YourObject__c> listObj = new List<YourObject__c>();

for(YourObject__c obj : [Select id from YourObject__c where CheckBoxFieldAPIName__c = false Limit 1000])
{
obj.CheckBoxFieldAPIName__c = true;
listObj.add(obj);

}

Database.SaveResult[] lsr = Database.update(listObj, false);

 

This was selected as the best answer
D0TD0T

This appears to be working. I will report back if there are any hiccups. As of now so far so good.

 

Thank you for your help.

A.Y

Shashikant SharmaShashikant Sharma

Surely you can always ask but I would request you to mark it as  Accepted Solution so that others could also get benifitted from it.