Apex Trigger Scenario: Based on Child Object change field value in Parent Object

 

I have a custom field on the contact record called connect_america_account__c. When the value of this contact field is true, I would like the CB field value on the related account, also called (connect_america_account__c) to also be true.

Apex Triggers can be divided into two types depending on their time of execution:
1. Before Triggers:
Before triggers are fired before any record is inserted, deleted, updated or validated in any object is saved in the database. These triggers simply run before the record is provided with any id.

2. After Triggers:
After triggers are fired right after any record is inserted, deleted, updated or validated in any object is saved in the database. They are used to perform logic on the related objects, access field values (like CreatedBy, LasteModifiedBy, LastModifiedDate) that are set by the system and make changes in the other records.

Insert: This operation creates a record in the database.
Update: This operation updates/ changes the existing record in the database.
Delete: This operation deletes the record in the database.
Merge: This operation merges the records in the database.
Upsert: This operation updates the existing records and inserts new record in the database.
Undelete: This operation extracts the deletes records from the recycle bin to the database.

I have a custom field on the contact record called connect_america_account__c. When the value of this contact field is true, I would like the CB field value on the related account, also called (connect_america_account__c) to also be true.

 

SUDO CODE TO UNDERSTAND LOGIC
1.Based on connect_america_account__c on contact, make changes connect_america_account__c on account
A. trigger will be written on contact

2. Will it be before or after trigger?
A.After Trigger are used to perform logic on the related objects so it is after trigger

3. How are account and contact related ?
A.They are related based on accountid on contact

Trigger ConnectAmericaUpdate on Contact(after update, after insert){
Set<Id> accidSet = new Set<id>();
Map<id,Boolean> mapAccountContact= new Map<id,Boolean>();
List<Account> lstAccountToUpdate = new List<Account>();
if (Trigger.isInsert) {
for(contact con: Trigger.New){
    if(con.connect_america_account__c!=false && con.accountid!=null){
        mapAccountContact.put(con.accountId,con.connect_america_account__c);
        accidSet.add(con.AccountId);
    } 
}
}

else {
for(contact con: Trigger.New){
    if(con.connect_america_account__c!=false && con.connect_america_account__c!=System.Trigger.oldMap.get(con.id).connect_america_account__c 
    && con.accountid!=null){
        mapAccountContact.put(con.accountId,con.connect_america_account__c);
        accidSet.add(con.AccountId);
    } 
}
}
list<account> accList = [SELECT id, connect_America_Account__c from Account WHERE Id =: accidSet];    
for(Account acc:accList ){
            if(mapAccountContact.containskey(accid))
            acc.connect_america_account__c = true;
            lstAccountToUpdate.add(acc);
        } 
        update lstAccountToUpdate;
}

Apex trigger to update account rating when the opportunity stage equals closed won

trigger updateRatingAccount on Opportunity(after update ) {

Set<id> setaccid = new Set<id>();
List<Account> newacclist = new List<Account>();

for (Opportunity opp : Trigger.new) {
if(opp.StageName=='Closed Won' && opp.accountid !=null
&& opp.StageName!=Trigger.OldMap.get(opp.id).StageName
){
setaccid.add(opp.accountid);
}

List<Account> acclist = [Select id,rating from account where id IN: setaccid];
for(Account acc:acclist){
acc.rating = 'Hot';
newacclist.add(acc);
}
}
update newacclist;
}

https://developer.salesforce.com/forums/?id=906F0000000AUhOIAW

Reference

https://salesforce.stackexchange.com/questions/40884/trigger-on-contact-record-that-updates-related-account-field-based-on-specific-c/327422#327422

https://salesforce.stackexchange.com/questions/10007/trigger-on-updating-account-field-based-on-opp-field/327423#327423

https://salesforce.stackexchange.com/questions/216491/copying-field-from-contact-to-account/327424#327424

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
100% LikesVS
0% Dislikes