3

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

enter image description here How should I assign product to OrderItem ?

10
  • can you post selectedOItems query? Also check if the price book is active
    – Rao
    Commented May 4, 2015 at 17:19
  • @rao updated the code
    – sfdc
    Commented May 4, 2015 at 17:22
  • If that's null, looks like you're going to want to post your controller code.
    – crmprogdev
    Commented May 4, 2015 at 17:30
  • @crmprogdev updated full controller code
    – sfdc
    Commented May 4, 2015 at 17:32
  • what do you see from debug logs on system.debug('oi.PricebookEntry.Product2Id ###'+ oi.PricebookEntry.Product2Id);
    – Rao
    Commented May 4, 2015 at 17:33

2 Answers 2

2

In your code, I dont see you are inserting PricebookEntry. before product2Id we need to have this value. I guess this question will help you on how we can insert Orderitem.

0

Thanks @Rao for pointing me in right direction and here is my code.

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

         list<id> selectedProductList = new list<Id>();

         for(OrderItem oi:selectedOItems){ 
         selectedProductList.add(oi.PricebookEntry.Product2Id);
        }

        list<OrderItem>oiList =  [Select o.PricebookEntry.ProductCode, o.PricebookEntry.IsActive, o.PricebookEntry.Product2Id, o.PricebookEntry.Pricebook2Id, o.PricebookEntry.Name, o.PricebookEntry.Id, o.PricebookEntryId, o.Id From OrderItem o where o.PricebookEntry.Product2Id in:selectedProductList];

         map<id,Id> pbemap = new map<Id,Id>();
         for(OrderItem oi:oiList){
             pbemap.put(oi.id, oi.PricebookEntryId);
         } 

         for(OrderItem oi:selectedOItems){
            OrderItem o = new OrderItem();
             o.PricebookEntryId = pbemap.get(oi.Id);
            o.Quantity = oi.Quantity;
            o.unitPrice = oi.UnitPrice;
            o.orderId = oId; 
            oItemsToInsert.add(o); 
          }

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

    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .