function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HTANIRSHTANIRS 

opportunity line items not inserting opportunity id

Hi,

I need help in below Trigger. I am unable to pull opportunity Id to insert Opportunity Line Items. I have hardcoded and inserted in below code.
Requirement:
I have a Custom Object where it has same fields as Opportunity Line Items. When Inserting Custom Object I want to insert Opportunity Line Items with respect to opportunity. Opportunity and custom object does not have relationship. But they have a common field called orderId where I can match Opp and Custom Obj.
 
trigger insertOppsLineitem on OrderItems__c (after insert) {
    System.debug('--- Inside OrderItems Trigger ---');      

    Set<Id> orderIds = new Set<Id>();
    Set<Id> oppIds = new Set<Id>();
    
    for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.Id);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }
    
    Map<Id, List<Opportunity>> oppMap = new Map<Id, List<Opportunity>>();
    
    if(!orderIds.isEmpty()){
        System.debug('--- Checking Order ID: ---');
        for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE Name IN: orderIds]){
            orderIds.add(opp.Id);
            System.debug('--- opportunity Ids:' + orderIds);
        } 
    }    

    List<OrderItems__c> orderItems = [SELECT Id, Name, OrderId__c FROM OrderItems__c WHERE Id IN :Trigger.newMap.keyset()];
    
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];

    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
    for(OrderItems__c o : Trigger.New){
        System.debug('--- Order Items : ---');
       // for(Opportunity opp : oppMap.get(o.Name)){
            System.debug('--- Inside Opportunity');
            OpportunityLineItem oli = new OpportunityLineItem();
            oli.OpportunityId = '0060p000001rgcR';
            oli.PricebookEntryId = priceBookList[0].Id; 
            oli.Quantity = o.Qty_Ordered__c;
            oli.TotalPrice = o.Price__c;
            oliList.add(oli);
         //   System.debug('--- Inside Opportunity' + opp);
            System.debug('--- Oli List: ---');
        //}
    }
}

Kindly help to achieve this code freeze.

Thanks.
Best Answer chosen by HTANIRS
Raj VakatiRaj Vakati
The problem  is  in For loop you are setting  OrderItems__c  ID 

for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.Id);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }

And you are passing this IDs to opportunity Name in SOQL i guess you dnt get any data here

for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE Name IN: orderIds]){
            orderIds.add(opp.Id);
            System.debug('--- opportunity Ids:' + orderIds);
        } 


Change Code As below 
 
trigger insertOppsLineitem on OrderItems__c (after insert) {
    System.debug('--- Inside OrderItems Trigger ---');      

    Set<Id> orderIds = new Set<Id>();
    Map<Id, Id> oppIds = new Map<Id, Id>();
    
    for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.Id);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }
    
    Map<Id, List<Opportunity>> oppMap = new Map<Id, List<Opportunity>>();
    
    if(!orderIds.isEmpty()){
        System.debug('--- Checking Order ID: ---');
        for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE OrderItems__c IN: orderIds]){
            oppIds.add(opp.OrderItems__c , opp.Id );
            System.debug('--- opportunity Ids:' + orderIds);
        } 
    }    

    
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];

    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
    for(OrderItems__c o : Trigger.New){
            System.debug('--- Inside Opportunity');
            OpportunityLineItem oli = new OpportunityLineItem();
            oli.OpportunityId = oppIds.get(o.Id);
            oli.PricebookEntryId = priceBookList[0].Id; 
            oli.Quantity = o.Qty_Ordered__c;
            oli.TotalPrice = o.Price__c;
            oliList.add(oli);
         
    }
}

 

All Answers

Raj VakatiRaj Vakati
The problem  is  in For loop you are setting  OrderItems__c  ID 

for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.Id);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }

And you are passing this IDs to opportunity Name in SOQL i guess you dnt get any data here

for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE Name IN: orderIds]){
            orderIds.add(opp.Id);
            System.debug('--- opportunity Ids:' + orderIds);
        } 


Change Code As below 
 
trigger insertOppsLineitem on OrderItems__c (after insert) {
    System.debug('--- Inside OrderItems Trigger ---');      

    Set<Id> orderIds = new Set<Id>();
    Map<Id, Id> oppIds = new Map<Id, Id>();
    
    for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.Id);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }
    
    Map<Id, List<Opportunity>> oppMap = new Map<Id, List<Opportunity>>();
    
    if(!orderIds.isEmpty()){
        System.debug('--- Checking Order ID: ---');
        for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE OrderItems__c IN: orderIds]){
            oppIds.add(opp.OrderItems__c , opp.Id );
            System.debug('--- opportunity Ids:' + orderIds);
        } 
    }    

    
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];

    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
    for(OrderItems__c o : Trigger.New){
            System.debug('--- Inside Opportunity');
            OpportunityLineItem oli = new OpportunityLineItem();
            oli.OpportunityId = oppIds.get(o.Id);
            oli.PricebookEntryId = priceBookList[0].Id; 
            oli.Quantity = o.Qty_Ordered__c;
            oli.TotalPrice = o.Price__c;
            oliList.add(oli);
         
    }
}

 
This was selected as the best answer
HTANIRSHTANIRS
Hi Raj,

Thanks for your reply. This is working for insert. Could you please share the code for update.

Thanks.