0

I have a text field named TestId in the object test which is capturing the Id of the record.Actually I have used an automation such as flow to update the value in the text field. Since I cannot make formula fields searchable I had to convert it to a text field. Working for newly created objects but I have to update the earlier created records with this value using anonymous code: How do I do this? Almost 20,000 records to be updated.

List <Test__c> test=testList=[Select Id, Name, TestId From Test__c];
for(test:testList){

}
10
  • If that is formula field then it should work for existing records as well. Commented Sep 1, 2022 at 7:29
  • @SaiPraveenKakkirala There is a correction. I had to make the field searchable So I had to add another text field which captures the value from a record triggered flow on test record creation. Right now the value is available for all the newly created records.. Have to update in the eariler scenarios,
    – JK123
    Commented Sep 1, 2022 at 7:35
  • 1
    You can do some dummy update from anonomous block as below List <Account> test=[Select Id, Name From Account]; List<Account> accounttoupdate= new List<Account>(); for(Account t:test){ accounttoupdate.add(t); } update accounttoupdate; Commented Sep 1, 2022 at 7:44
  • 1
    Just replace the Account with your object Commented Sep 1, 2022 at 7:44
  • 1
    Then it will not work. Till you perform this action put the condition for both create and update and later you can change it for create as if we update the flow will not trigger as you put it only for creation. Commented Sep 1, 2022 at 8:26

2 Answers 2

1

Like I said in my comments you can use dataloader and export the data and perform update operation with out any change so the flow gets invoked and the field value gets calculated.

If you want to execute in anonomous block you can perform dummy update as below.

List <Account>acclist=[Select Id, Name From Account];
List<Account> accounttoupdate= new List<Account>();
for(Account t:acclist){   
accounttoupdate.add(t);
}
update accounttoupdate;

In both the process you are just doing dummy update for the records only so that the flow gets invoked and value gets calculated

1

You can work with Lists

for (List<Test__c>testList : [Select Id, Name, TestId From Test__c WHERE TestId = NULL LIMIT 9000]) {
    for(Test__c test : testList){
        // update code here
    }

    update testList;
}

https://developer.salesforce.com/docs/atlas.en-us.234.0.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm

Thanks

2
  • 1
    You might want to add that that will fail because of 20.000 records to update. So will need to be split up in multiple runs (including a mechanism to check that you don't select the same record twice). Commented Sep 1, 2022 at 7:55
  • 1
    Hi Guy, Yes, you are right. Tried to give overall idea. DML limit 10k per transaction. code should be [SELECT ... FROM Test__c WHERE TestId != NULL LIMIT 9000]. something like this
    – Pavlo M
    Commented Sep 1, 2022 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .