+ 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 Sandrine
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;