I have one recordtype named 'India' in both Account and Contact Objects.My requirement is whenever i create a new account record of type 'India' a new contact record of type 'India' should be created automatically.
i tried with the following code:
trigger AccRecTypeTrigger on Account (before insert) {
List<RecordType> recType = [select id,name from RecordType where sobjectType = 'Account' and isActive = true];
Map<String,Id> m = new Map<String,Id>();
for(RecordType r : recType){
m.put(r.name,r.id);
}
List<Contact> conlst = new List<Contact>();
for(Account a : trigger.new){
a.recordTypeId = m.get('India');
Contact con = new Contact();
con.accountId = a.id;
con.recordTypeId = a.recordTypeId;
con.recordType.name = a.recordType.name;
conlst.add(con);
}
insert conlst;
}
But i am getting following error when i try to save
Apex trigger AccRecTypeTrigger caused an unexpected exception, contact your administrator: AccRecTypeTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AccRecTypeTrigger: line 14, column 1
Please help me how to achieve this......