I am trying to develop a "Handler class" for converting opportunity products to assets when the opportunity is closed/won Any code would be very much appreciate it! Thank you!

  • We can just suggest you that how to do this, you have to code that yourself. Write a trigger on Opportunity object, Events: after insert, after the update and here write the code to convert Product to Asset. Mar 29, 2017 at 9:51
  • Ok no problem....
    – user351004
    Mar 29, 2017 at 9:53

2 Answers 2


I think you should try this this code,and also update your trigger class .

Apex Trigger

trigger OpportunityTrigger on Opportunity (after insert,after update)

Handler Class

        public Class OpportunityTriggerHandler
    public static void testhandler(List <Opportunity> oplist)
      list <OpportunityLineItem> OLI=new list<OpportunityLineItem>();
       list<Asset> ast = new list<Asset>();
   for(Opportunity o: oplist)
    if(o.isWon == true && o.HasOpportunityLineItem == true && o.AccountId !=null)
     String opptyId = o.Id;
     OLI = [Select UnitPrice, Quantity , PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description
                                  From OpportunityLineItem
                                  where OpportunityId = :opptyId ];

     Asset a = new Asset();
    for(OpportunityLineItem ol: OLI{
      a = new Asset();
      a.AccountId = o.AccountId;
      a.Product2Id = ol.PricebookEntry.Product2Id;
      a.Quantity = ol.Quantity;
      a.Price =  ol.UnitPrice;
      a.PurchaseDate = o.CloseDate;
      a.Status = 'Purchased';
      a.Description = ol.Description;
      a.Name = ol.PricebookEntry.Product2.Name;


         update OLI;
    insert ast;

I hope it will help you.

Please mark my answer as a solution if it is helpful.

  • Thanks user351004 for selecting my answer as a best answer .It's my pleasure to help you! Mar 30, 2017 at 9:07


trigger OpportunityTrigger on Opportunity (after insert,after update) {
   if(trigger.isInsert && trigger.isAfter){
   if(trigger.isUpdate && trigger.isAfter){


public Class OpportunityTriggerHandler{
   public static void onAfterInsert(List<Opportunity> newOpp){

   public static void onAfterUpdate(List<Opportunity> newOpp, List<Opportunity> oldOpp){



This is the basic to create a handler, newOpp and oldOpp is the value from trigger.new and trigger.old respectively. now you can perform your operations in these method, let me know if you need more help.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.