You need to sign in to do that
Don't have an account?
inbox outbox 7
We can do the below code with LIST as well but that would give us an error whenever we are INSERTing more than 1 Contact and so we used MAP.
Whenever a contact is inserted, if that contact is associated with an account, then update that account name from this newly inserting contact name. It has to be bulkified. If the contact name is "John", then the associated account's name should also be "John:
public Contact trigger updateAccount (AFTER INSERT){
Map<Id, Account> accountList = New Map<Id, Account> ();
for(Contact con: Trigger.New){
if(con.AccountId){
Account acc = New Account();
acc.Name = con.LastName;
acc.Id = con.AccountId;
accountList.put(acc.Id, acc);
}
}
UPDATE accountList.values();
}
I would like to know why we would get an error if we use LIST in production and that we have to use either MAP or SET.
public Contact trigger updateAccount (AFTER INSERT){
Map<Id, Account> accountList = New Map<Id, Account> ();
for(Contact con: Trigger.New){
if(con.AccountId){
Account acc = New Account();
acc.Name = con.LastName;
acc.Id = con.AccountId;
accountList.put(acc.Id, acc);
}
}
UPDATE accountList.values();
}
I would like to know why we would get an error if we use LIST in production and that we have to use either MAP or SET.
Your code is correct can you please check size of map and pelase share error that's you are facing
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
Can you also share the code with List so experts can help you why the issue is with the List and why not with the map.
Thanks
All we have to do is replace map with list and add map methods.
public Contact trigger updateAccount (AFTER INSERT){
List< Account> accountList = New List<Account> ();
for(Contact con: Trigger.New){
if(con.AccountId){
Account acc = New Account();
acc.Name = con.LastName;
acc.Id = con.AccountId;
accountList.add(acc);
}
}
UPDATE accountList ;
}
Your code is correct can you please check size of map and pelase share error that's you are facing
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh