How to populate data in a new field, from existing records

I have a text field ‘C’ which is updated using a workflow. The field concatenates values of two fields, say ‘A’ and ‘B’, to prevent any duplicates. How can I populate this field ‘C’ with existing records that I already have.

Answer

I dont really think you need a trigger here as you already admit that you have a workflow that will perform this action moving forward. If you didnt already have a workflow in place, then yes, a trigger would be the way to go, but since this is just a one time update, you can simply run some code in execute anonymous. If you have a full sandbox, you can test in there prior to production to ensure it has the desired outcome. If this becomes something you want to do regularly, you can easily make this a batch and/or scheduled job.

Something like this should work

list<your_object__c> myList = [Select Id, Name, A, B, C From your_object__c Where C = null || C = ''];
for(your_object__c o : myList){
     o.C = o.A + ' ' + o.B;
}
update myList;

Attribution
Source : Link , Question Author : Subham , Answer Author : Chris Duncombe

Leave a Comment