I currently have a class that populates the related lists of all child records with a contact role based on the creation of a contact on the parent record. An issue I ran into is that whenever the contact role is edited, rather than replacing the current role on the child records with the new value it adds it again under the previous contact role. I tried creating a trigger that replaces the old value with the new one if if the contact name is the same but nothing is happening when fired. Can anyone point me in the right direction?
Trigger:
trigger LAM_DuplicateContactRole on Contact (before insert, before update) {
map<Id, Contact> existingContactMap = new map<Id, Contact>([Select Id, Name, Customer_Role__c From Contact Where Customer_Role__c != null]);
map<Id, AccountContactRole> existingACRoleMap = new map<Id, AccountContactRole>([Select Id, Role FROM AccountContactRole]);
for(Contact c : Trigger.new){
if(Trigger.isUpdate){
if(c.name == existingContactMap.get(c.Id).Name && c.Customer_Role__c != existingACRoleMap.get(c.Id).Role){
LAM_RelatedListUpdate.RollContactRolesToAccountChildren(trigger.new);
}
upsert c;
}
}
}
CLASS THAT HANDELS THE CONTACT ROLE ROLL DOWN ON ACCOUNT HIERARCHY:
public class LAM_RelatedListUpdate {
Public static void RollContactRolesToAccountChildren(List<Contact> con){
//variables for the account lam record types
Id LAMCRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();//get recordtypeID of Large Customer
Id LAMSRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();//get recordtypeID of Large Customer
Id LAMDRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId() ;//get recordtypeID of Large Customer
//list of child accounts, parent accounts, contacts and contact roles
List<Id> accountsFromContacts = new List<Id>();
List<Account> parentAccounts = new List<Account>();
List<Account> listOfAccountChildren = new List<Account>();
List<AccountContactRole> contactRolesClones = new List<AccountContactRole>();
System.debug(con);
for(contact c : con){
accountsFromContacts.add(c.AccountId);
}
parentAccounts = [Select Id, RecordTypeId from Account Where Id = :accountsFromContacts ];
listOfAccountChildren = [Select Id, RecordTypeId, Parent_Account_for_LAM__c from Account Where Parent_Account_for_LAM__c = :accountsFromContacts ];
//updates the contact roles of the child accounts
for(contact c : con){
for(Account a : listOfAccountChildren ){
if(isMatchingRecord(a.Parent_Account_for_LAM__c, parentAccounts) ){
AccountContactRole cloneConatactRole = new AccountContactRole( AccountId = a.id , ContactId = c.Id, Role = c.Customer_Role__c );
contactRolesClones.add(cloneConatactRole);
}
}
}
//Database.insert(contactRolesClones);
//Database.update(contactRolesClones);
Boolean isUpsertfirstTime = true;
try{
upsert contactRolesClones;
}catch(DMLException e){
if(isUpsertfirstTime){
System.debug(e);
isUpsertfirstTime = false;
}
}
}
/* public static void deleteChildRole(Id ParAcc, List<Contact> c){
List<AccountContactRole> contactRolesClones = new List<AccountContactRole>();
List<Contact> listOfAccountChildren = [Select Id, RecordTypeId, Parent_Account_for_LAM__c from Account Where Parent_Account_for_LAM__c = :accountsFromContacts ];
}
*/
//checks that the parent account is of these lam record types, and if the parentId matches the one of the child
public static Boolean isMatchingRecord(Id ParAcc, List<Account> acc){
Id LAMCRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();//get recordtypeID of Large Customer
Id LAMSRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();//get recordtypeID of Large Customer
Id LAMDRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId() ;//get recordtypeID of Large Customer
for (account a: acc){
if (a.Id == ParAcc ){
if(a.recordtypeid == LAMCRecordTypeId || a.recordtypeid == LAMDRecordTypeId || a.recordtypeid == LAMSRecordTypeId){
return true;
}
}
}
return false;
}
public static void deleteChildAccountContactRoles(List<Contact> con){
List<AccountContactRole> contactRoles = new List<AccountContactRole>();
for (contact c : con){
contactRoles = [Select id From AccountContactRole where contactId = :c.Id ];
}
delete contactRoles;
}
public static void upsertnewContactRole(List<Contact> nCon){
List<AccountContactRole> nContactRoles = new List<AccountContactRole>();
for (contact nc: nCon){
// if(nc.FirstName == nCon.FirstName)
nContactRoles = [Select id from AccountContactRole where contactId = :nc.Id];
}
upsert nContactRoles;
}
}
/*** for(Account a : accountsWithRoles ) {
if(a.recordtypeid == LAMCRecordTypeId || a.recordtypeid == LAMDRecordTypeId || a.recordtypeid == LAMSRecordTypeId) {
for (Account acc :listOfAccountChildren ){
if(acc.ParentId == a.Id){
for(AccountContactRole ACR : a.AccountContactRoles){
AccountContactRole cloneConatactRole = new AccountContactRole( AccountId = acc.id , ContactId = ACR.ContactId, Role = ACR.Role );
contactRolesClones.add(cloneConatactRole);
}
}
}
}
} **/
//AccountContactRole newConatactRole = new AccountContactRole( AccountId = acc.id , ContactId = ACR.ContactId, Role = ACR.Role );
TRIGGER FOR CLASS:
trigger LAM_Contact_ReleatedList on Contact (after insert, after update, after delete) {
if(Trigger.isAfter){
if(Trigger.IsInsert || Trigger.IsUpdate){
LAM_RelatedListUpdate.RollContactRolesToAccountChildren(trigger.new);
}
}
if(Trigger.isAfter && Trigger.isDelete){
LAM_RelatedListUpdate.deleteChildAccountContactRoles(trigger.old);
}
}