I am working through a class that runs on before insert on the OpportunityLineItem object.
Basically, I create a key on the OLI record, search for an existing custom object (Responsible Party) record, if there is no matching key I create a new record and then finally I match and assign the Responsible Party into a lookup field on the OLI. This works in the sandbox where I can populate existing party records or newly created party records.
Where I am stuck is I want to populate the Account Owner into a user lookup field and the Account Owner's Name into a text field. in my method getResParty. I can get the Account Id from a string set but I can't figure out how to query for the account value without doing a query in a for loop or using a get wit the id.
When I try to compile this code I get the error:
Method does not exist or incorrect signature: get(String)
on the line
newResParty.User__c = get(strKey.left(18)).OwnerId;
Where am I going wrong?
public without sharing class NPD_Responsible_Party_Locator
{
public static void filterInsert(List<OpportunityLineItem> newList)
{
List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
Set<String> olikeys = new Set<String>();
Set<Id> oppIds = new Set<Id>();
Set<Id> prodIds = new Set<Id>();
// Create a set of all the unique Opp Ids that we need to query for
for(OpportunityLineItem oliO : newList)
oppIds.add(oliO.OpportunityId);
// Create a map so that Opportunity is locatable by its Id (key)
Map<Id, Opportunity> oppsMap = new Map<Id, Opportunity>(
[SELECT Id, Account.OwnerId, AccountId FROM Opportunity WHERE Id IN :oppIds]);
// Create a set of all the unique Product Ids that we need to query for
for(OpportunityLineItem oliP : newList)
prodIds.add(oliP.Product2Id);
// Create a map so that the Product is locatable by its Id (key)
Map<Id, Product2> prodsMap = new Map<Id, Product2>(
[SELECT Id, Formula_LOB__c, Practice__c, Practice__r.Key_Type__c FROM Product2 WHERE Id IN :prodIds]);
// On Insert we add to all products
for(OpportunityLineItem oppyLine : newList)
{
olikeys.add(oppsMap.get(oppyLine.OpportunityId).AccountId + '-' + prodsMap.get(oppyLine.Product2Id).Practice__c);
olis.add(oppyLine);
}
Map<String, Responsible_Party__c> rmap = getResParty(olikeys);
updateOLI(rmap, olis, oppsMap, prodsMap);
}
public static Map<String, Responsible_Party__c> getResParty(Set<String> olikeys)
{
Map<String, Responsible_Party__c> rmap = new Map<String, Responsible_Party__c>();
for(Responsible_Party__c r : [SELECT Id,
Name,
User__c,
Sector__c,
Key__c
FROM Responsible_Party__c
WHERE Key__c IN : olikeys])
{
rmap.put(r.Key__c, r);
}
//if record does not exists then create the record here
List<Responsible_Party__c> rlst = new List<Responsible_Party__c>();
Set<String> newKeySet = new Set<String>(); //capture Key__c for which records to be created. It will help to fetch newly created records.
for(String strKey : olikeys)
{
if(!rmap.containsKey(strKey))
{
Responsible_Party__c newResParty = new Responsible_Party__c();
newResParty.Key__c = strKey;
newResParty.Name = 'Workflow Field Update';
newResParty.Key_Type__c = 'Account - Practice';
newResParty.Practice__c = strKey.right(18);
newResParty.User__c = get(strKey.left(18)).OwnerId;
newResParty.Team_Name__c = get(strKey.left(18)).Owner.Name;
newResParty.Account__c = strKey.left(18);
rlst.add(newResParty);
newKeySet.add(strKey);
}
}
insert rlst;
//retrieve newly created values and add this to the map
for(Responsible_Party__c r : [SELECT Id,
Name,
User__c,
Sector__c,
Key__c
FROM Responsible_Party__c
WHERE Key__c IN : newKeySet])
{
rmap.put(r.Key__c, r);
}
return rmap;
}
public static void updateOLI(Map<String, Responsible_Party__c> rmap, List<OpportunityLineItem> olis, Map<Id, Opportunity> oppsMap, Map<Id, Product2> prodsMap)
{
Responsible_Party__c r = new Responsible_Party__c();
for(OpportunityLineItem o : olis)
{
r = rmap.get(oppsMap.get(o.OpportunityId).AccountId + '-' + prodsMap.get(o.Product2Id).Practice__c);
if(r != null)
{
o.Responsible_Party__c = r.Id;
}
else{
o.Responsible_Party__c = null;
}
}
}
}
get
like a function, but it needs an instance of a map to be called from, which you use elsewhere in this code (rmap.get(...)
). You should identify what map you want to callget
on, and start there.Map<Id, sObject> someMap = new Map<Id, sObject>([SELECT Id FROM Account WHERE Id IN :someIds]);