I need to get Opportunity id to insert new OpportunityLineItem.. OrderProducts_c object has master detail relationship to Order_c which has lookup to Opportunity. I don't understand this code suppose to work.. If anybody can help me i will appreciate it
here is my error message:
first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]: Class.OrderLineItemProcess.onAfterInsert: line 41, column 1
Map<Id, Order__c> ords = new Map<Id, Order__c>();
set<Id> productIds=new set<Id>();
Pricebook2 standartpb = [select id from Pricebook2 where IsStandard = true and isActive= true limit 1];
for(OrderProducts__c record: TriggerNew.values()) {
productIds.add(record.products__c);
ords.put(record.Id, null);
}
Map<Id, PricebookEntry> prices = new Map<Id,PricebookEntry>();
for(PricebookEntry pricebook: [Select Id,Product2Id,UnitPrice,Pricebook2Id From PricebookEntry Where Product2Id IN :productIds and Pricebook2Id=:standartpb.Id])
{
prices.put(pricebook.Product2Id,pricebook);
}
List<OpportunityLineItem> lineItems=new List<OpportunityLineItem>();
for(OrderProducts__c record: TriggerNew.values())
{
OpportunityLineItem oppLineItem=new OpportunityLineItem(
PricebookEntryId=prices.get(record.Products__c).Id,
OpportunityId=record.order_mas__r.OpportunityLookup__c, **// required field is missing**
Quantity=record.Quantity__c,
Discount=record.Discount__c,
UnitPrice=record.Salesprice__c,
Description=record.LineDescription__c,
OrdLineItemExternalId__c=record.Id
);
lineItems.add(oppLineItem);
}
FlaG.run=false;
insert(lineItems);
Attribution to: user7361
Possible Suggestion/Solution #1
My guess :
If this error is happening during save of the OrderProducts record looks like
1) OrderProducts does not have order mas field( lookup) populated (or)
2) The opportunityLookup field related to the ordermas is not populated on the order mas record related to the orderproduct record you are trying to save.
If this error is happening when you are deploying/ running a test class
Your test class data is not populating the opportunity lookup data because of which the code might throw this error.
Update:
When you pointed out the trigger as below
trigger OLITrigger on OrderProducts__c (after insert) {
OrderLineItemProcess handler=new OrderLineItemProcess();
if(Trigger.isAfter && Trigger.isInsert && FlaG.run) {
handler.onAfterInsert(Trigger.newMap);
}
}
what happens here is that the trigger only passes field info related to orderProducts and none of the related list fields are pulled over when you use Trigger.new, Trigger.newmap
if you need to associate the opptyId with order_mas_r.OpportunityLookup_c you need to query this data
Change your
for(OrderProducts__c record: TriggerNew.values()) { ..... ..... ....
}
into
for(OrderProducts__c record: [selectProducts__c,order_mas__r.OpportunityLookup__c,Quantity__c,Discount__c,Salesprice__, LineDescription__c,OrdLineItemExternalId__c from OrderProducts__c where Id IN: TriggerNew.values())
{
......
.....
}
Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30200