function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shruthi MN 88Shruthi 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);
    }
}
Atosi MalakarAtosi Malakar
Hello Shruthi,
While calling the method from trigger you can pass Trigger.NewMap, Trigger.OldMap instead of trigger.old,trigger.new.

Regards
Atosi
Shruthi MN 88Shruthi MN 88
How to passs maps instead of lists?
Shruthi MN 88Shruthi MN 88

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);
    }
}User-added image

AshwiniAshwini (Salesforce Developers) 
Hi Shruthi,

Try using below code:
public class HelperAccount {
    public static void helpaccmethod(Map<Id, Account> oldAccountMap, Map<Id, Account> newAccountMap) {
        Map<Id, String> oldAccidVsPhone = new Map<Id, String>();
        Map<Id, String> newAccidVsPhone = new Map<Id, String>();
        
        for (Account newAcc : newAccountMap.values()) {
            Account oldAcc = oldAccountMap.get(newAcc.Id);
            if (oldAcc != null && newAcc.Phone != oldAcc.Phone) {
                oldAccidVsPhone.put(oldAcc.Id, oldAcc.Phone);
                newAccidVsPhone.put(newAcc.Id, newAcc.Phone);
            }
        }
        
        List<Contact> updateContactList = new List<Contact>();
        List<Contact> conList = [SELECT Id, AccountId, Phone FROM Contact WHERE AccountId IN :newAccountMap.keySet()];
        
        for (Contact con : conList) {
            if (oldAccidVsPhone.containsKey(con.AccountId)) {
                con.Phone = oldAccidVsPhone.get(con.AccountId);
                con.OtherPhone = newAccidVsPhone.get(con.AccountId);
                updateContactList.add(con);
            }
        }
        
        if (!updateContactList.isEmpty()) {
            update updateContactList;
        }
    }
}
 
trigger UpdateAccount on Account (before update, after update) {
    if (Trigger.isUpdate && Trigger.isBefore) {
        HelperAccount.helpaccmethod(Trigger.oldMap, Trigger.newMap);
    }
}
If this information helps, please mark the answer as best. Thank you