trigger ChildToParentUpdate on Contact (After insert,after update) {
Set<Id>Setacc=new Set<Id>();
map<id,List<Contact>>mapcc=new map<id,List<Contact>>();
List<Account>ListAcc=new List<Account>();
for(Contact cc:trigger.new){
Setacc.add(cc.accountid);
}
List<account>listObj=[select id,name,phone from account where id in:Setacc];
List<Contact>ListObj1=[Select id,accountid,name,mobilephone from Contact where accountid in:Setacc];
for(Id TempId:Setacc){
List<Contact> TempContList = new List<Contact>();
for(Contact cc1:ListObj1){
if(cc1.accountid ==TempId){
TempContList.add(cc1);
}
}
mapcc.put(TempId, TempContList );
}
for(Account Acct : listObj) {
List<Contact> TempContList = new List<Contact>();
TempContList=mapcc.get(Acct.id);
for(Contact ccs:TempContList){
Acct.Phone= ccs.MobilePhone;
System.debug('ListAcc1--->'+ccs.MobilePhone);
System.debug('ListAcc2--->'+Acct.Phone);
ListAcc.add(Acct);
}
}
if(ListAcc.size()>0){
update ListAcc;
System.debug('ListAcc--->'+ListAcc.size());
}
}
Set<Id>Setacc=new Set<Id>();
map<id,List<Contact>>mapcc=new map<id,List<Contact>>();
List<Account>ListAcc=new List<Account>();
for(Contact cc:trigger.new){
Setacc.add(cc.accountid);
}
List<account>listObj=[select id,name,phone from account where id in:Setacc];
List<Contact>ListObj1=[Select id,accountid,name,mobilephone from Contact where accountid in:Setacc];
for(Id TempId:Setacc){
List<Contact> TempContList = new List<Contact>();
for(Contact cc1:ListObj1){
if(cc1.accountid ==TempId){
TempContList.add(cc1);
}
}
mapcc.put(TempId, TempContList );
}
for(Account Acct : listObj) {
List<Contact> TempContList = new List<Contact>();
TempContList=mapcc.get(Acct.id);
for(Contact ccs:TempContList){
Acct.Phone= ccs.MobilePhone;
System.debug('ListAcc1--->'+ccs.MobilePhone);
System.debug('ListAcc2--->'+Acct.Phone);
ListAcc.add(Acct);
}
}
if(ListAcc.size()>0){
update ListAcc;
System.debug('ListAcc--->'+ListAcc.size());
}
}
No comments:
Post a Comment