function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rajesh k 10rajesh k 10 

How to insert opportunity line item record in salesforce?

Best Answer chosen by rajesh k 10
Jigar.LakhaniJigar.Lakhani

Hello,

Step 1: You need to select producuts for your line items, each product have separate line item. Means if you will select 2 products then it will create two opportunity line items for each.


User-added image
 

Step 2: Now you can enter other details like Quantity, Date and Descirption for line items.


User-added image
 

Step 3: You opporutnity line items will display like below, in related list of opportunity.

 

User-added image

Thanks and Cheers,
Jigar

All Answers

Jigar.LakhaniJigar.Lakhani
Do you want to insert it using apex code or from opportuinty detail page?

Using Apex Code:
Account objAccount = new Account();
objAccount.Name = 'Test';
Insert objAccount;

Opportunity objOpporunity = new Opportunity();
objOpporunity.Name = 'Test Opporutnity';
objOpporunity.AccountId = objAccount.Id;
objOpporunity.StageName = 'Closed Won';
objOpporunity.CloseDate = system.Today();
Insert objOpporunity;

Product2 objProduct = new Product2();
objProduct.Name = 'Test';
objProduct.Description = 'Test';
objProduct.RevRec_StartDate__c = system.today();
objProduct.RevRec_EndDate__c = system.today().addDays(1);
Insert objProduct;

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
PricebookEntry objPBEntry = new PricebookEntry(Pricebook2Id = pb2Standard.Id, Product2Id=objProduct.Id,UnitPrice=500,IsActive=true);
Insert objPBEntry;

OpportunityLineItem objLineItem = new OpportunityLineItem();
objLineItem.PriceBookEntryId = objPBEntry.Id;
objLineItem.OpportunityId = objOpporunity.Id;
objLineItem.Quantity = 1;
objLineItem.Unitprice = 500;
Insert objLineItem;

From Detail Page:

User-added image

Thanks and Cheers,
Jigar
rajesh k 10rajesh k 10
Hi,
   After click Addproduct what are the steps i will follow ?
Jigar.LakhaniJigar.Lakhani

Hello,

Step 1: You need to select producuts for your line items, each product have separate line item. Means if you will select 2 products then it will create two opportunity line items for each.


User-added image
 

Step 2: Now you can enter other details like Quantity, Date and Descirption for line items.


User-added image
 

Step 3: You opporutnity line items will display like below, in related list of opportunity.

 

User-added image

Thanks and Cheers,
Jigar

This was selected as the best answer
rajesh k 10rajesh k 10
Thank you sir..