+ Start a Discussion
Anil Rao 17Anil Rao 17 

assigning Opportunity ID to Opportunitlineitem records

I am creating a Opportunity & Opportunity Products via a Apex Code. I am able to create a Opportunity. For the created Opportunity, I want to add some Opportunity Product (OpportunityLineItem) recirds. My Code is

ID OppID;
OppID = opps[0].ID;  //Get the ID for the added Opportunity

//Now I want to add Products
OLI = new OpportunityLineItem();
 OLI.Opportunity.ID = oppID; // Getting runtime error = Attempt to de-reference a NULL Object
 OLI.product2.ID = cdtl.product__r.ID; // This works

Pl. help
Best Answer chosen by Anil Rao 17
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
Hey Anil,

It's worth going through the relationship between Product, Pricebook, PricebookEntry, Opportunity Product. All you need to create Opportunity Product is OpportunityId, PriceBookEntryId of the product, Unit Price & Quantity.

Please go through the this https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_opportunitylineitem.htm to know about the required fields.

Thanks,
Natarajan

All Answers

Taha Syed | SalesforceTaha Syed | Salesforce
Opportunity Opp = new Opportunity(); // New Opportunity

//Now I want to add Products
OpportunityLineItem OLI = New OpportunityLineItem(); //New line item
OLI.OpportunityId = Opp.id; // Adding OLI to Opp
 
Anil Rao 17Anil Rao 17
still same issue -
OLI = new OpportunityLineItem();
OLI.OpportunityID = OppID;
Taha Syed | SalesforceTaha Syed | Salesforce
Check last line..
OLI.OpportunityId = Opp.id; // Adding OLI to Opp
Anil Rao 17Anil Rao 17
If you look at my first 2 lines in the origina post, I am getting the opp.ID into a var called OPPID & assigning the OPPID.
Taha Syed | SalesforceTaha Syed | Salesforce
Works fine for me,

List <Opportunity> Opps = New List <Opportunity>();
Opps = [SELECT id from Opportunity Limit 1];

ID OppID = opps[0].id;  //Get the ID for the added Opportunity

//Now I want to add Products
OpportunityLineItem OLI = new OpportunityLineItem();
OLI.OpportunityID = oppID; // works fine
 

 
Raj VakatiRaj Vakati
Try this code
 
ID OppID= opps[0].ID;  //Get the ID for the added Opportunity

//Now I want to add Products
OLI = new OpportunityLineItem();
 OLI.OpportunityID = oppID; // Getting runtime error = Attempt to de-reference a NULL Object
 OLI.product2.ID = cdtl.product__r.ID; // This works

 
Anil Rao 17Anil Rao 17
Works now with the below 2 lines -     OLI.Opportunityid = oppid; &      OLI.product2ID = cdtl.product__r.ID;

However the Products are still not being added into the Opportunity Product Object. I am not getting any errors. PL. HELP.....THIS IS THE LAST PIECE in my POC....

Greatly appreciate.....
             

public class CreateOpportunity {
    public static void createOpp() {
   
      List<Opportunity> opps ;
      Opportunity indopp;
        ID OppID;
      Integer cartheadercnt = 0;
        
      List<OpportunityLineItem> OppLines;
      OpportunityLineItem OLI;
       
      Cart_header__c[] cart_header = [SELECT name, account__r.id, Close_Date__c FROM Cart_header__c WHERE active__c=false]; 
        if (cart_header.IsEmpty() == true) {
            System.debug('EMPTY Cart header'); 
            return;            
        }
        
        opps = new List<Opportunity>();    
        indopp = new Opportunity();
        
        for(cart_header__c ch : cart_header) {           
                   System.debug('Cart Name:' + ch.name);
                   indopp.AccountID = ch.account__r.id;
                 indopp.closedate = ch.Close_Date__c;
                 indopp.name = ch.name;
                 indopp.stagename = 'New Deal';
                   opps.add(indopp);          
      }
          insert opps;
        OppID = opps[0].ID;
        
        //*******************************
        cartheadercnt = opps.size(); 
       
       try {
                OppLines = new List<OpportunityLineItem>();
            for(Integer a = 0; a < cartheadercnt; a++) { 
                System.debug('Cart NameXX:' + opps[a].name + '=====' + cartheadercnt);
                List<cart_detail__c> cart_detail = [SELECT product__r.ID,lic_begin_dt__c, lic_end_dt__c,territory__c 
                                                    FROM cart_detail__c WHERE cart_header__r.name = :opps[a].name];
                if (cart_detail.IsEmpty() == true) {
                    System.debug('EMPTY CartDetail'); 
                    return;            
                }
                System.debug(' Cart Detail NOT EMPTY' + cart_detail.size());
                for(cart_detail__c cdtl : cart_detail) { 
                    OLI = new OpportunityLineItem();

                  OLI.Opportunityid = oppid;//opps[0].ID;
                    OLI.product2ID = cdtl.product__r.ID;
                    OLI.lic_beg_dt__c = cdtl.lic_begin_dt__c;
                    OLI.lic_end_dt__c = cdtl.lic_end_dt__c;
                    OLI.territory__c = cdtl.territory__c;
                    OLI.Quantity = 5;
                    OppLines.add(OLI);   
                }    
            }
            insert OppLines;
        } catch (DMLException e) {
            System.debug('Error:' + e.getMessage());
        }
      //  *******************************/
    }
}
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
Hey Anil,

It's worth going through the relationship between Product, Pricebook, PricebookEntry, Opportunity Product. All you need to create Opportunity Product is OpportunityId, PriceBookEntryId of the product, Unit Price & Quantity.

Please go through the this https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_opportunitylineitem.htm to know about the required fields.

Thanks,
Natarajan
This was selected as the best answer
Anil Rao 17Anil Rao 17
Got it. All I was missing was assigning a PriceBook at the Opportunity level. I had a single pricebook assigned for a Product. So was assuming that when I add a product the default prcebook will be taken. Also what confused me was that when I add from the entry screen, I am not asked for a Pricebook. I can just add a Product. Hence was trying to duplicae the scenario via apex.

Adding a Pricebook to a Opportuity solved the problem.