You need to sign in to do that
Don't have an account?
Joel 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?
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; } } }
something like:
Something like that, i just wrote it right here without testing it, just to tell you the idea.
Hope it helps.