0

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;
        }
    }
}
}
  • You seem to be using 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 call get on, and start there. – battery.cord Aug 28 '17 at 20:03
  • You can also skip some space when making maps of sObjects by calling the soql like this: Map<Id, sObject> someMap = new Map<Id, sObject>([SELECT Id FROM Account WHERE Id IN :someIds]); – battery.cord Aug 28 '17 at 20:05
2

Find my comments in the code.

Step 1: Retrieve Account Information from this SOQL

Map<Id, Opportunity> oppsMap = new Map<Id, Opportunity>(
        [SELECT Id, Account.OwnerId, Account.Owner.Name , AccountId FROM Opportunity WHERE Id IN :oppIds]);

Step 2: Then store them into oliAccountMap like this:

// On Insert we add to all products
    for(OpportunityLineItem oppyLine : newList)
    {
        olikeys.add(oppsMap.get(oppyLine.OpportunityId).AccountId + '-' + prodsMap.get(oppyLine.Product2Id).Practice__c);
        //put information respective to OLI.
        oliAccountMap.put(oppyLine.Id, oppsMap.get(oppyLine.OpportunityId).Account.OwnerId + '-' + oppsMap.get(oppyLine.OpportunityId).Account.Owner.Name);
        olis.add(oppyLine);
    }

Step 3: Pass oliAccountMap as a parameter of getResParty method.

Step 4: Then retrieve elements from oliAccountMap

String[] strAccountInfo = oliAccountMap.get(strKey).split('-');

 newResParty.User__c = strAccountInfo[0];  //Account's OwnerId
 newResParty.Team_Name__c = strAccountInfo[1]; //Account's Owner Name

Entire code

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>();

    Map<String, String> oliAccountMap = new Map<String, String>();

    // 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, Account.Owner.Name , 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);
        //put information respective to OLI.
        oliAccountMap.put(oppyLine.Id, oppsMap.get(oppyLine.OpportunityId).Account.OwnerId + '-' + oppsMap.get(oppyLine.OpportunityId).Account.Owner.Name);
        olis.add(oppyLine);
    }

    Map<String, Responsible_Party__c> rmap = getResParty(olikeys,oliAccountMap);
    updateOLI(rmap, olis, oppsMap, prodsMap);
}

public static Map<String, Responsible_Party__c> getResParty(Set<String> olikeys, Map<String, String> oliAccountMap)
{
    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,
                                            OwnerId,
                                            Owner.Name,
                                  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);
            String[] strAccountInfo = oliAccountMap.get(strKey).split('-');

            newResParty.User__c = strAccountInfo[0];  //Account's OwnerId
            newResParty.Team_Name__c = strAccountInfo[1]; //Account's 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;
        }
    }
}
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.