+ Start a Discussion
BobBob 

Adding Record Types to an Account/Contact Trigger

I need to add specific record types to my trigger below. The record type names are below as well. I've tried adding record types, but nothing seems to work. Any help with this would be greatly appreaciated. 

Record Types:
NA Customer Account Record Type - Active Customer - BW
NA Customer Account Record Type - Prospect - BW
NA Contact Record Type - BW
 
//Created by Bob Poliquin  4/15/2016


trigger Trigger_ChangeContactOwner on Contact (after insert, after update) {
List <Id> AccountIds = new List <Id>();
List <Account> accounts = new List<Account>();
List <Contact> contacts = new List<Contact>();

List <Account> accSave = new List<Account>();
List <Contact> contSave = new List<Contact>();

for(Contact c:trigger.new ){
if(c.AccountId !=NULL)
AccountIds.add(c.AccountId);
}

accounts = [Select Id, OwnerId, RecordType.DeveloperName From Account Where Id IN :AccountIds];
contacts = [Select Id, AccountId, OwnerId From Contact Where AccountId IN :AccountIds and Id NOT IN :trigger.new];

for(Contact c:trigger.new ){
for(Account acc:accounts){

if(c.AccountId == acc.Id && c.OwnerId !=acc.OwnerId ){
acc.OwnerId = c.OwnerId;
accSave.add(acc);
}
}

for(Contact con:contacts){
if(c.AccountId == con.AccountId && c.OwnerId != con.OwnerId){
con.OwnerId = c.OwnerId;
contSave.add(con);
}
}
}
update(accSave);
update(contSave);
}

 
JeffreyStevensJeffreyStevens
I didn't throw any code at you yet - just want to make sure you understand how to use and manipulate record types in APEX code first.

What do you mean by you want to "add record types"?  Do you mean that you want to change a record type of an existing record?  I think that's what you're wanting - just want to be sure though. 

Couple of things about RecordTypes...
There is a standard object called - RecordType.  There is a field called sObjectType, and a field called Name.  When you see a recordtype - it's that name field that you're seeing.  It's the ID of that record that is stored on your other salesforce objects.  

For example - if you have a record type "NA Customer Account Record Type - Active Customer - BW" - and the sObjectType is Account, it is the record ID of the RecordType record that is stored on the Account record. 

So - if you want to change the record type in the Account record from "NA Customer Account Record Type - Prospect - BW" to "NA Customer Account Record Type - Active Customer - BW", you would need to do a SOQL on RecordType - where Name = 'NA Customer Account Record Type - Active Customer - BW' (actually - you should use the DeveloperName field - it never changes) - and the sObjectType = 'Account'.  Then you would update the Account.RecordTypeId field with the ID return from that SOQL. 

Also, - as I looked at your code - you might want to look into Maps and how to use them.  For example - instead of doing a SOQL into a list of Account - you could do a SOQL into a map that has the AccountID as the key, and the Account record as the value.  Then you could just do an - .containsKey() method on that map - instead of looping through your entire accounts list for each triggered Contact.

Hope that helps
BobBob
This trigger assigns the contact owner when the account owner is changed. So if there are multiple contacts associated with the account they will change to the new account owner. The record types already exist. So I need a query/map that finds records with those specific record types and the account owner is change. Does this make sense? 
JeffreyStevensJeffreyStevens
Oh - well - see your trigger is on Contact, but yet you want to change the Contact when the ACCOUNT is changed - right?  So, in this case - I'm not sure you even need to do anything with record types.

Maybe code like this...
trigger accountChanged on Account(after update) {
/*
    When an Account owner changes, update all contacts 
    associated with that account to the same owner
*/

set<id> ownerChangedAccountIds = new set<id>();
for(Account a :trigger.new) {
  if(a.ownerId != trigger.oldMap.get(a.id).ownerId) {
    ownerChangedAccountIds.add(a.id);
  }
}

list<Contact> contactsToUpdate = new list<Contact>();
for(Account a :[SELECT id,ownerId,(Select id,ownerId FROM Contacts) 
                 FROM Account 
                 WHERE id IN :ownerChangedAccountIds]) {

  for(Contact c :a.Contacts) {
    c.ownerId = a.ownerId;
    contactsToUpdate.add(c);
  }

}

if(contactsToUpdate.size()>0) {
    update contactsToUpdate;
}
}