+ Start a Discussion
Matt1000Matt1000 

How to assign PriceBookEntryId to OpportunityLineItem without character literal (de-reference error)

I have a class that inserts an OpportunityLineItem into an Opportunity.

The following statement is permitted:
    opportunitylineitem.PriceBookEntryID = '01uE0000000KoyD'; // literal is permitted
But the following statement results in an "Attempt to de-reference a null object" error
    opportunitylineitem.PriceBookEntryID = entry[0].id; // where entry is a LIST<PriceBookEntry> element
Investigating the PriceBookEntry table, I see that the PriceBookEntry ids are 18 characters long, where the literal permitted (and used on the usual Add Products to Opportunity page) is only 15 characters. I tried truncating the id via String.valueOf(entry[0].id).substring(0,15) but this causes the same de-reference error.
Is there a way to assign the id directly, or truncate it down to the needed 15 characters, without the dereference error? If I could get this, I'd be able to develop our solution further.
(Or am I missing something, a better approach.)
Here is the class. See the red highlighted lines.
//////////////////////
public class addProductToOp003 {
  Opportunity opportunity;
  
  public Opportunity getOpportunity() {
        opportunity = [SELECT id, name, PriceBook2Id FROM Opportunity
                WHERE id = :ApexPages.currentPage().getParameters().get('id')];
        return opportunity;
    }
    
    PriceBook2 pricebook2;
    public PriceBook2 getPriceBook2() {    
        pricebook2 = [SELECT id, name FROM PriceBook2 WHERE id=:opportunity.PriceBook2Id LIMIT 1];
        return pricebook2;
    }
    LIST<PriceBookEntry> entry;
    
    public LIST<PriceBookEntry> getEntry() {
        entry = [Select Id, name from PriceBookEntry where pricebook2id = :opportunity.PriceBook2Id ];
        return entry;
    }
    
    OpportunityLineItem opportunitylineitem;
    
    public OpportunityLineItem getOpportunityLineItem() {
        if(opportunitylineitem == null) opportunitylineitem = new OpportunityLineItem();
        opportunitylineitem.OpportunityID = ApexPages.currentPage().getParameters().get('id');
        //01uE0000000d3Y8 is PriceBookEntryID from HTML <form> on Add Products to Opportunity page
        //01uE0000000KoyD is a truncated ID from a different product in the PriceBookEntry table
        opportunitylineitem.PriceBookEntryID = '01uE0000000KoyD'; //'01uE0000000d3Y8'; // works as literal like this
        // opportunitylineitem.PriceBookEntryID = entry[0].id; // fails with de-reference error on visual force page
        // opportunitylineitem.PriceBookEntryID = String.valueOf(entry[0].id).substring(0,15); // also fails with de-ref error
        opportunitylineitem.Quantity = 3;
        opportunitylineitem.UnitPrice = 5000;
        return opportunitylineitem;
   }
    
   public PageReference save() {
      // Add the opportunity line item to the database.       
      insert opportunitylineitem;
      // Send the user to the detail page for the new account.      
      PageReference oppPage = new ApexPages.StandardController(opportunity).view();
      oppPage.setRedirect(true);
      return oppPage;
   }
    
}
//////////////////////
Any guidance is appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
Matt1000Matt1000

For reference, I found 2 solutions:

 

1. move the assignment to entry into the save() method

 

or

 

2. Create a constructor method for the class, and set the entry variable there. In this way, it is available to the later assignment call and avoids the null pointer error.

 

Basically, I think, in the error-prone code, the setEntry() was not necessarily called prior to the assignment statement as I assumed, even though it listed lower in the code. By using the constructor, I am able to ensure the variable is not null.

 

 

All Answers

Matt1000Matt1000

Addendum:

 

I can also find success with the 18-character ID used as a literal.

 

Any guidance how I can use the PriceBookEntry variable...or ensure it is not null at the time of assign.

 

Thanks!

 

Matt

Matt1000Matt1000

For reference, I found 2 solutions:

 

1. move the assignment to entry into the save() method

 

or

 

2. Create a constructor method for the class, and set the entry variable there. In this way, it is available to the later assignment call and avoids the null pointer error.

 

Basically, I think, in the error-prone code, the setEntry() was not necessarily called prior to the assignment statement as I assumed, even though it listed lower in the code. By using the constructor, I am able to ensure the variable is not null.

 

 

This was selected as the best answer