1

I have created a new date field and set the default value to 'today()'. The date value is being shown for all the new records that are created after the creation of this date field. But the old records do not hold a value for this date field. Is there a way that I can update the date value for all the old records?

  • I think it should be populated for all records please do a crosscheck because its a formula field. – Akum Nov 13 '16 at 18:50
4

You can use Developer Console's Execute Anonymous feature to accomplish that.
You will find it on Debug drop down list on Developer Console.

Query null values from desired object and update them.

Code Example for Account object:

List<Account> AccList = [select id,customDateField__c from Account where customDateField__c = null];
for(Account a:AccList){
   a.customDateField__c = system.today();
}

update AccList;
| improve this answer | |
  • I was writing the same thing. :) – Samir Nov 8 '16 at 11:42
4

There's no automatic way, but you have some options:

  1. Run the code on the Developer Console's Execute Anonymous window as @umithuckan mentioned (although instead of System.today(), you might want to use a.CreatedDate)

  2. Using Apex Data Loader or dataloader.io to export the accounts and then import them putting the CreatedDate column on you custom date field.

| improve this answer | |
2

You can write 1 time script(anonymous code block) that updates the date value for the records where that particular date field is blank.

For example:

List<Account> accListToUpdate = new List<Account>();
for(Account accObj: [Select Id,customdatefield__c from Account Where customdatefield__c = null]) {
     accObj.customdatefield__c= today's date or whatever date you want to set
     accListToUpdate.add(accObj);
}
if(!accListToUpdate.isEmpty()){
    update accListToUpdate;
}

If the records are more then you can go for a batch class. But will not recommend that as it is 1 time activity.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.