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

Changing record type for many records based on related to field


I have exsisting Account and opportunities records.
They have Tasks related to it.

Task have record type TA, TB, TC

I want to replace all the TA related to Accounts with TB
and I want to replace all the TA related to opportunites with TC.
I am planning to run a code in Anonymous mode in Production, 

Thank you for advise.
Best Answer chosen by Ab
David ZhuDavid Zhu
This is the sample of changing Account record type. Opportunity is similar.

 id recordTypeIdTA = Schema.SObjectType.Account.getRecordTypeInfosByName().get('TA').getRecordTypeId();
 id recordTypeIdTB = Schema.SObjectType.Account.getRecordTypeInfosByName().get('TB').getRecordTypeId();

list<account> Accs = [select id,RecordTypeid from account where recordtypeid = :recordTypeIdTA ];  //may subject to soql governor limit
foreach(Account a : Accs)
      a.recordtypeid = recordTypeIdTB;
update Accs;