Skip to main content
Ilya Potapov (Krapka) 님이 #Apex에 질문했습니다
Hello everyone, I had got stuck in writing trigger.

I have an object called AccountContact. This object is for linking Contact and Account (junction?) 

One contact can work in several accounts. Contact can have only one primary workplace which gets determined by the checkbox 

on the AccountContact object. Trigger must determines primary work place automatically.  

If reference object gets created for the first time and there are no other working places for this contact isPrimary=true.​

​If iSprimary was changed from ‘true’ to ‘false’, ‘true’ should be set on the last workplace(use created date field on reference object).​

If Isprimary was changed from ‘false’ to ‘true’, Isprimary should be unchecked from last primary workplace (use created date field on reference object).​

​only one primary workplace is allowed for one contact​

bulkification should be taken into consideration. I.e. insert, update list of references. ​

​Thanks for any help
답변 2개
  1. 2022년 9월 18일 오전 9:07
    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.
0/9000