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
Kevin Chiles 930Kevin Chiles 930 

Renewal Opp from Asset

Hello!

I am trying to create opportunity renewals from Assets.  If an asset is tied to the same opportunity as an expiring asset, we need to add that as a line item on the opportunity page.  Right now, I can only create an opp with 1 product, but I may have 4 assets that need to be added to that opp as products due to their previous relationship.  Please see my code below to let me know what I am doing wrong:
 
trigger createOpp2 on Asset (after update) {


for(Asset A : Trigger.New){



//Opportunity opp=[select Id from Opportunity where id =: A.Opportunity__c Limit 1];


list<Asset> assets=[select Id,Product2id,Opportunity__c From Asset where Opportunity__c =:a.Opportunity__c];
 

 List<PriceBookEntry> ProductList =  [SELECT  Id, Product2Id,UnitPrice FROM PriceBookEntry WHERE Product2Id =:a.Product2id];
 
    

List<OpportunityLineItem> listOI = new List<OpportunityLineItem> ();
  

 if (Trigger.isUpdate && A.Create_Renewal_Opp__c==TRUE ) 
                        {  
                        Opportunity o = New Opportunity(
                            AccountId =A.AccountId,
                            Name='New Subscrition Renewal',                    
                            Type = 'Subscription Renewal',
                            CloseDate=System.Today()+60,
                            RecordTypeId=[select Id from RecordType where Name = 'Renewal' and SobjectType = 'Opportunity'].Id,
                            StageName='Renewal');
                                     
                            insert o;
                            
                            
                            for(PricebookEntry Product: ProductList){
                               Opportunitylineitem NewItem = new opportunitylineItem();
                                   NewItem.opportunityId=o.id;
                                       NewItem.quantity=a.quantity;
                                              NewItem.UnitPrice=(Product.UnitPrice*0.5+Product.UnitPrice);
                                               NewItem.PricebookEntryId=Product.Id;
                                               
                                                   listOI.add(NewItem);   
                   
       }
                                                  insert listOI;
                                                               
    
     }}}