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

This is a standard “aggregate-query-update” style trigger.

  • First, we gather all accounts that have changed,
  • Then we query for all contacts for those accounts and see if they matched the old values, and if so, add them to a list to update,
  • Finally we update the records.

Q. I want to write trigger to enable a checkbox in contact whenever its Account is modified.

UNDERSTAND LOGIC
1.Based on connect_america_account__c on account, make changes connect_america_account__c on contact
A. trigger will be written on account
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
4. Which trigger context variable will be used?
update

trigger UpdateConnectAmericaTrigger on Account (after update){
List<Contact> newCList = new List<Contact>();
Set<Id> setaccountId = new Set<Id>();

for(Account acc : Trigger.new)
{
if(acc.connect_america_account__c!=Trigger.oldMap.get(acc.id).connect_america_account__c
&& acc.connect_america_account__c == true
){
setaccountId.add(acc.id);
}
}

List<Contact> conlist =[Select id, connect_america_account__c,accountid from contact where accountid IN: setaccountId];
for(contact c: conlist){
c.connect_america_account__c = true;
newClist.add(c);
}
update newClist;
}

Q.How to write trigger to change contact phone number when account phone number change?

trigger UpdatephoneTrigger on Account (after update) {
set<id> AccounidSet = new set<id>();
list<contact> contoUpdate=new list<contact>();
for(Account acc:trigger.new)
{
// Account oldphone = trigger.oldmap.get(acc.Id);
if(acc.phone!= trigger.oldmap.get(acc.Id).phone){
AccounidSet .add(acc.id);
}
}
for(contact con :[select id,Phone,Account.phone from contact where Accountid IN: AccounidSet ]){
for(Account acc:trigger.new){
if(con.AccountId==acc.id){
con.Phone=acc.phone;
contoUpdate.add(con);
}
}
}
update contoUpdate;
}

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

Q.Account and Contact both have Billing_Address__c field. Contact also have checkbox called active__c. If active__c is true and Account Billing_Address__c is update, then Contact’s Billing_Address__c is updated

UNDERSTAND LOGIC
1.Based on Billing_Address__c on account and active__c on contact, make changes Billing_Address__c on contact
A. trigger will be written on account
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
4. Which trigger context variable will be used?
update

Trigger updateBillingAddressTrigger on Account(after update){
Set<id> accIdSet = new accIdSet();
List<id> ccNewList = new ccNewList();
for(account acc:trigger.new){
if(acc.Billing_Address__c != null && acc.Billing_Address__c!=trigger.oldMap.get(acc.id).Billing_Address__c)
accIdSet.add(acc.id);
}
for(Contact cclist: [Select id,Billing_Address__c,Account.Billing_Address__c,active__c from Contact where accountId IN: accIdSet]){
if(ccList.accountId =acc.id && cclist.active__c ==true){
ccList.Billing_Address__c = cclist.Account.Billing_Address__c;
ccNewList.add(ccList);
}
else
{
con.Billing_Address__c = null;
ccNewList.add(con);
}
}
update ccNewList;
}

https://developer.salesforce.com/forums/?id=9060G000000IBknQAG

Trigger on Account to update contact(whenever account billingaddress is updated contact mailing address also gets updated)

trigger updateContact on Account (After update) {

list<account> accList = [select id,name, billingCity,(select id, MailingCity from contacts) from account where id in :trigger.newMap.keyset()];
list<contact> conList = new list<contact>();
for(account acc: accList){
if(trigger.oldMap.containskey(acc.id) && trigger.newMap.containskey(acc.id) && trigger.oldMap.get(acc.id).billingCity != trigger.newMap.get(acc.id).billingCity){
for(contact c:acc.contacts){
c.MailingCity = trigger.newMap.get(acc.id).billingCity;
conList.add(c);
}
}
}
update conList;
}

https://salesforce.stackexchange.com/questions/117600/trigger-on-account-to-update-contactwhenever-account-billingaddress-is-updated

Update phone field on contacts from the account field for existing records:

1.Iterate your new account records (preferably while checking if the phone number was actually modified, using the Trigger.oldMap map), get their ids to a set or list.
2.Get the contacts that refer to the selected accounts.
3.Iterate the contacts and assign the account phone number to the contacts.
Update the contacts.

trigger updatephonefield on account (before update ) {
Set<id> setaccid = new Set<id>();
List<Contact> newclist = new List<Contact>();
for (Account acc : Trigger.new) {
if ( acc.Phone != null && acc.phone != trigger.oldmap.get(acc.id).phone) {
setaccid.add(acc.id);
}

List<Contact> clist = [Select id,phone from contact where accountid IN: setaccid];
for(contact cp:clist){
cp.Phone = acc.Phone;
newclist.add(cp);
}
}
update newclist;
}

https://developer.salesforce.com/forums/?id=906F0000000fydeIAA
https://salesforce.stackexchange.com/questions/41701/update-contact-phone-with-the-account-phone/327522#327522
https://salesforce.stackexchange.com/questions/222590/get-values-of-the-records-from-trigger-newmap-in-before-update-triggers/327524#327524

Create a trigger to auto create a Contact when there is a new Account

trigger newContactTrigger on Account(after insert){
List<Contact> clist = new List<Contact>();
for(Account acc: trigger.new){
contact c = new contact();
c.accountid = acc.id;
c.LastName = acc.name;
clist.add(c);
}
insert clist;
}

https://salesforce.stackexchange.com/questions/88467/error-with-trigger-createaccountcontact/327523#327523

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 )
50% LikesVS
50% Dislikes