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
Bruce HansonBruce Hanson 

Set CONTACT record type based on ACCOUNT record type

am a begginner developer and I could really benefit from some assistance.  I’m trying to code a trigger that will set the Contact record type based on the Account record type.  I’m doing this in order to control the available picklist options on a custom pick list field on the contact record.  In short, the record types on both the account and contact objects are matching names.  I can’t use workflow because I need the contact record type established (to control the custom picklist field selection options) on the INITIAL  ‘create new’ page layout.  Although my trigger runs without error, its is not working as the ‘new’ contact record type always stays as the ‘default’ setting.  I’m not good at debugging so I’m not sure where to turn.  My code is below.  Any insight is greatly appreciated!

trigger SET_CON_RTYPE on Contact(before insert, before update){

//Query for the Account record types
    List<RecordType> A_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Account' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> A_RecordTypes = new Map<String,String>{};
    for(RecordType A_RTY: A_rtypes)
       A_RecordTypes.put(A_RTY.Name,A_RTY.Id);

//Query for the Contact record types
    List<RecordType> C_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Contact' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> C_RecordTypes = new Map<String,String>{};
    for(RecordType C_RTY: C_rtypes)
       C_RecordTypes.put(C_RTY.Name,C_RTY.Id);

  for (Contact THIS_CON : trigger.new)  {
  
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Agency')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Agency');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('BodyShop')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('BodyShop');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Rental')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Rental');
                                  
           } else {
           THIS_CON.RecordTypeid = C_RecordTypes.get('Other');
           }
      }
    }
Best Answer chosen by Bruce Hanson
Amit Chaudhary 8Amit Chaudhary 8
Hi Bruce Hanson,

Trigger will only fire when you create/Update a record(DML) not on screen( VF page or page layout). It will not set the record at screen. For Screen level you can set the defualt Record Type or you can choose the Recorf Type from Manu.

NOTE:- If you will set the default record type on profile level then it will not work according to account record type.

Please mark the above solution as best Answer if your Trigger issue is resolved. So that if some one has same issue that post can help.

Thanks,
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Bruce Hanson,
Please try below code:-
trigger SET_CON_RTYPE on Contact(before insert, before update)
{
    List<RecordType> C_rtypes = [Select Name, Id From RecordType where sObjectType='Contact' and isActive=true];
    Map<String,RecordType> mapContactRecordTypes = new Map<String,RecordType>{};
    for(RecordType C_RTY: C_rtypes)
	{
       mapContactRecordTypes.put(C_RTY.Name,C_RTY);
	}
	
	Set<String> setAccID = new Set<String>();
	for (Contact THIS_CON : trigger.new)  
	{
		setAccID.add(THIS_CON.accountid);
	}
		
	Map<Id,Account> MapAccount = new Map<id,Account>( [Select id,RecordType.Name,recordTypeId from account where id in :setAccID ] );

	for (Contact THIS_CON : trigger.new)  
	{
		if(MapAccount.containsKey(THIS_CON.accountId))
		{
			Account acc = MapAccount.get(THIS_CON.accountId);
			if(mapContactRecordTypes.containsKey(acc.RecordType.Name))
			{
				THIS_CON.recordTypeId = mapContactRecordTypes.get(acc.RecordType.Name).id;
			}
		}
	}
}
I hope this will help you!!

Please mark this as solution if this will help you. So that is some one has same issue this post can help,

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Bruce HansonBruce Hanson
Hi Amit.
Thanks.  Although your code changes the Contact record type after the record is created/saved, it does not set the Contact record type for the initial create screen ... even though you use 'before insert / before update'.  Does the problem have anything to do with having a default record type for that object and having the user enable use of the default?   Your continued assistance is greatly appreciated!
Amit Chaudhary 8Amit Chaudhary 8
Hi Bruce Hanson,

Trigger will only fire when you create/Update a record(DML) not on screen( VF page or page layout). It will not set the record at screen. For Screen level you can set the defualt Record Type or you can choose the Recorf Type from Manu.

NOTE:- If you will set the default record type on profile level then it will not work according to account record type.

Please mark the above solution as best Answer if your Trigger issue is resolved. So that if some one has same issue that post can help.

Thanks,
Amit Chaudhary
 
This was selected as the best answer