You need to sign in to do that
Don't have an account?
Shruthi MN 88
Write a map
HI Everyone,
I have written the below code :
Can you write map instead of a list . Instead of two for loops pass maps:
public class HelperAccount{
public static void helpaccmethod(List<account> oldAccount, List<account> newAccount){
List<contact> conList=[select lastname,otherphone,accountid,phone from contact
where accountid IN: newAccount];
map<id,string> oldAccidVsPhone= new map<id,string>();
map<id,string> newAccidVsPhone= new map<id,string>();
for(account newAcc: newAccount){
for(account oldAcc: oldAccount){
if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id){
oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
newAccidVsPhone.put(newAcc.id,newAcc.phone);
}
}
}
list<contact> updateContactList= new List<contact>();
for(contact con: conList){
if(oldAccidVsPhone.containskey(con.accountid)){
con.phone=oldAccidVsPhone.get(con.accountid);
con.Phone=newAccidVsPhone.get(con.accountid);
updateContactList.add(con);
}
}
if(updateContactList.size()>0){
update updateContactList;
}
}
}
Trigger:
trigger UpdateAccount on Account (before update,after update) {
if(trigger.isUpdate && trigger.isBefore){
HelperAccount.helpaccmethod(trigger.old,trigger.new);
}
}
I have written the below code :
Can you write map instead of a list . Instead of two for loops pass maps:
public class HelperAccount{
public static void helpaccmethod(List<account> oldAccount, List<account> newAccount){
List<contact> conList=[select lastname,otherphone,accountid,phone from contact
where accountid IN: newAccount];
map<id,string> oldAccidVsPhone= new map<id,string>();
map<id,string> newAccidVsPhone= new map<id,string>();
for(account newAcc: newAccount){
for(account oldAcc: oldAccount){
if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id){
oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
newAccidVsPhone.put(newAcc.id,newAcc.phone);
}
}
}
list<contact> updateContactList= new List<contact>();
for(contact con: conList){
if(oldAccidVsPhone.containskey(con.accountid)){
con.phone=oldAccidVsPhone.get(con.accountid);
con.Phone=newAccidVsPhone.get(con.accountid);
updateContactList.add(con);
}
}
if(updateContactList.size()>0){
update updateContactList;
}
}
}
Trigger:
trigger UpdateAccount on Account (before update,after update) {
if(trigger.isUpdate && trigger.isBefore){
HelperAccount.helpaccmethod(trigger.old,trigger.new);
}
}
While calling the method from trigger you can pass Trigger.NewMap, Trigger.OldMap instead of trigger.old,trigger.new.
Regards
Atosi
HI
I have added maps biut I am getting the attached errors
public class HelperAccount{
public static void helpaccmethod(){
map<id,string> oldAccount = new map<id,string>();
map<id,string> newAccount = new map<id,string>();
List<contact> conList=[select lastname,otherphone,accountid,phone from contact
where accountid IN: newAccount];
map<id,string> oldAccidVsPhone= new map<id,string>();
map<id,string> newAccidVsPhone= new map<id,string>();
for(account newAcc: newAccount){
for(account oldAcc: oldAccount){
if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id){
oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
newAccidVsPhone.put(newAcc.id,newAcc.phone);
}
}
}
list<contact> updateContactList= new List<contact>();
for(contact con: conList){
if(oldAccidVsPhone.containskey(con.accountid)){
con.phone=oldAccidVsPhone.get(con.accountid);
con.Phone=newAccidVsPhone.get(con.accountid);
updateContactList.add(con);
}
}
if(updateContactList.size()>0){
update updateContactList;
}
}
}
Trigger
trigger UpdateAccount on Account (before update,after update) {
if(trigger.isUpdate && trigger.isBefore){
HelperAccount.helpaccmethod(trigger.oldmap,trigger.newmap);
}
}
Try using below code: If this information helps, please mark the answer as best. Thank you