You need to sign in to do that
Don't have an account?
Ummul Fazaria
update contact trigger
I am trying to create a trigger where contact description will update when account object description is updated and my code is as below: but i am getting an error "Loop must iterate over collection: Map<Id,Account>"
trigger ContactFieldUpdate on Account (after update) {
Set<ID> setAccId = new Set<ID>();
List<contact> lstConUpdate = new List<Contact>();
for(Account accl : Trigger.new)
{
setAccId.add(accl.id);
}
Map<ID, Account> mapAccount = new Map<ID, Account>([select id,description,(select id,description from contacts ) from account where id in :setAccId ]);
for(Account acc: mapAccount){
for(Contact con : acc.contacts){
con.Description = acc.Description;
lstConUpdate.add(con);
}
}
update lstConUpdate;
}
trigger ContactFieldUpdate on Account (after update) {
Set<ID> setAccId = new Set<ID>();
List<contact> lstConUpdate = new List<Contact>();
for(Account accl : Trigger.new)
{
setAccId.add(accl.id);
}
Map<ID, Account> mapAccount = new Map<ID, Account>([select id,description,(select id,description from contacts ) from account where id in :setAccId ]);
for(Account acc: mapAccount){
for(Contact con : acc.contacts){
con.Description = acc.Description;
lstConUpdate.add(con);
}
}
update lstConUpdate;
}
So you can try changing your snippet to something like below and try:
Edit: I have removed the loop and replaced it with trigger.newmap.keyset() which would be returning the set of id's of new records and used it in the soql, thereby removing a loop.
Let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.
Thanks.
All Answers
So you can try changing your snippet to something like below and try:
Edit: I have removed the loop and replaced it with trigger.newmap.keyset() which would be returning the set of id's of new records and used it in the soql, thereby removing a loop.
Let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.
Thanks.
You can take reference from this below code:-
You are getting the error because you can never iterate the map but yes you can iterate map.keyset() and map.values(). if(acc.contacts.size()>0) --> If you will not use this condition it throws null pointer exception
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Thanks and Regards
Suraj Tripathi
Hi Ummul,
You can also try the below code with Best Practices.
Don't forget to mark it as the Best Answer if your query is solved.
If you have any issues please tell
Thank You