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
LuvaraLuvara 

Update Record Type based on Parent's Record Type

I'm trying to update the record type of my contacts based on the change of the record type on the account. I figured out how to have it update the contacts on an account record type change - but only to one fixed record record type. I realize it's not best practice to hard code ID's, but I'll tackle that part if i figure this out - and it's even possible...

 

Here's what I have so far.. 

 

 

trigger UpdateContactsOnRecordTypeChange on Account (before update) { Map<Id, Account> acctsWithNewRecordType = new Map<Id, Account>(); for (Integer i = 0; i < Trigger.new.size(); i++) { if ( (Trigger.old[i].RecordTypeId != Trigger.new[i].RecordTypeId)) { acctsWithNewRecordType.put(Trigger.old[i].id, Trigger.new[i]); } } List<Contact> updatedContacts = new List<Contact>(); for (Contact c : [SELECT id, RecordTypeId, accountId FROM Contact WHERE AccountId in :acctsWithNewRecordType.keySet()]) {Account parentAccount = acctsWithNewRecordType.get(c.accountId); c.RecordTypeId = '012000000008QwA'; updatedContacts.add(c); } update updatedContacts; }

 I tried replacing the:

 

c.RecordTypeId = '012000000008QwA';

with: 

 

 

if (Trigger.new[i].RecordTypeId != '012000000008Qw1') {c.RecordTypeId = '012000000008QwF'};

 

 

to accomplish this:


(Trigger.new[i].RecordTypeId != '012000000008Qw1') then c.RecordTypeId = '012000000008QwF'
(Trigger.new[i].RecordTypeId != '012000000008Qw0') then c.RecordTypeId = '012000000008QwA'
(Trigger.new[i].RecordTypeId != '012000000008Qvv') then c.RecordTypeId = '012000000008Qw5'

 

 

But to no avail...

 

Any help would be appreciated.

 

Thanks, Chris

 

 

 

 

 

tamirtamir
Have you solved this problem?  I would like to do exactly the same thing.
NikiVNikiV

How about naming the Record Types the same names for both Contact and Account?  That way you are not hard coding ID values (which may not match going from Sandbox to Prod), even though you are still hardcoding the names.  A fancier way would be to create a custom object that holds the matches from Account RecordTypeId to Contact RecordTypeId so you can do a lookup in that object for the partner Id.  Then the hard coding is done in the SFDC object and can easily be modified on the fly if the RecordType names/ids change.  More work though.

 

Anyways, if you want to match on names, you can collect up a list of all the distinct Account RecordType names, then query for Contact RecordTypes where the name is in the list (filter on the object so you don't get the same Account record type records back).  Map that out based on name and you can grab the Contact RecordType Id value during your for loop.

 

Does that make any sense?

EshEsh

I have the same Problem..Have you got any solution for this..Thank you