You need to sign in to do that
Don't have an account?
Bob
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
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); }
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
Maybe code like this...