• brian.muller1.3914465263748274E12
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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!
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!