I am trying to create an orderitem from visualforce page and I am getting an error
Attempt to de-reference a null object
while assigning pricebookentry.priduct2Id.Here is my part of code where it is failing.
public class ReductionOrderProductsExtensions {
public List<OrderItem> orderItems{get;set;}
public List<OrderItem> orderItems2{get;set;}
public String oId;
public List<OrderItemWrapper> orderItemList{get;set;}
public List<Id> oiIds = new List<Id>();
public List<OrderItem> selectedOItems = new list<OrderItem>();
public List<OrderItem> oItemsToInsert = new list<OrderItem>();
public ReductionOrderProductsExtensions(ApexPages.StandardController controller) {
oId = ApexPages.currentPage().getParameters().get('OrderId');
this.orderItemList = new List<OrderItemWrapper>();
getOrderItemSet();
}
public void getOrderItemSet()
{
Order o = [select AccountId,Id from Order where Id = :oId];
orderItems = [select orderId,Order.AccountId,Status__c,quantity,Product_Family__c,installed_billable_as_of__c,Vendor__c,Circuit_type__c,Circuit_Speed__c,Pricebookentry.Product2.Name,ListPrice,UnitPrice,Vendor_Changed__c,Installed_Quantity__c,Revenue_Type__c,Order.Status from OrderItem
where Order.AccountId =: o.AccountId and Order.Status = 'Activated' and quantity >0];
set<String> uniqueOItems = new set<String>();
for(OrderItem oi:orderItems){
if(!uniqueOItems.contains(oi.Pricebookentry.Product2.Name + oi.Vendor__c + oi.Circuit_Speed__c + oi.Circuit_Type__c)){
uniqueOItems.add(oi.Pricebookentry.Product2.Name + oi.Vendor__c + oi.Circuit_Speed__c + oi.Circuit_Type__c);
oiIds.add(oi.Id);
}
}
orderItems2 = [select orderId,Order.AccountId,Status__c,quantity,Product_Family__c,installed_billable_as_of__c,Vendor__c,Circuit_type__c,Circuit_Speed__c,PricebookEntry.Product2Id,Pricebookentry.Product2.Name,ListPrice,UnitPrice,Vendor_Changed__c,Installed_Quantity__c,Revenue_Type__c,Order.Status,Id from OrderItem
where Id in: oiIds];
for(OrderItem oi:orderItems2){
this.orderItemList.add(new OrderItemWrapper(oi));
}
}
public class OrderItemWrapper{
public OrderItem oitem{get;set;}
public Boolean selected{get;set;}
public OrderItemWrapper(OrderItem o){
this.oitem = o;
this.selected = false;
}
}
public PageReference save(){
try{
selectedOItems.clear();
for(OrderItemWrapper owrapper : orderItemList){
if(owrapper.selected == true)
selectedOItems.add(owrapper.oitem);
}
system.debug('selectedOItems ###'+selectedOItems);
for(OrderItem oi:selectedOItems){
system.debug('oi ###'+oi);
OrderItem o = new OrderItem();
system.debug('o ###'+o);
system.debug('oi.PricebookEntry.Product2Id ###'+ oi.PricebookEntry.Product2Id);
system.debug('o.PricebookEntry.Product2Id ###'+ o.PricebookEntry.Product2Id);
o.PricebookEntry.Product2Id = oi.PricebookEntry.Product2Id;
system.debug('oi.PricebookEntry.Product2Id ###'+ oi.PricebookEntry.Product2Id);
o.Quantity = oi.Quantity;
o.unitPrice = oi.UnitPrice;
system.debug('oId ###'+oId);
o.orderId = oId;
oItemsToInsert.add(o);
}
system.debug('oItemsToInsert ###'+oItemsToInsert);
if(oItemsToInsert.size()>0)
insert oItemsToInsert;
pageReference ref = ApexPages.currentPage();
Id id = ref.getParameters().get('OrderId');
ref.getParameters().clear();
ref.getParameters().put('OrderId', id);
ref.setRedirect(true);
return ref;
}
catch(Exception ex){
ApexPages.addMessages(ex);
}
return null;
}
public PageReference cancel(){
return new PageReference('/' + oId);
}
}
Here is the debug log of those two lines..I see the order item is being picked but not sure how to check for active pricebookentry
How should I assign product to OrderItem ?