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
123_P123_P 

please suggest example for map and same example using list why we need map and why we need list give me same example on both

Niraj Kr SinghNiraj Kr Singh
Hi Arush,

Please go through this link where explain List, Map and Set comparatively.
https://techilaservices.com/collection-salesforce-example-using-list-map-use-map-list-use-set-map-list/
And let me know if still you have doubt.

Mark your ans

Thanks
Niraj
Ajay K DubediAjay K Dubedi
Hi Aarush,

Both Trigger on Account If Contact MailingCountry is same Account BillingCountry then 
update contact otherPhone number is same account phone number.

Avoid nested for loop therefore map used and also used for code bulkified. 

---Trigger with map---

public class Update_Contact_Phone {
    public static void contactOtherPhone(map<Id,Account> newAccMap){
        set <id> IdCollect = newAccMap.keySet();
        List<Contact> toUpdateconList = new List<Contact>();
        if(IdCollect.size() > 0){
            List <Contact> conList = [SELECT MailingCountry,OtherPhone,AccountId From Contact WHERE AccountId IN :IdCollect];
            if(conList.size()>0){
                for(Contact c:conList){
                    if(c.MailingCountry==newAccMap.get(c.AccountId).BillingCountry){
                        c.OtherPhone = newAccMap.get(c.AccountId).Phone;
                        toUpdateconList.add(c);
                    }
                }
            }
            if(toUpdateconList.size()>0){
                update toUpdateconList;            
            }
        }
    }    
}

----Simple trigger without Map---

public class Update_Contact_Phone {
    public static void contactOtherPhone( List<Account> acList){
        set <id> IdCollect = new Set<Id>();
        List<Contact> toUpdateconList = new List<Contact>();
        for(Account ac :acList){
               if(ac.Id!=Null){
                 IdCollect.add(ac.Id);
                }
        }      
            List <Contact> conList = [SELECT MailingCountry,OtherPhone,AccountId From Contact WHERE AccountId IN :IdCollect];
            for(Account a:acList){
                if(conList.size()>0){
                    for(Contact c:conList){
                        if(c.MailingCountry==a.BillingCountry){
                            c.OtherPhone = a.Phone;
                            toUpdateconList.add(c);
                        }
                    }
                }
            }
            if(toUpdateconList.size()>0){
                update toUpdateconList;            
            }        
    }    
}

Refer this URL:https://developer.salesforce.com/forums/?id=906F0000000DDpWIAW

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi