You need to sign in to do that
Don't have an account?
HTANIRS
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.
Kindly help to achieve this code freeze.
Thanks.
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.
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
All Answers
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
Thanks for your reply. This is working for insert. Could you please share the code for update.
Thanks.