You need to sign in to do that
Don't have an account?
DaNae Peterson
Incompatible key type
Hi all,
We have a 3rd party app that moves all opportunities on a child account to the parent account every hour. We do not want this to happen so I am trying to write an after update trigger that will move the opportunity back to the previous/child account. I am running into trouble though with my code. I am getting the following error: "Incompatible key type Opportunity for Map<Id,Opportunity>". I am also getting the error "Variable o does not exist" -- I think one of my }s is off (both of these errors are happening on line 13). Below is my code. Can someone please look at it and figure out where I went wrong and shed some light on how to fix it?
Thanks!
trigger SwitchBackAccount on Opportunity (after update) {
map<id,Opportunity> changeAcctId = new map<id,Opportunity>();
List<Opportunity> SROpps = new List<Opportunity>();
for(Opportunity o : trigger.new){
if(trigger.oldmap.get(o.id).AccountId != o.AccountId){
changeAcctId.put(o.id,o);
}
}
List<Opportunity> opps = [select Id, AccountId from Opportunity where Id in :changeAcctId.keyset()];
if(opps <> NULL && changeAcctId.size() > 0){
for(Opportunity op : opps){
op.AccountId = changeAcctId.get(o).AccountId;
opps.add(op);
}
update SROpps;
}
}
We have a 3rd party app that moves all opportunities on a child account to the parent account every hour. We do not want this to happen so I am trying to write an after update trigger that will move the opportunity back to the previous/child account. I am running into trouble though with my code. I am getting the following error: "Incompatible key type Opportunity for Map<Id,Opportunity>". I am also getting the error "Variable o does not exist" -- I think one of my }s is off (both of these errors are happening on line 13). Below is my code. Can someone please look at it and figure out where I went wrong and shed some light on how to fix it?
Thanks!
trigger SwitchBackAccount on Opportunity (after update) {
map<id,Opportunity> changeAcctId = new map<id,Opportunity>();
List<Opportunity> SROpps = new List<Opportunity>();
for(Opportunity o : trigger.new){
if(trigger.oldmap.get(o.id).AccountId != o.AccountId){
changeAcctId.put(o.id,o);
}
}
List<Opportunity> opps = [select Id, AccountId from Opportunity where Id in :changeAcctId.keyset()];
if(opps <> NULL && changeAcctId.size() > 0){
for(Opportunity op : opps){
op.AccountId = changeAcctId.get(o).AccountId;
opps.add(op);
}
update SROpps;
}
}
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!
All Answers
Please try following code:
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!
SwitchBackAccount: execution of AfterUpdate caused by: System.FinalException: Cannot modify a collection while it is being iterated.: Trigger.SwitchBackAccount: line 12, column 1
Any thoughts?
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!
As we cann't perform DMl operation on same Object on which trigger is written so please use following code:
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!