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
Joel Kikkert 23Joel Kikkert 23 

How to add a list of child assets to a list of parent assets

Hi, 
In my org we have Products(Service Lines) and Subproducts(Product Lines) and I wrote  a trigger on Opportunity to convert them to Assets(Services). 
However, I still need to add the subproducts to assets.

For Example: 1 product has 2 subproducts (master-detail)
it converts to 1 Asset and 2 subassets. 

Here is what I have so far. Any tips?
 
trigger OpportunityToService on Opportunity (after update) {


		
//1. if statement when opportunity field isWon = true AND other condition?
			for(Opportunity wonOpportunity : Trigger.new){
			if (wonOpportunity.IsWon == true && wonOpportunity.IsClosed == true) {
				

				//query all the service lines on the opportunity and store the Id's in an array
				String opportunityID = wonOpportunity.Id;
				Service_Line__c[] serviceLines = [SELECT Id, AccountId__c, Contract_End__c, Contract_Start__c,
													Contract_Term__c, Description__c, GoingToProducts__c, Implementation_Fee__c, 
													IsHealthMgmt__c, Opportunity_Product_Id__c, Opportunity__c, OwnerId, Practice__c,
													Price_Book_Entry_Id__c, Project_Fee__c, Recurring_Fixed_Revenue__c, Recurring_Revenue__c, 
													Recurring_Variable_Revenue__c, Service_Line__c, Service_Name__c, Service__c, 
													Total_Contract_Margin__c, Total_Contract_Value__c, Trust_Employer__c, Won_Service__c, 
													Y4_Revenue__c, Y5_Revenue__c, Y6_Revenue__c, Y7_Revenue__c
													FROM Service_Line__c
													WHERE Opportunity__c = :opportunityID];
				
				System.debug('The array contains the following IDs' + serviceLines);

				//for every Serviceline in the list , create a new service
			
				//create array in which to store all services for bulk insert
				Service__c[] newServices = new Service__c[]{};

				//create service
				Service__c newService = new Service__c();
				for (Service_Line__c sl: serviceLines) {
					newService = new Service__c();
					newService.Account__c						= wonOpportunity.AccountId;
					newService.Contract_Start__c				= sl.Contract_Start__c;
					newService.Contract_Term__c					= sl.Contract_Term__c;
					newService.Description__c					= sl.Description__c;
					newService.Implementation_Fee__c			= sl.Implementation_Fee__c;
					newService.Opportunity__c					= sl.Opportunity__c;
					newService.Price_Book_Entry_Id__c			= sl.Price_Book_Entry_Id__c;
					newService.Recurring_Fixed_Revenue__c		= sl.Recurring_Fixed_Revenue__c;
					newService.Recurring_Variable_Revenue__c	= sl.Recurring_Variable_Revenue__c;
					newService.Project_Fee__c					= sl.Project_Fee__c;
					newService.Total_Contract_Value__c			= sl.Total_Contract_Value__c;
					newService.Total_Contract_Margin__c			= sl.Total_Contract_Margin__c;
					newService.Y4_Revenue__c					= sl.Y4_Revenue__c;
					newService.Y5_Revenue__c					= sl.Y5_Revenue__c;
					newService.Y6_Revenue__c					= sl.Y6_Revenue__c;
					newService.Y7_Revenue__c					= sl.Y7_Revenue__c;
					newService.Is_Won__c						= true;
					newService.Service_Owner__c					= sl.OwnerId;
					newService.Service__c						= sl.Price_Book_Entry_Id__c.Product2.Name;
					newService.Trust_Employer__c				= sl.Trust_Employer__c;
					newServices.add(newService);
				

					}

					insert newServices;
					


				}


			}

				

			


		}

 
DixitDixit
try a query with a subquery, make a new Service for every won oportunitty (btw, is "cleaner" if you use if(wonOpportunity.isWon && wonOpportunity.isClosed), you don't need to equal to "True") and then make a new subasset for every subproduct.

something like:
 
Service_Line__c[] serviceLines = [SELECT Id, AccountId__c, Contract_End__c, Contract_Start__c, Contract_Term__c, Description__c, GoingToProducts__c, Implementation_Fee__c, IsHealthMgmt__c, Opportunity_Product_Id__c, Opportunity__c, OwnerId, Practice__c, Price_Book_Entry_Id__c, Project_Fee__c, Recurring_Fixed_Revenue__c, Recurring_Revenue__c, Recurring_Variable_Revenue__c, Service_Line__c, Service_Name__c, Service__c, Total_Contract_Margin__c, Total_Contract_Value__c, Trust_Employer__c, Won_Service__c, Y4_Revenue__c, Y5_Revenue__c, Y6_Revenue__c, Y7_Revenue__c, (SELECT id, name, customfields__c FROM service_products__r) FROM Service_Line__c WHERE Opportunity__c = :opportunityID];

list<service__c> servicesinsert = new list<service__c>();
list<products__c> productsinsert = new list<products__c>();

for(serviceline__c sl : serviceLines){
    Service__c newService = new Service__c();
   /* blah blah all the fields */ 

     for(service_products__c sp : sl.service_products__r){
      productline__c prod = new productline__c();
      prod.service_related__c = newService.id;
     /* all the fields */ 

    productsinsert.add(prod);
    }

serviceinsert.add(newservice);
}

if(serviceinsert.size()>0){
 insert serviceinsert;
}
if(productsinsert.size()>0){
 insert productsinsert;
}

Something like that, i just wrote it right here without testing it, just to tell you the idea.

Hope it helps.



 
Joel Kikkert 23Joel Kikkert 23
Thanks, that was exactly what I was looking for. I'll write and test it tomorrow:).