+ Start a Discussion
brian.muller1.3914465263748274E12brian.muller1.3914465263748274E12 

Trigger to Update Account Record type based on Contact record type

Hello, 

I am trying to write a trigger that creates a new account when an orphaned Contact is created. The problem that I'm having is when I try to change the Account Record Type. FYI I am completely new to Apex and triggers (In fact this is only the second one I've written).


This is what I have so far :
 
(note:most of this is based on answers to questions I have found here and other websites. Just don't want to try to take credit for something that I didn't create myself :) )

trigger CreateAccountsForContacts on Contact (before insert) {
    Map<Contact, Account> accounts = new Map<Contact, Account>();
    for(Contact record: Trigger.new) {
        if(record.AccountId == null) {
            accounts.put(record, new Account(Name=record.FirstName+' '+record.LastName));
        }
    }
    insert accounts.values();
    for(Contact record: Trigger.new) {
        if(record.AccountId == null) {
            record.AccountId = accounts.get(record).Id;
        }
      
        if(record.RecordType.Name == 'Patient') {
            account.RecordTypeId = '012U000000014g1';
            }
    }
}

If I take out 

if(record.RecordType.Name == 'Patient') {
            account.RecordType.Name = 'Patient';
            }

it works fine, creates the account no problem. However I need it to change the Account Type based on the Contact Record Type, otherwise it could default to the wrong one. My idea was to check to see if the Contact Record Type was Patient, and if it was, set the Account Record Type to Patient as well.
 I'm sure it's something in the syntax I just don't understand. Any help would be appreciated. Thank you!
Best Answer chosen by brian.muller1.3914465263748274E12
GunnarGunnar
Looks like you save the accounts, then try to do an update?
I gather up all my foreign keys and records at the very top, first thing. In this case Account record types.
Where you create the new account, assign the record type at that time.
If you don't have enough information at that time, then you may have to restructure so you do.

All Answers

GunnarGunnar
Looks like you save the accounts, then try to do an update?
I gather up all my foreign keys and records at the very top, first thing. In this case Account record types.
Where you create the new account, assign the record type at that time.
If you don't have enough information at that time, then you may have to restructure so you do.
This was selected as the best answer
brian.muller1.3914465263748274E12brian.muller1.3914465263748274E12
Thank you for your response.

I did what you suggested and moved the statement up to where I was creating the account and it worked perfectly! Thank you so much for your help! For future if anyone else wants to use it, here is my code:

trigger CreateAccountsForContacts on Contact (before insert) {
    Map<Contact, Account> accounts = new Map<Contact, Account>();
    for(Contact record: Trigger.new) {
        if(record.AccountId == null) {
            if(record.RecordTypeId == '012U000000014gu'){
            accounts.put(record, new Account(Name=record.FirstName+' '+record.LastName,RecordTypeId='012U000000014g1'));
            } else {
            accounts.put(record, new Account(Name=record.FirstName+' '+record.LastName));
           
        }
       
    }
    insert accounts.values();
    for(Contact record1: Trigger.new) {
        if(record.AccountId == null) {
            record.AccountId = accounts.get(record).Id;
        }
       
    }
}