You need to sign in to do that
Don't have an account?

avoid below loop inside loop using map
Hello everyone,
Can any one explain me, how to avoid below loop inside loop using map , can any one explain if i would write bellow code, will it get Hit by governer limits. Thanks Advance.
Thanks
Can any one explain me, how to avoid below loop inside loop using map , can any one explain if i would write bellow code, will it get Hit by governer limits. Thanks Advance.
for(String acct : acc) { List<String> contlist = new List<String>(); for(Contact c : trigger.new) { if(acct == c.AccountId) contlist.add(c.id); } }
Thanks
We can use the Map over list to avoide query inside for loop with above example
If we use List and not Map Same Trigger using the Map:
Please let us know if this will help you
All Answers
Below is the way you can avoid the for loop.
Map<id,Account> AccountMap = new Map<id,Account>([select id,name from account LIMIT 50000]);
List<String> contlist = new List<String>();
for(Contact c : trigger.new) {
if(AccountMap.containsKey(c.accountId))
contlist.add(c.id);
}
Mark it as answer if it solves your query.
Thanks
Manoj S
We can use the Map over list to avoide query inside for loop with above example
If we use List and not Map Same Trigger using the Map:
Please let us know if this will help you
I got so much clearity with your example code regarding looping. Amit, thanks for your great examples,
But i still have one doubt. if we would have written below code. does it hit governer limits. if answer is yes . could you please tel us
how can we handle that situation
Thanks