[SalesForce] how to get Product name using product2 from opportunitylineitems

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; 
}
}

Best Answer

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.