function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lovely LopezLovely Lopez 

Update checkbox in Account to true if one of the Contacts have the checkbox set to true

I have a checkbox named Views in Contact and Account. I would like the checkbox in Account to be checked if one of the Contacts have that checkbox checked.  I know about the cross object formula but that only seem to work from Parent to Child object not the other way around. Is there any way to do this except for creating a trigger? If the trigger is only the solution, can you give me an idea what to do? I'm not very familiar with trigger yet. I would need help with this. Thank you!
Best Answer chosen by Lovely Lopez
Deepak RamaDeepak Rama
trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Web_Views__c!=Trigger.oldMap.get(cont.Id).Web_Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id, Account> accsMap =new Map<Id, Account>( [Select Id,Web_Views__c From Account Where Id in :accIds]);
  List<Contact> contacts = [Select Id,Web_Views__c, AccountId From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Web_Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Web_Views__c =  acc.Web_Views__c || cont.Web_Views__c;
  }
  update accsMap.values();
}
Try modifying your trigger to code above.

All Answers

Deepak RamaDeepak Rama
Lopez,

Trigger is the only solution in this case. You can read the documentation about triggers here and since you have a easy use-case, it should not be very difficult to code these triggers.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm

Also please take a look at the following step-by-step tutorial on how to write triggers:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm

Denis VakulishinDenis Vakulishin
Here the simple sample for your needs
trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Views__c!=Trigger.oldMap.get(cont.Id).Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id.Account> accsMap = [Select Id,Views__c From Account Where Id in :accIds];
  List<Contact> contacts = [Select Id,Views__c From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Views__c =  acc.Views__c || cont.Views__c;
  }
  update accsMap.values();
}


Denis VakulishinDenis Vakulishin
Hi,
Did it work for you ?
If yes, could you mark the Best Anwser?
If no, feel free to ask more questions.
Thanks.
Lovely LopezLovely Lopez
I apologized for not replying for a while. I got stuck with other work related stuff. I tried doing the sample that you gave but it keeps giving me this error: map must have exactly 2 type arguments at line 1 column 1.
Deepak RamaDeepak Rama
Try modifying the code to Following:


trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Views__c!=Trigger.oldMap.get(cont.Id).Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id.Account> accsMap =new Map<Id, Account>( [Select Id,Views__c From Account Where Id in :accIds]);
  List<Contact> contacts = [Select Id,Views__c From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Views__c =  acc.Views__c || cont.Views__c;
  }
  update accsMap.values();
}



Lovely LopezLovely Lopez
Still giving me the error. Here's what I have
trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Web_Views__c!=Trigger.oldMap.get(cont.Id).Web_Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id.Account> accsMap =new Map<Id, Account>( [Select Id,Web_Views__c From Account Where Id in :accIds]);
  List<Contact> contacts = [Select Id,Web_Views__c From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Web_Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Web_Views__c =  acc.Web_Views__c || cont.Web_Views__c;
  }
  update accsMap.values();
}

Deepak RamaDeepak Rama
I guess the map definition has error. Try modifying code to following: 

trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Web_Views__c!=Trigger.oldMap.get(cont.Id).Web_Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id, Account> accsMap =new Map<Id, Account>( [Select Id,Web_Views__c From Account Where Id in :accIds]);
  List<Contact> contacts = [Select Id,Web_Views__c From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Web_Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Web_Views__c =  acc.Web_Views__c || cont.Web_Views__c;
  }
  update accsMap.values();
}


Lovely LopezLovely Lopez
That works. It let me saved the trigger but now I'm getting this error when I tried updating the contact. 

Apex trigger UpdateViews caused an unexpected exception, contact your administrator: UpdateViews: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Trigger.UpdateViews: line 27, column 1
Deepak RamaDeepak Rama
trigger UpdateViews on Contact (after insert,after update) {
 
  Set<Id> accIds = new Set<Id>();
  if(Trigger.isInsert)
  {
    for(Contact cont: Trigger.new) {
       if(!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  else if (Trigger.isUpdate)
  {
    for(Contact cont: Trigger.new) {

       if(cont.Web_Views__c!=Trigger.oldMap.get(cont.Id).Web_Views__c&&!accIds.contains(cont.AccountId))
        accIds.add(cont.AccountId);
    }
  }
  
  Map<Id, Account> accsMap =new Map<Id, Account>( [Select Id,Web_Views__c From Account Where Id in :accIds]);
  List<Contact> contacts = [Select Id,Web_Views__c, AccountId From Contact Where AccountId in :accIds];

  for(Account acc : accsMap.values())
    acc.Web_Views__c = false;
  for(Contact cont : contacts)
  {
    Account acc = accsMap.get(cont.AccountId);
    acc.Web_Views__c =  acc.Web_Views__c || cont.Web_Views__c;
  }
  update accsMap.values();
}
Try modifying your trigger to code above.
This was selected as the best answer
Lovely LopezLovely Lopez
It works perfectly! Thank you!
Lovely LopezLovely Lopez
I came across another issue when I applied this trigger. This works perfectly in an account that has contacts but not in person account. I've been told to write the trigger on the account where the IsPersonAccount is true but not really sure how.