Skip to content

Instantly share code, notes, and snippets.

@amitastreait
Created April 6, 2021 06:43
Show Gist options
  • Save amitastreait/365b7eda74876deff99ca29a8d81bbf4 to your computer and use it in GitHub Desktop.
Save amitastreait/365b7eda74876deff99ca29a8d81bbf4 to your computer and use it in GitHub Desktop.
How to Create order Line Item in salesforce Apex
Product2 p = new Product2();
p.Name = 'Test Product';
p.Description='Test Product';
p.productCode = 'ABC';
p.isActive = true;
insert p;
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;
// insert account
Account acc = new Account(
Name = 'SFDCPanther.com',
Rating = 'Hot',
Industry = 'Banking',
Phone = '9087654321'
);
insert acc;
Order order = new Order(
AccountId = acc.Id,
EffectiveDate = System.today(),
Status = 'Draft',
PriceBook2Id = Test.getStandardPricebookId()
);
insert order;
OrderItem lineItem = new OrderItem();
lineItem.OrderId = order.id;
lineItem.Quantity = 24;
lineItem.UnitPrice = 240;
lineItem.Product2id = p.id;
lineItem.PricebookEntryId=standardPrice.id;
insert lineItem;
// Now update & Activate the Order
order.Status = 'Activated';
update order;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment