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 field value on the opportunity, also called (connect_america_account__c) to also be true.
UNDERSTAND LOGIC
1.Based on connect_america_account__c on contact, make changes connect_america_account__c on opportunity
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 opportunity and contact related ?
A.They are related based on accountid on contact and opportunity
Trigger ConnectAmericaUpdateOppTrigger on Contact(after update){ Set<id> setAccId = new Set<id>(); List<Opportunity> oppNewList = New List<Opportunity>(); for(contact c: Trigger.New) { if(c.connect_america_account__c!=false && c.connect_america_account__c!=Trigger.OldMap.get(c.id).connect_america_account__c && c.accountid !=null ){ setAccId.add(c.accountid); } } List<Opportunity> oppList = [select id,connect_america_account__c from Opportunity where AccountId IN: setAccId]; if (oppList .size() > 0) { for(Opportunity opp: oppList){ opp.connect_america_account__c = true; oppNewList.add(opp); } } update oppNewList; }
Points Noted:
Avoid using SOQL queries inside of for loops. Best practice is to do your queries outside of the loops and then loop over the returned records. You can still filter your queries by the ids in your trigger map or list by doing Opportunity.id IN :Trigger.new
When the value of contact field connect_america_account__c is true, I would like the field value on the opportunity, account also called (connect_america_account__c) to also be true.
If you notice, this time we want to update both opportunity and account so let’s see:
Trigger ConnectAmericaUpdateOppTrigger on Contact(after update){ Set<id> setAccId = new Set<id>(); List<Opportunity> oppNewList = New List<Opportunity>(); List<Account> accNewList = New List<Account>(); for(contact c: Trigger.New) { if(c.connect_america_account__c!=false && c.connect_america_account__c!=Trigger.OldMap.get(c.id).connect_america_account__c && c.accountid !=null ){ setAccId.add(c.accountid); } } List<Account> acclist = [select id,connect_america_account__c,(Select id,connect_america_account__c from Opportunities) from Account where Id IN: setAccId]; if (acclist.size() > 0) { for(Account acc: acclist){ for(Opportunity opp: acc.Opportunities){ acc.connect_america_account__c = true; opp.connect_america_account__c = true; accNewList.add(acc); oppNewList.add(opp); } } } update oppNewList; update accNewList; }
Point to Note
Looping thorugh SOQL Subquery
for ( Account acc: [ SELECT Id, connect_america_account__c,(SELECT id,connect_america_account__c FROM Opportunities ) FROM Account ] ) { for ( Opportunity opp : acc.Opportunities ) { }
Reference:
https://developer.salesforce.com/forums/?id=906F000000090bvIAA
https://developer.salesforce.com/forums/?id=906F00000008xhQIAQ
https://developer.salesforce.com/forums/?id=906F0000000BR3aIAG