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
Kate LinskeKate Linske 

APEX trigger CreateAssetonClosedWon

I downloaded the APEX trigger to create assets from Opportunity Line Items. How can I then update the opportunity line items with the ID of the newly created Asset? Any help is appreciated!

trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
     for(Opportunity o: trigger.new){ 
      if(o.isWon == true && o.HasOpportunityLineItem == true && o.type == 'Purchase'){
         String opptyId = o.Id;
         OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c, DOM__c, Asset__c  
                                      From OpportunityLineItem 
                                      where OpportunityId = :opptyId  and Converted_to_Asset__c = false];
         Asset[] ast = new Asset[]{};
         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;
            a.Original_Opportunity__c = opptyID;
            a.In_Inventory__c = true;
            a.Date_of_Manufacturer__c = ol.DOM__c;
            ast.add(a);
            ol.Converted_to_Asset__c = true;
       }
      update OLI; 
      insert ast;
     }
    }
}
 
Best Answer chosen by Kate Linske
sancasanca

Hi Kate,

Please change line 26 to mapOfAsset.put(ol.id,a);

All Answers

sancasanca
Hi Kate ,

How is opportunity line items  is related to Asset ? Did you create any  master detail or lookup relation between these two?

 
Kate LinskeKate Linske
Hi Sanju, Yes! Sorry, There is a lookup field Asset__c on the Opportunity Line Item. I added that field to the OLI table thinking that would need to be done at some point? I am just starting out in APEX but unfortunately I need this amended faster than I can learn it. Appreciate the help!
sancasanca
Hi Kate ,

Could you try  the modified code ,

trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
     for(Opportunity o: trigger.new){ 
      if(o.isWon == true && o.HasOpportunityLineItem == true && o.type == 'Purchase'){
         String opptyId = o.Id;
         Map<ID,Asset> mapOfAsset = new  Map<ID,Asset>();
         OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c, DOM__c, Asset__c  
                                      From OpportunityLineItem 
                                      where OpportunityId = :opptyId  and Converted_to_Asset__c = false];
         Asset[] ast = new Asset[]{};
         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;
            a.Original_Opportunity__c = opptyID;
            a.In_Inventory__c = true;
            a.Date_of_Manufacturer__c = ol.DOM__c;
            ast.add(a);
            ol.Converted_to_Asset__c = true;
           mapOfAsset.add(ol.id,a);
       }
      insert ast;
for(OpportunityLineItem ol: OLI)
{
ol.Asset__c =mapOfAsset.get(ol.id).id;
}
update OLI; 
     }
    }
}
sancasanca
Please give kudos to the Ans if its helpful . 
Kate LinskeKate Linske
Hi Sanju, I get this error trying to save it: Error: Compile Error: Method does not exist or incorrect signature: [Map<Id,Asset>].add(Id, Asset) at line 26 column 12
Kate LinskeKate Linske
line 26 is this: mapOfAsset.add(ol.id,a);
sancasanca

Hi Kate,

Please change line 26 to mapOfAsset.put(ol.id,a);

This was selected as the best answer
Kate LinskeKate Linske
It worked!!!! Thank you SO MUCH!!
Vibhuti Sharma 3Vibhuti Sharma 3
Hi Kate,
Could you tell how did you fix the error for line number 26 ?
Kate LinskeKate Linske
Yes, I had picked it as the best answer so it was out of order. It should be:
mapOfAsset.put(ol.id,a);

Hope that helps!