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
David SmithDavid Smith 

Updating flag on Account when Contact is edited via Trigger

Hi everyone,

 

I'm new to Apex coding, so please forgive me if this question is amazingly basic.

 

I'm trying to build a trigger that fires when a contact record is edited. It should look at the value of a checkbox on the contact and use that to update the value of a checkbox on the contacts Account. 

 

This is what I have currently:

 

trigger Update_Powerful on Contact (before update, before insert) {

//When contact Powerful connection checkbox is changed, update Account field

    // Loop through the incoming records
     for (Contact c : Trigger.new) {

        //Get account record
      Account a =c.Account;
           

        //Is Powerful connections ticked?
        
     a.Consultative_Selling_Partner__c = c.Powerful_Connections_Presentation_check__c;
            
            

   }


}

 

The error I receive on the contact when updating is:

Error:Apex trigger Update_Powerful caused an unexpected exception, contact your administrator: Update_Powerful: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Email: Trigger.Update_Powerful: line 28, column 44

 

 

To give you an understanding of my overall goal with this, I want my users to tick a box on the event screen. Once that event is saved, if the box is ticked it updates the contact, which in turn updates the account. I've started with contact, since that seems to be an easier entry point to trigger design.

 

I guess my understanding falls down when I try to reference the account that's related to the contact, and edit that accounts fields. If anyone could point me int he right direction, I'd really appreciate it :)

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

Hey David - Try the code below and it should work (Sorry if there would be extra indents of spaces coz I needed to change the formatting to make sure the code was readable)

 

trigger Update_Powerful on Contact (before update, before insert)

{   

List<Account> accForUpdate = new List<Account>();       

for(Contact c : Trigger.New)

{

If

(

Trigger.IsInsert ||

(

Trigger.IsUpdate &&

(

Trigger.oldMap.get(c.Id).Powerful_Connections_Presentation_check__c !=Trigger.newMap.get(c.Id).Powerful_Connections_Presentation_check__c

)

)

)

{           

accForUpdate.add

(

new Account

(

Id = c.AccountId,

Consultative_Selling_Partner__c = c.Powerful_Connections_Presentation_check__c

)

);       

}   

}       

 

if(accForUpdate != NULL && accForUpdate.size() > 0)

{       

update accForUpdate;

}    

}

All Answers

SwarnasankhaSwarnasankha

Hey David - Try the code below and it should work (Sorry if there would be extra indents of spaces coz I needed to change the formatting to make sure the code was readable)

 

trigger Update_Powerful on Contact (before update, before insert)

{   

List<Account> accForUpdate = new List<Account>();       

for(Contact c : Trigger.New)

{

If

(

Trigger.IsInsert ||

(

Trigger.IsUpdate &&

(

Trigger.oldMap.get(c.Id).Powerful_Connections_Presentation_check__c !=Trigger.newMap.get(c.Id).Powerful_Connections_Presentation_check__c

)

)

)

{           

accForUpdate.add

(

new Account

(

Id = c.AccountId,

Consultative_Selling_Partner__c = c.Powerful_Connections_Presentation_check__c

)

);       

}   

}       

 

if(accForUpdate != NULL && accForUpdate.size() > 0)

{       

update accForUpdate;

}    

}

This was selected as the best answer
David SmithDavid Smith

That did exactly what I wanted it to do, thanks a lot. I also used it for the event process, and it worked there as well.

 

Part of the functionality of this code is that it will turn the flag off on the account if the contact is edited to turn theirs off. Is there any way to do someting like a SOQL query in the code to check whether any other contacts on the account have their flag set to yes, and if so not run the update on the account?

 

Currently it's:

Consultative_Selling_Partner__c = c.Powerful_Connections_Presentation_check__c

 

But I'm thinking something along the lines of:

 

if (c.Powerful_Connections_Presentation_check__c =TRUE)

{Consultative_Selling_Partner__c = TRUE},

else if ( (count (select contactid from account where accountId = Id and Powerful_Connections_Presentation_check__c = TRUE))>0)

{Consultative_Selling_Partner__c = TRUE}

 

 

Hopefully what I've put there makes sense.