Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
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
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