Reply
Newbie
Lipps7974
Posts: 4
0
Accepted Solution

Account checkbox marks true when at least one Contact's checkbox is true

We currently have a custom checkbox field in contacts that we have set to true if they are the best person to call for that particular account. I want to create a checkbox field in accounts that will automatically mark true if it contains a contact with that particular checkbox marked true. 

Essentially, I want an account checkbox called "Contains a best contact" that marks true when at least one of its contacts has the "Best person to contact" checkbox marked true.

Is this possible?

Thanks in advance!

Trusted Contributor
NaiduPothini
Posts: 369
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

[ Edited ]

You can write a trigger on the contact object, when a record with Best person to contact is checked or unchecked, update the account record's Contains a best contact field.

Thanks & Regards
Naidu
Newbie
Lipps7974
Posts: 4
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

Could you provide me with the jist of the code?
Trusted Contributor
NaiduPothini
Posts: 369
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

Trigger ContactTrigger on Contact(after insert, after update)
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            List<Id> accIds = new List<Id>();
            
            for(Contact con : Trigger.new)
            {
                if(con.Best_person_to_contact__c)
                {
                    accIds.add(con.AccountId);
                }
            }

            List<Account> accts = [SELECT Id, Name, Contains_a_best_contact__c FROM Account WHERE Id IN :accIds];

            for(Account acc : accts)
            {
                acc.Contains_a_best_contact__c = true;
            }

            update accts;
        }



        if(Trigger.isUpdate)
        {
            List<Id> updateAccs = new List<Id>();
            List<Account> accts = new List<Account>();
            
            for(Integer i =0; i < Trigger.new.size(); i++)
            {
                if(Trigger.new[i].Best_person_to_contact__c <> Trigger.Old[i].Best_person_to_contact__c)
                {
                    updateAccs.add(Trigger.new[i].AccountId);
                }
            }

            Map<Id, AggregateResult> accMap = new Map<Id, AggregateResult>([SELECT AccountId Id, Count(Id) cnt FROM Contact WHERE AccountId IN :updateAccs AND Best_person_to_contact__c = true GROUP BY AccountId]);

            for(Account acc : [SELECT Id, Name, Contains_a_best_contact__c FROM Account WHERE Id IN :updateAccs])
            {
                AggregateResult ar = accMap.get(acc.Id);
                
                if(ar <>  null)
                {
                    if(Integer.valueOf(ar.get('cnt')) > 0)
                    {
                        acc.Contains_a_best_contact__c = true;
                    }
                    else
                    {
                        acc.Contains_a_best_contact__c = false;
                    }
                }
                else
                {
                    acc.Contains_a_best_contact__c = false;
                }

                accts.add(acc);
            }

            update accts;
        }
    }
}

 Try this.

Thanks & Regards
Naidu
Super Contributor
sfdcfox
Posts: 3,892

Re: Account checkbox marks true when at least one Contact's checkbox is true

trigger updateRelatedAccounts on Contact( after insert, after update, after delete, after undelete ) {
  Map< id, account > accs = new map< id, account >( );
  if( trigger.old != null )
    for( contact record: trigger.old )
      accs.put( record.accountid, new account( id = record.accountid ) );
  if( trigger.new != null )
    for( contact record: trigger.new )
      accs.put( record.accountid, new account( id = record.accountid ) );
  accs.remove( null );
  update accs;
}

 

trigger updateBestContactField on Account ( before update ) {
  for( account record: trigger.new )
    record.has_best_contact__c = false;
  for( contact record: [select accountid from contact where best_contact__c = true and accountid in :trigger.new )
    trigger.newmap.get( record.accountid ).has_best_contact__c = true;
}

 

~ sfdcfox ~


I am a sandwich. That is all.

Newbie
Lipps7974
Posts: 4
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

Thank you!!!!!
Newbie
Lipps7974
Posts: 4
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

Just to clarify, this will only work for accounts moving forward, right? All the Accounts I have already set up will not have this checked until the contact that is flagged for "best contact" is updated or changed, correct?
Super Contributor
sfdcfox
Posts: 3,892
0

Re: Account checkbox marks true when at least one Contact's checkbox is true

Correct. Naidu's solution requires an update to contacts, my solution requires an update to accounts or contacts. By the way, Naidu's solution doesn't cover contacts that move accounts or are deleted, so be aware of that limitation.

~ sfdcfox ~


I am a sandwich. That is all.