Monday, 2 June 2014

How to Insert Opportunity Record with Product and PriceBook in Salesforce through Apex Code

Here is the example to inserting Opportunity Record with Product and PriceBook in Salesforce through Apex Code

  1. // Insert Product
  2. Product2 pr = new Product2();
  3. pr.Name='Moto - G1';
  4. pr.isActive=true;
  5. insert pr;

  6. // Insert Pricebook
  7. PriceBook2 customPriceBook = new PriceBook2();
  8. customPriceBook.Name='Custom Pricebook';
  9. customPriceBook.IsActive=true;
  10. insert customPriceBook;

  11. // Query Standard and Custom Price Books
  12. Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
  13. Id stdPriceBookRecId = Test.getStandardPricebookId();

  14. // Create Standard PriceBookEntry
  15. PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
  16. stdPriceBookEntry.Product2Id=pr.Id;
  17. stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
  18. stdPriceBookEntry.UnitPrice=2000;
  19. stdPriceBookEntry.IsActive=true;
  20. insert stdPriceBookEntry;
  21. // Create Custom PriceBookEntry
  22. PriceBookEntry customPriceBookEntry = new PriceBookEntry();
  23. customPriceBookEntry.Product2Id=pr.Id;
  24. customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
  25. customPriceBookEntry.UnitPrice=5000;
  26. customPriceBookEntry.IsActive=true;
  27. insert customPriceBookEntry;

  28. // Create Opportunity
  29. Opportunity opp = new Opportunity();
  30. opp.Name = 'Test';
  31. opp.CloseDate= System.Today();
  32. opp.StageName='Prospecting';
  33. insert opp;

  34. // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
  35. OpportunityLineItem oppLineItem = new OpportunityLineItem();
  36. oppLineItem.OpportunityId = opp.Id;
  37. oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
  38. oppLineItem.UnitPrice = 7000;
  39. oppLineItem.Quantity = 5;
  40. insert oppLineItem;


  1. This comment has been removed by the author.

  2. I have a problem when I try execute the code: "Illegal assignment from PricebookEntry to Pricebook2".
    Could you help me?

  3. I read that Post and got it fine and informative. how to make money online

  4. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. bestcheapro

  5. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. queens cannabis dispensary

  6. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. psilocybe mexicana

  7. i am getting invalid type error for OpportunityLineItem?? Help me to resolve this

  8. I read a article under the same title some time ago, but this articles quality is much, much better. How you do this.. ks kurve

  9. It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. Buy guns in Europe

  10. Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share 안전놀이터

  11. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here. I think this is an educational post and it is extremely helpful and learned. along these lines, I might want to thank you for the endeavors you have made in composing this article. Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it! I am thinking about how I may be told at whatever point another post has been made. I have subscribed to your RSS which may do the trap? Have an extraordinary day! 먹튀사이트

  12. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. Much appreciated such a great amount for this data. I need to tell you I agree on a few of the focuses you make here and others may require some further audit, yet I can see your perspective. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. 토토매거진

  13. Interesting post. I Have Been wondering about this issue. so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks . We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up . I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. 토디즈

  14. I'm truly intrigued that there is such a lot of data about this subject that have been revealed and you've put forth a valiant effort, with such a lot of class. I was suggested this blog by my cousin. I'm uncertain about whether this post is composed by him as no one else know such point by point about my trouble. You're great! Much obliged! Noteworthy site, Distinguished input that I can handle. Im pushing ahead and may apply to my present place of employment as a pet sitter, which is truly pleasant, however I need to extra extend. Recognizes for paper a particularly advantageous organization, I staggered adjacent to your blog other than translate a restricted declare. I need your method of engraving.. 검증사이트

  15. Wonderful article. Thanks too much for the breakdown you have presented here. I am still sort-of an illiterate on this topic. So this post was really helpful to me personally. My husband just launched his own ebook on this issue and I think some things learned from your post would enable me personally to give him the necessary support. I don t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site. You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog. 온카맨

  16. It is a completely interesting blog publish.I often visit your posts for my project's help about Diwali Bumper Lottery and your super writing capabilities genuinely go away me taken aback.Thank you a lot for this publish. This is an extraordinary element for sharing this useful message. I am dazzled by the information you have on this blog. It causes me from multiple points of view. A debt of gratitude is in order for posting this once more. Excellent blog here! Additionally your web site a lot up very fast! What host are you using? Can I am getting your associate hyperlink for your host? I wish my site loaded up as fast as yours 생활바카라


Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...