I want to setup logic in a before update method where if an Opportunity's AccountId is changed two fields on the Opportunity is updated based on the new Account record.
I found a bunch of examples, one here, where example code is combined into one large method. As I have been trying to separate my concerns I've tried to pull apart each piece into it's own so to better test and understand what is going on.
My question is how do I correctly write the update method to reference a map of Opportunities and Accounts.
Currently, I get the compile error:
Loop variable must be of type Id
On this line: for(Opportunity oppy : filtered.keyset())
public static Map<Id, Id> updatePracticeAndParentAccountFilter(Map<Id, Opportunity> oldMap, Map<Id, Opportunity> newMap)
{
Map<Id, Id> oppys = new Map<Id, Id>();
for(Opportunity o : newmap.values())
{
if(
o.AccountId != oldMap.get(o.Id).AccountId &&
o.AccountId != null
)
oppys.put(o.Id, o.AccountId);
}
return oppys;
}
public static void queryAccountupdateOpportunity(Map<Id, Id> filtered)
{
Map<Id, Account> accMap = new Map<Id, Account>();
accMap.putAll([SELECT Id,
Practice__c,
ParentId
FROM Account
WHERE Id IN : filtered.values()]);
for(Opportunity oppy : filtered.keyset())
{
Account a = AccMap.get(oppy.AccountId);
oppy.Practice__c = a.Practice__c;
oppy.Parent_Account__c = a.ParentId;
}
}