You are on page 1of 3

Step 1: Map on Parent Side itrate the trigger and put the Parent Data.

Step 2:List<Child> SOQL over Child Object


Step 3:Itrate above List<child> check in Map whether or childs parent id available.
if yes then procced the bussines logic.
# When Account Type is technology Partner the update the all releated contact to
LeadSource 'Purchased List'.

trigger AccountToTypeToLeadSource on Account (before update) {


Map<Id,Account>accMap=new Map<Id,Account>();
if(trigger.Isupdate){
for(Account acc:trigger.new){
if(acc.Type=='Technology Partner'||acc.Type=='Prospect'||acc.Type=='Other'
&& acc.Type!=trigger.OldMap.get(acc.Id).Type){
accMap.put(acc.Id, acc);
}
}
}
List<contact> conList=[select Id,AccountId from contact where AccountId
in:accMap.keySet()];
if(conList.size()>0){
for(contact con:conList){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId).Type=='Technology Partner'){
con.LeadSource='Purchased List';
}
if(accMap.get(con.AccountId).Type=='Prospect'){
con.LeadSource='Web';
}
if(accMap.get(con.AccountId).Type=='Other'){
con.LeadSource='Other';
}

if(accMap.get(con.AccountId).SLA__c=='Silver'){
con.Level__c='Secondary';
}
}
}
}
if(!conList.isEmpty())
Database.update(conList,false);
}
---------------------------------------------------------------------------------
#create a trigger Rating is Hot then LeadSource Is Web.
trigger AccountToContactHotTrigeer on Account (after update) {
Map<Id,Account> accMap=new Map<Id,Account>();
for(Account acc:trigger.new){
if(trigger.Isupdate && acc.rating=='Hot'&& acc.rating!
=trigger.OldMap.get(acc.id).rating ){
accmap.put(acc.Id, acc);
}
}
List<contact>conList=[select Id,AccountId from contact where AccountId
in:accMap.keySet()];
if(conList.size()>0){
for(contact con:conList){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId).rating=='Hot'){
con.LeadSource='Web';
}
}
}
}
if(!conList.isEmpty())
Database.update(conList,false);
}

-----------------------------------------------------------------------------------
-----
# Child to Parent
trigger ContactToAccountSLATrigger on Contact (after insert, after update){
Set<Id> accIDSet=new Set<Id>();
for(contact con:trigger.new){
if(trigger.Isinsert && con.Level__c!=null && con.AccountId!=null){
accIDSet.add(con.AccountId);
}
if(trigger.Isupdate && con.Level__c!=null && con.AccountId!=null){
if(trigger.OldMap.get(con.Id).Level__c!=con.Level__c){
accIDSet.add(con.AccountId);
}
}
}
Map<Id,Account> accMap=new Map<Id,Account>();
for(Account a:[select Id,SLA__c from Account where Id in:accIDSet]){
accMap.put(a.Id,a);
}
List<Account> newUpdateList= new List<Account>();
if(!accMap.isEmpty()){
for(contact con:trigger.new){
if(accMap.containsKey(con.AccountId)){
Account a=new Account();
a.Id=con.AccountId;
if(con.Level__c=='Primary'){
a.SLA__c='Gold';
}
if(con.Level__c=='Secondary'){
a.SLA__c='Silver';
}
if(con.Level__c=='Tertiary')
{
a.SLA__c='Platinum';
}
newUpdateList.add(a);
}
}
}
if(!newUpdateList.isEmpty())
Database.update(newUpdateList);
}
-----------------------------------------------------------------------------------
---------
#Child To Parent
trigger TriggerOptyUpdateAccount on Opportunity (after insert,after update) {
Set<Id> accIDSet=new Set<Id>();
for( Opportunity Opp:trigger.new){
if(trigger.Isinsert && Opp.StageName!=null && Opp.Type!=null){
accIDSet.add(Opp.AccountId);
}
if(trigger.Isupdate && Opp.StageName!=null && Opp.Type!=null){
if(trigger.OldMap.get(Opp.Id).StageName!=Opp.StageName &&
trigger.OldMap.get(Opp.Id).Type!=Opp.Type){
accIDSet.add(Opp.AccountId);
}
}
}

Map<Id,Account> accMap=new Map<Id,Account>();


for(Account a:[select id,Rating from Account where id in:accIDSet]){
accMap.put(a.Id, a);
}
List<Account> accUpdateList=new List<Account>();
for(Opportunity Opp:trigger.new){
if(accMap.containsKey(Opp.AccountId)){
Account acc=new Account();
acc.Id=Opp.AccountId;
acc.Description=Opp.Description;
acc.AnnualRevenue=Opp.Amount;
if(Opp.StageName=='Closed Won' && Opp.Type=='New Customer'){
acc.Rating='Hot';
acc.Type='Other';
acc.Industry='Banking';
}
if(Opp.StageName=='Closed Lost' && Opp.Type=='Existing Customer -
Upgrade'){
acc.Rating='Cold';
acc.Type='Prospect';
acc.Industry='Finance';
}
accUpdateList.add(acc);
}
}
if(!accUpdateList.isEmpty())
Database.update(accUpdateList,false);
}
-----------------------------------------------------------------------------------
--------------------

You might also like