0

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......

1
  • 1
    Can you please format your code correctly?
    – Akram G
    Jul 28, 2016 at 12:29

3 Answers 3

4

In your code, you are assigning a incorrect record type id to contact.

con.recordTypeId = a.recordTypeId; 

In above line you assinging a record type id of Account to a contact record. You need to use contact record type id.

con.recordType.name = a.recordType.name

We should assign the record type id, not the name. Hence, above line should not be required.

Instead of quering record type id, you can use a Describe call. By doing this, you will save SOQL in your trigger code.

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business').getRecordTypeId();
System.debug('::::'+recordTypeId );
1
  • 1
    Though it's worth noting that the Name field isn't unique, and best practice is to find parent records by a unique field when possible. Still, better to describe than to query.
    – Adrian Larson
    Jul 28, 2016 at 12:51
0

The recordType for Account is not the same than the recordType for Contact

You need to udate the rectype

List recType = [select id,name from RecordType where sobjectType = 'Contact' and isActive = true];

also this code have no sense (can you explain why?) con.recordType.name = a.recordType.name;

What is the purpose of the the map created?

Can you please share you code in a better format?

this is my suggestion to you

trigger AccRecTypeTrigger on Account (before insert) {
RecordTypeId recTypeId = [select id,name from RecordType where sobjectType = 'Contact' and isActive = true].Id;
    List<Contact> conlst = new List<Contact>();
    for(Account a : trigger.new){
        Contact con = new Contact(); 
        con.accountId = a.id; 
        con.recordTypeId = recTypeId;
        conlst.add(con);
    }
    insert conlst;
}
0

Two think you made mistake.

  1. You must use after insert in your trigger then only you get the Account Id value.

    trigger AccRecTypeTrigger on Account (after insert)

  2. To get "India" Contact Record type and assign to that Id the contact recordtypeId.

    Id IndiaContactRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('India').getRecordTypeId();

    con.recordTypeId = IndiaContactRecordTypeId;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .