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
Prasanth RPrasanth R 

recordtype with trigger

Create a picklist field with two value Type1, Type2 in contact object. Picklist fields name = 'Secondary Contact Type', make it mandatory When a user creates a Contact record, if User selects value in Secondary Contact Type a new record in the Object called Secondary Contact should be created.
Create two record type for Secondary Contact object namely Sales and Service.
If Type1 selected - Secondary Contact of record type 'Sales' should be created
If Type2 selected - Secondary Contact of record type 'Service' should be created
Secondary Contact created should be seen in Contact records detail page as a lookup field.

trigger contactRecordValues on Contact (after insert) { List<Secondary_Contact1__c> sectlist = new List<Secondary_Contact1__c>(); for(Contact con:Trigger.new){ if(con.Secondary_Contact_Type__c=='Type1'){ Secondary_Contact1__c seccon = new Secondary_Contact1__c(); seccon.RecordTypeId = con.Secondary_Contact1__c; sectlist.add(seccon);
}
}
if(sectlist.size()>0){ insert sectlist; }
}

above code is i tried...kindly someone help meUser-added imageUser-added imageUser-added imageUser-added image
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prasanth,

try with below code.
trigger contactRecordValues on Contact (after insert) { 
List<Secondary_Contact1__c> sectlist = new List<Secondary_Contact1__c>();
Id SalesRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Sales').getRecordTypeId();
Id ServiceRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Service').getRecordTypeId();
 
for(Contact con:Trigger.new){ 
if(con.Secondary_Contact_Type__c=='Type1')
{ 
Secondary_Contact1__c seccon = new Secondary_Contact1__c(); 
//Add required fields for Secondary_Contact1__c creation
seccon.RecordTypeId = con.SalesRecordTypeId;
seccon.contact__c =con.id; 
sectlist.add(seccon);
}else if(con.Secondary_Contact_Type__c=='Type2'){

Secondary_Contact1__c seccon1 = new Secondary_Contact1__c(); 
//Add required fields for Secondary_Contact1__c creation
seccon1.RecordTypeId = con.ServiceRecordTypeId;
seccon1.contact__c =con.id; 
sectlist.add(seccon1);
}
}
if(sectlist.size()>0){ 
insert sectlist; 
}
}

If this helps, please mark it as best answer.

Thanks!!
 
Prasanth RPrasanth R
hi @ankaiah
can you explain these two lines

Id SalesRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Sales').getRecordTypeId(); Id ServiceRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Service').getRecordTypeId();
AnkaiahAnkaiah (Salesforce Developers) 
By using those two lines You need to query the Secondary_Contact1__c object recordtype ids.

 Sorry.!! Can you please replace the Account with Secondary_Contact1__c. Please refer the below modified code.
 
trigger contactRecordValues on Contact (after insert) { 
List<Secondary_Contact1__c> sectlist = new List<Secondary_Contact1__c>();
Id SalesRecordTypeId = Schema.SObjectType.Secondary_Contact1__c .getRecordTypeInfosByName().get('Sales').getRecordTypeId();
Id ServiceRecordTypeId = Schema.SObjectType.Secondary_Contact1__c .getRecordTypeInfosByName().get('Service').getRecordTypeId();
 
for(Contact con:Trigger.new){ 
if(con.Secondary_Contact_Type__c=='Type1')
{ 
Secondary_Contact1__c seccon = new Secondary_Contact1__c(); 
//Add required fields for Secondary_Contact1__c creation
seccon.RecordTypeId = con.SalesRecordTypeId;
seccon.contact__c =con.id; 
sectlist.add(seccon);
}else if(con.Secondary_Contact_Type__c=='Type2'){

Secondary_Contact1__c seccon1 = new Secondary_Contact1__c(); 
//Add required fields for Secondary_Contact1__c creation
seccon1.RecordTypeId = con.ServiceRecordTypeId;
seccon1.contact__c =con.id; 
sectlist.add(seccon1);
}
}
if(sectlist.size()>0){ 
insert sectlist; 
}
}

If this helps, Please mark it as best answer.

Thanks!!

 
Prasanth RPrasanth R
Id SalesRecordTypeId = Schema.SObjectType.Secondary_Contact1__c .getRecordTypeInfosByName().get('Sales').getRecordTypeId(); Id ServiceRecordTypeId = Schema.SObjectType.Secondary_Contact1__c .getRecordTypeInfosByName().get('Service').getRecordTypeId();

i'm not able to understand this.can you give anyother way?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prasanth,

Try with below alterantive query.
Id SalesRecordTypeId = [SELECT id FROM RecordType WHERE name = 'Sales' and sobjecttype ='Secondary_Contact1__c '].Id;
Id ServiceRecordTypeId= [SELECT id FROM RecordType WHERE name = 'Service' and sobjecttype ='Secondary_Contact1__c '].Id;

If this helps, please mark it as best answer.
Prasanth RPrasanth R
i tried this facing problem,,kindly assist me
trigger contactRecordValues on Contact (after insert) {
    
    List<Secondary_Contact1__c> sectlist = new List<Secondary_Contact1__c>();   
    
    Id SalesRecordTypeId = [SELECT id FROM RecordType WHERE name = 'Sales' and sobjecttype ='Secondary_Contact1__c '].Id;
    Id ServiceRecordTypeId= [SELECT id FROM RecordType WHERE name = 'Service' and sobjecttype ='Secondary_Contact1__c '].Id;    
    for(Contact con:Trigger.new){ 
        if(con.Secondary_Contact_Type__c=='Type1'){ 
            Secondary_Contact1__c seccon = new Secondary_Contact1__c();
            seccon.RecordTypeId = con.SalesRecordTypeId;
            seccon.Contact__c = con.Id;        
            sectlist.add(seccon);       
        }
        else if(con.Secondary_Contact_Type__c=='Type2'){ 
            Secondary_Contact1__c seccon1 = new Secondary_Contact1__c(); 
            seccon1.RecordTypeId = con.ServiceRecordTypeId;
            seccon1.Contact__c = con.Id;        
            sectlist.add(seccon1); 
        }
    }
        if(sectlist.size()>0){
            insert sectlist;
        }
    }

User-added image
 
Prasanth RPrasanth R
also lookupfield should be created in contact object
AnkaiahAnkaiah (Salesforce Developers) 
Can you remove the con variable at line number 10 & 16.
Try with below code. 
trigger contactRecordValues on Contact (after insert) {
    
    List<Secondary_Contact1__c> sectlist = new List<Secondary_Contact1__c>();   
    
    Id SalesRecordTypeId = [SELECT id FROM RecordType WHERE name = 'Sales' and sobjecttype ='Secondary_Contact1__c '].Id;
    Id ServiceRecordTypeId= [SELECT id FROM RecordType WHERE name = 'Service' and sobjecttype ='Secondary_Contact1__c '].Id;    
    for(Contact con:Trigger.new){ 
        if(con.Secondary_Contact_Type__c=='Type1'){ 
            Secondary_Contact1__c seccon = new Secondary_Contact1__c();
            seccon.RecordTypeId = SalesRecordTypeId;
            seccon.Contact__c = con.Id;        
            sectlist.add(seccon);       
        }
        else if(con.Secondary_Contact_Type__c=='Type2'){ 
            Secondary_Contact1__c seccon1 = new Secondary_Contact1__c(); 
            seccon1.RecordTypeId = ServiceRecordTypeId;
            seccon1.Contact__c = con.Id;        
            sectlist.add(seccon1); 
        }
    }
        if(sectlist.size()>0){
            insert sectlist;
        }
    }
If this helps, Please mark it as best answer.

Thanks!!
 
AnkaiahAnkaiah (Salesforce Developers) 
also lookupfield should be created in contact object??
explain more about this statement
Prasanth RPrasanth R
Secondary Contact created should be seen in Contact records detail page as a lookup field......