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
Jhony DeepJhony Deep 

I am trying to develop a "Handler class" for converting opportunity products to assets when the opportunity is closed/won

Shivdeep KumarShivdeep Kumar
Hi Jhony,
Please look at the below code, it ll help you bcoz its working fine for me.
// Trigger
Trigger CreateAssetonClosedWon on Opportunity (after insert, after update){
    CreateAssetonClosedWonHelper.OppAfterUpdate(trigger.New, trigger.old, trigger.newMap, trigger.oldMap);
}

// Helper class

public class CreateAssetonClosedWonHelper{

    Private static Map <Id,boolean> oldIsWonMap = null;

    public static void OppAfterUpdate(list<Opportunity> newOpps, list<Opportunity> oldOpps, map<Id,Opportunity> newMap, map<Id,Opportunity> oldMap) {

        if(oldIsWonMap == null) {
            oldIsWonMap = new map<Id,boolean >();
        }

        map<Id,Opportunity> justWonOppsMap = new map<Id,Opportunity>();
        for (Opportunity o : newOpps) {

            //This checks to see if there was a value set in a previous trigger run
            boolean oldIsWon = (oldIsWonMap.containsKey(o.id)) ? oldIsWonMap.get(o.id) : oldmap.get(o.id).isWon;

            //this checks the current opp value with the 'correct' old value
            if(o.isWon && !oldIsWon){
                justWonOppsMap.put(o.Id, o);
            }

            //this puts in the 'correct' old value in case the trigger is run again in the same context
            if(oldIsWon != o.isWon) {
                oldIsWonMap.put(o.id,o.isWon);
            }
        }
        Map<id, opportunityLineItem> mapOpp = new Map<id, opportunityLineItem>([Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name,Converted_to_Asset__c  
                                      From OpportunityLineItem where opportunityId IN : justWonOppsMap.KeySet() and converted_to_Asset__c = false]);
        List<Asset> assets = new list<Asset>();
        
        for(Opportunity opp : justWonOppsMap.values()){
            For (opportunityLineItem Oli : mapOpp.values()){
                If(opp.iswon == true && opp.hasopportunitylineitem ==true){
                    Asset a = new asset();
                    a.name = Oli.pricebookentry.product2.name;
                    a.Accountid = opp.Accountid;
                    a.product2id = Oli.pricebookentry.product2id;
                    a.quantity = Oli.quantity;
                    a.price = Oli.unitprice;
                    a.purchasedate = opp.closedate;
                    assets.add(a);
                }
            }
            
        }
        If (assets.size()>0)
            Insert assets;

    }
}

Please let me know if this help !

Thanks
Shivdeep
Harshit Garg 6Harshit Garg 6
Hi Jhony,

Apex Trigger
 
trigger OpportunityTrigger on Opportunity (after insert,after update)
{
 OpportunityTriggerHandler.testhandler(trigger.new);
}

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;
      ast.add(a);

     }

   }
   }
         update OLI;
    insert ast;
}
}

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

Thanks,
Harshit Garg
harshitgarg2591@gmail.com
NagendraNagendra (Salesforce Developers) 
Hi Jhony,

Please try below code:

Apex Trigger;
trigger OpportunityTrigger on Opportunity (after insert,after update) {
   if(trigger.isInsert && trigger.isAfter){
      OpportunityTriggerHandler.onAfterInsert(trigger.new);
   }
   if(trigger.isUpdate && trigger.isAfter){
      OpportunityTriggerHandler.onAfterInsert(trigger.new,trigger.old);
   }
}
Handler:
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 this method, let me know if you need more help.

Hope this helps.

Regards,
Nagendra.