You need to sign in to do that
Don't have an account?
Anil 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
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
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
//Now I want to add Products
OpportunityLineItem OLI = New OpportunityLineItem(); //New line item
OLI.OpportunityId = Opp.id; // Adding OLI to Opp
OLI = new OpportunityLineItem();
OLI.OpportunityID = OppID;
OLI.OpportunityId = Opp.id; // Adding OLI to Opp
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
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());
}
// *******************************/
}
}
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
Adding a Pricebook to a Opportuity solved the problem.