1

I am working on a before insert class that searches for a custom object record and populates it on the OpportunityLineItem. For a while this morning, it was not working. However, I found this post from Jeff Douglas that solved why my class was not working: http://blog.jeffdouglas.com/2010/02/23/relationship-lookup-objects-in-triggers-are-null/ The post describes that lookups are null on isnew triggers and that you must query the object to get the values.

I did this in my method and my debug statements shows that it works. The problem is I think I need to do the same thing it again in a later method. It doesn't make sense to me that I would need to make two queries again to get record info that I already have queried earlier in the process.

I have two questions: * What am I doing wrong in my updateOLI method because as it's written it's not finding a match against my concatenation. * Is there a way to reuse maps from one method to another?

Example:

public static void filterInsert(List<OpportunityLineItem> newlist)
{
    Set<String> olikeys = new Set<String>();
    List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
    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 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, Practice__c FROM Product2 WHERE Id IN :prodIds]);

    // On Insert we add to all products
    for(OpportunityLineItem oppyLine : newList)
    {
        olikeys.add(oppsMap.get(oppyLine.OpportunityId).Account.OwnerId + '-' + prodsMap.get(oppyLine.Product2Id).Practice__c);
        olis.add(oppyLine);
    }

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

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);
    }
    return rmap;

}

public static void updateOLI(Map<String, Responsible_Party__c> rmap, List<OpportunityLineItem> olis)
{
    for(OpportunityLineItem o : olis)
    {
        Responsible_Party__c r = rmap.get(o.Opportunity.Account.OwnerId + '-' + o.Product2.Practice__c);

        If(r != null)
        {
            o.Responsible_Party__c = r.Id;
        }
        //else{
        //  o.Responsible_Party__c = null;
        //}
    }
}

1 Answer 1

1

You can pass parameters from one method to the next. So, in filterInsert, you can take out olis (the OpportunityLineItem list):

for(OpportunityLineItem oppyLine : newList)
{
    olikeys.add(oppsMap.get(oppyLine.OpportunityId).Account.OwnerId + '-' + prodsMap.get(oppyLine.Product2Id).Practice__c);
}

Then, just pass the maps:

updateOLI(rmap, newList, oppsMap, prodsMap);

...

public static void updateOLI(Map<String, Responsible_Party__c> rmap, List<OpportunityLineItem> olis, Map<Id, Opportunity> oppsMap, Map<Id, Product2> prodsMap)

...

rmap.get(oppsMap.get(o.OpportunityId).Account.OwnerId + '-' + prodsMap.get(o.Product2Id).Practice__c);
1
  • Is there a way to have the last line be more dynamic? For example maybe I don't always want my key to be the owner Id would I need to code in all the possible lines in the filter and the update? Commented Aug 14, 2017 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .