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
CW5CW5 

Supposedly Simply Trigger to Update Set True Checkbox on Account if any Contact Has Checkbox True?

I'd like to write a simple trigger that updates a field on account called HPD if any of the contacts have a checkbox called HPD checked.

 

This is what I've come up with so far but it needs a little tweaking... Any thoughts?

 

------------------------------------------------------------------------

 

trigger UpdateAccountFromContacts on Account(before insert, before update) {

Set<Id> accountIds = new Set<Id>();

for (Account a : Trigger.new)

accountIds.add(a.Id);

Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts) from Account a where HPD__c = True ] );

for (Account a : Trigger.new) {

Account accFromMap = accounts.get(a.Id);

if (accFromMap.Contacts.size() > 0) {

a.HPD__c = True;

}

} }

Best Answer chosen by Admin (Salesforce Developers) 
CW5CW5

This is the final solution to Update Account based on Contacts, using a trigger...

 

trigger UpdateAccountFromContacts on Account(before update) {

//Creates variable accountIds
Set<Id> accountIds = new Set<Id>();

//Selects all of the accounts that were newly created/updated
for (Account a : Trigger.new)

//Assigns the newly created/updated account IDs to the variable
accountIds.add(a.Id);

//Creates a map of the Accounts that were newly created and selects contacts where checkbox is true
Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts Where HPD__c = True) from Account a where a.ID in :accountIds] );

//Moves through the newly created/update accounts one by one
for (Account a : Trigger.new) {

    //Gets the account and sets it to variable
    Account accFromMap = accounts.get(a.Id);
    
    //If the contact exists on the contact, set the value to TRUE, else FALSE
    if (accFromMap.Contacts.size() > 0) {
        
        a.HPD__c = true;
    }
     else {
        
        a.HPD__c = false;
    }

}
}

All Answers

CW5CW5

Okay, I have it working nicely if the contact already exists at the company. I even have it looking for only the contacts that have the HPD checkbox set to true.

 

How do you handle EOF or null records being returned?

 

I receive an error whenever I create a new company, or try and update a company that doesn't have a contact with the HPD box checked.

 

Is there a IF (NO CONTACTS FOUND IN SELECT STATEMENT) then (END TRIGGER or DO NOTHING)??

 

Any ideas?

 

See working code below.

 

-------------------

 

 

trigger UpdateAccountFromContacts on Account(before insert, before update) {

Set<Id> accountIds = new Set<Id>();

for (Account a : Trigger.new)

accountIds.add(a.Id);

Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts Where HPD__c = True) from Account a ] );

for (Account a : Trigger.new) {

Account accFromMap = accounts.get(a.Id);

if (accFromMap.Contacts.size() > 0) {

a.HPD__c = accFromMap.Contacts[0].HPD__c;

}

} }

ahab1372ahab1372

regarding the error:

instead of

if (accFromMap.Contacts.size() > 0) 

 

try

if (accFromMap.Contacts != Null) 

 

 

you cannot aplly the size() method to something that does not exist (is null).

 

Do you want the trigger to fire when an account is updated, or when a contact is inserted/updated? In the latter case, the trigger needs to be on contact instead of Account

CW5CW5

This is the final solution to Update Account based on Contacts, using a trigger...

 

trigger UpdateAccountFromContacts on Account(before update) {

//Creates variable accountIds
Set<Id> accountIds = new Set<Id>();

//Selects all of the accounts that were newly created/updated
for (Account a : Trigger.new)

//Assigns the newly created/updated account IDs to the variable
accountIds.add(a.Id);

//Creates a map of the Accounts that were newly created and selects contacts where checkbox is true
Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts Where HPD__c = True) from Account a where a.ID in :accountIds] );

//Moves through the newly created/update accounts one by one
for (Account a : Trigger.new) {

    //Gets the account and sets it to variable
    Account accFromMap = accounts.get(a.Id);
    
    //If the contact exists on the contact, set the value to TRUE, else FALSE
    if (accFromMap.Contacts.size() > 0) {
        
        a.HPD__c = true;
    }
     else {
        
        a.HPD__c = false;
    }

}
}

This was selected as the best answer