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
Aviator517Aviator517 

Enforcing atleast 1 record within a group to have a checkbox checked

I'm running into recursive issues when designing code to enforce having at least 1 contact within a household to have "Head of Household" checked. Does anyone have any advice when working with something like this in the past? The code should be such that there is always someone designated "Head of Household" and can be changed at any point, but there can never be two. The problem comes in when I have 2 more contacts, with one set as the head of household. I then go to the other contact, to select the check box (which should go through and uncheck the other contact), but it gets in a loop where it keeps going back and forth updating each other. It seems to be an issue with dealing with not allowing the end user to uncheck a checkbox, but then in the code I should be able to uncheck it (to facilitate changing the head of household). Any friendly advice would be greatly appreciated.
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Aviator517,

 

Based on my understanding of your concept, you are coming to say that while updating or creating a contact there should be a house hold check box enabled only for one contact. Isn't it?

 

Then, try the following trigger in contact object,

 

trigger checkAllContacts on Contact (before insert, before update) {
    for(contact c: trigger.New){
       List<Contact> clist = [Select id,name,Head_Of_House_Hold__c from contact where Head_Of_House_Hold__c =: true];
       List<Contact> c1list = new List<Contact>();
       if(clist.size() > 0){
          for(contact c1 : clist){
            if(c.Head_Of_House_Hold__c  == true && c1.Head_Of_House_Hold__c == true){
                     c1.Head_Of_House_Hold__c = false;
                     c1list.add(c1);
            }
          }
          update c1list;
        }
    }
}

 

Hope this will help you...!

 

Please don't forget to give kudos and mark this as a solution, if this works out.

Aviator517Aviator517

Thanks for the reply! This is definitely on the right track. The only issue I run into is preventing users from manually changing the value from True to False for that checkbox.

 

For example, if they want to change the record who has it checked, they should check the other record, and the code will change anyone else to false.

 

But, if the user manually changes it from True to False, it should go back to True (to enforce having atleast 1 person with the box checked at any given time) - does that make sense?

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

No no not yet. Can you please explain in detail with an example?

 

And, let me know what hap when a user create a contact, Does the checkbox can be left blank at that time?

Also, while updating, it should not gets false right? but can we update a false value to true ,if it is created as false?

 

 

Aviator517Aviator517

Hi,

 

My apologies for the explanation. It's hard to explain.

 

If a new contact is created under accont, and it's the only one the checkbox should be true

If another contact is created under the same account, it should default to false.

If the other contact is updated from "False" to "True" it will make the change, and change any other contacts under that account to "False".

 

If a user tries to manually change a contact from "True" to "False", it will change back to "True" (enforcing validation that atleast 1 contact has it marked as true). If they would like to change who has it checked, they should go to the other contact, and change from "False" to "True" firing the trigger to clear out the other values.

 

Thanks again for your help so far!

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Thanks for your breif explaination. I can understand your scenario now.

 

Try the folloiwng trigger,

 

trigger checkAllContacts on Contact (before insert, before update) {
   
        for(contact c: Trigger.New){
           List<Contact> clist = [Select id,name,AccountId,Head_Of_House_Hold__c from contact
                                  where AccountId =: c.AccountId and Head_Of_House_Hold__c = true];
           List<Contact> c1list = new List<Contact>();
           if(clist.size() > 0){
              for(contact c1 : clist){
                if(c.Head_Of_House_Hold__c  == true ){
                         c1.Head_Of_House_Hold__c = false;
                         c1list.add(c1);
                }
              }
              update c1list;
            }
            else{
                if(c.Head_Of_House_Hold__c  == false){
                    c.addError('There should be at least one contact should be a "Head Of House Hold"');
                }
            }
        }
    }

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

Aviator517Aviator517
Wouldn't the c1 list update cause the updated records to run through the trigger (and cause the error)?