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 on the initial 'create new' screen

I have a working trigger (see below) that sets the Contact Record Type based on the Record type of the Account record.  By design, the contact record type names match the account record type names.  The purpose of this design is to use record types/page layouts to contol/filter the available values in a custom pick list field.  Although this trigger accurately sets the Contact Record type to the correct value, it does not set it until after the initial contact record is saved.  Unfortunatley, I need it to be set and be in effect when the initial 'create new contact record' screen appears.  If working properly, the inital screen would take into affect the controls/filters as defined for that specific record type.  I had assumed that the trigger setting of 'before insert, before update' would meet my requirement but it does not. Can anyone help with this or am I asking for something that is not possible.  I'm aware that I can use User Profiles to control the default record type, but that is not really a good solution for me.  Any advice and insight is greatly appreciated!
Thanks,
Bruce

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;
            }   }  } }
 

ManojjenaManojjena
Hi Bruce,

Try with 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,Id> mapContactRecordTypes = new Map<String,Id>{};
    for(RecordType C_RTY: C_rtypes){
       mapContactRecordTypes.put(C_RTY.Name,C_RTY.Id);
    }
    Set<String> setAccID = new Set<String>();
    for (Contact THIS_CON : trigger.new){
       if(THIS_CON.AccountId != null){
            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(THIS_CON.AccountId != null){
            if(mapContactRecordTypes.containsKey(MapAccount.get(THIS_CON.AccountId).RecordType.Name)){
                    THIS_CON.recordTypeId = mapContactRecordTypes.get(MapAccount.get(THIS_CON.AccountId).RecordType.Name);
            }   
         } 
     }          
}
Let me know if it helps
 
Bruce HansonBruce Hanson

Hi Manoj,
Thanks for the response but using this code did not change the user experience in sandbox.
I've had others tell me that what I'm trying to do can not be done as the I can only set default record types using profile settings.
I'm not sure where to go from here.
Thoughts?
Thanks again.
Bruce

ManojjenaManojjena
Hi Bruce,

I don't think it is possible through profile level .As you may have mor then record type in Account as well as Contact . 
Only through coding it is possible .However you can do one thing you can create a validation which will force user to seect that record type which account has .
If you want to give all contact record type access to profile and let user to select record type and through trigger you can add validation Account record type is X so you need to select X record type in contact .Still it will be probelm for user .

Finally you can do one thing you can override contact new button with VF page an dcheck the Record type visibilty only the Account record type by default .
Let me know if it helps .

Thanks 
Manoj