2 risposte
Hello, Priya. Here I have three function on different situations : 1) creating 2)changed from ‘true’ to ‘false’ 3 )changed from ‘false’ to ‘true’The first one is only on Before Insert and it works properly, The second and third are on Before Update both and I get runtime error.public class AccountContactTriggerHelper { public static void firstMethod(List<AccountContact__c> acInputList) { List<AccountContact__c> contactAccountExisting = new List<AccountContact__c>( [Select Contact__c, isPrimary__c From AccountContact__c]); List<Id> contactId = new List<Id>(); for (AccountContact__c ac : contactAccountExisting) { contactId.add(ac.Contact__c); } for(AccountContact__c a : acInputList) { if (!contactId.contains(a.Contact__c)) { a.isPrimary__c = true; } } } public static void secondMethod(List<AccountContact__c> acInputList,Map<Id,AccountContact__c> acOldMap) { List <AccountContact__c> accountContactsChangedToFalse = new List <AccountContact__c>(); List <AccountContact__c> accountContactsToUpdate = new List <AccountContact__c>(); Set <Id> ContactsChangedToFalse = new Set <Id>(); for(AccountContact__c a : acInputList) { AccountContact__c oldAccountContact = acOldMap.get(a.ID); if ((a.isPrimary__c == false) && (oldAccountContact.isPrimary__c == true)) { accountContactsChangedToFalse.add(a); ContactsChangedToFalse.add(a.Contact__c); } } if (accountContactsChangedToFalse.isEmpty()) return; List <AccountContact__c> accountContactsSortedByDate = new List<AccountContact__c>( [SELECT Contact__c,Account__c,isPrimary__c,CreatedDate,Name FROM AccountContact__c WHERE Contact__c IN : ContactsChangedToFalse ORDER BY CreatedDate DESC]); for(AccountContact__c a : accountContactsChangedToFalse) { for (AccountContact__c b : accountContactsSortedByDate) { if ((a.Contact__c == b.Contact__c) && (a.Account__c != b.Account__c)) { b.isPrimary__c = true; accountContactsToUpdate.add(b); break; } } } update accountContactsToUpdate; } public static void thirdMethod(List<AccountContact__c> acInputList,Map<Id,AccountContact__c> acOldMap) { List <AccountContact__c> accountContactsChangedToTrue = new List <AccountContact__c>(); List <AccountContact__c> accountContactsToUpdate = new List <AccountContact__c>(); Set <Id> ContactsChangedToTrue = new Set <Id>(); for(AccountContact__c a : acInputList) { AccountContact__c oldAccountContact = acOldMap.get(a.ID); if ((a.isPrimary__c == true) && (oldAccountContact.isPrimary__c == false)) { accountContactsChangedToTrue.add(a); ContactsChangedToTrue.add(a.Contact__c); } } if (accountContactsChangedToTrue.isEmpty()) return; List <AccountContact__c> accountContactsSortedByDate = new List<AccountContact__c>( [SELECT Contact__c,Account__c,isPrimary__c,CreatedDate,Name FROM AccountContact__c WHERE Contact__c IN : ContactsChangedToTrue ORDER BY CreatedDate DESC]); for(AccountContact__c a : accountContactsChangedToTrue) { for (AccountContact__c b : accountContactsSortedByDate) { if (b.isPrimary__c == true) { b.isPrimary__c = false; accountContactsToUpdate.add(b); break; } } } update accountContactsToUpdate; }}Thanks for help.