0

here is my code in which i am trying to get the product name from opportunity line item but its returning back NULL value. can anyone please help me to get that product name. I can get id from product2 but i want the name of product so that i can abbreviate it and can store it in Order_Name__c field of orderItem. After adding the product only i am firing this trigger.

   public class Procuct_Adding_To_Order{

public static void productOrder(List<OpportunityLineItem> oli){ 
   system.debug('-----------------------');

    set<id> prodset = new set<Id>();
for(OpportunityLineItem item : oli)
{
  prodSet.add(item.product2id);
}
// collect products
Map<Id, product2> prodMap = new Map<Id, Product2>([select name from product2 where id in :prodSet]); 

// create a map for OLIid & corresponding Product Name
Map<Id, string> OLIprodMap = new Map<Id, string>();

for(OpportunityLineItem item : oli)
{
 OLIprodMap.put(item.id, OLIprodMap.get(oli.product2id).name); //getting Error in this line.
}


   set<Id> setOppId = new set<Id>(); 
    for(OpportunityLineItem obj: oli){ 
        setOppId.add(obj.OpportunityId); 
    } 

   list<opportunity> needed = [Select Master_Opportunity__c from opportunity Where Opportunity.id =: setOppId];
        List<OrderItem> orderIt = new List<OrderItem>();
    for(Order ord: [Select id from Order where opportunityId =: needed[0].Master_Opportunity__c])
      {
       for(OpportunityLineItem o : oli)
       {  
        OrderItem op = new OrderItem(); 
        op.Opportunity_del__c = needed[0].Master_Opportunity__c; 
        op.orderId = ord.id;
        op.Order_Name__c = o.product2Id;
        system.debug('-----------------------3333333333333333333333'+ o.Product2Id);
        op.PriceBookEntryId = o.PriceBookEntryId;
        op.Quantity = o.Quantity;
        op.UnitPrice = 100;
        orderIt.add(op);

          } 
        }
    upsert orderIt; 
}
}
2
  • from where you are calling productOrder method?
    – Himanshu
    Commented Feb 7, 2016 at 6:46
  • from opportunityLineitem, trigger
    – Sharma_raj
    Commented Feb 7, 2016 at 7:20

2 Answers 2

1

As you are calling this method from trigger so you can't direct access parent field from the 'Trigger.New' what you can do here is first store product ID in set and query of Product object and get Name of product and store it in the map. Now iterate over the list and using the Product Id get Product name from Map and use it in your code.

2
  • Ohkay, so i can't get the name of product using Product2.Name or PriceBookEntry.Product2.Name
    – Sharma_raj
    Commented Feb 7, 2016 at 7:27
  • 1
    No in Trigger syntax you can't get them. You have to query them to get the product name. Commented Feb 7, 2016 at 7:28
1

You are complicating it. You can retrieve the productids directly from Opportunity Products

set<id> prodset = new set<Id>();
for(OpportunityLineItem item : oli)
{
  prodSet.add(item.product2id);
}
// collect products
Map<Id, product2> prodMap = new Map<Id, Product2>([select name from product2 where id in :prodSet); 

// create a map for OLIid & corresponding Product Name
Map<Id, string> OLIprodMap = new Map<Id, string>();

for(OpportunityLineItem item : oli)
{
 OLIprodMap.put(item.id, OLIprodMap.get(item.product2id).name);
}

then use OLIprodMap while creating the Orders something like

order.productname = OLIprodMap.get(item.id) //pseudo code
7
  • I m getting this error--- Compile Error: Initial term of field expression must be a concrete SObject: List<OpportunityLineItem> at line 19 column 41 Line-- OLIprodMap.put(item.id, OLIprodMap.get(oli.product2id).name); ,,,,,,,,, what should i do to correct it.
    – Sharma_raj
    Commented Feb 7, 2016 at 7:54
  • @RAT__ this is not clear, paste your full code on your question itself
    – Jarvis
    Commented Feb 7, 2016 at 8:05
  • i have updated the code please check it up there ,,,,,,, in my Question
    – Sharma_raj
    Commented Feb 7, 2016 at 8:10
  • I have edited, the snippet should be OLIprodMap.put(item.id, OLIprodMap.get(item.product2id).name);
    – Jarvis
    Commented Feb 7, 2016 at 8:18
  • no Bennie still the same error at same line
    – Sharma_raj
    Commented Feb 7, 2016 at 10:56

You must log in to answer this question.

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