You need to sign in to do that
Don't have an account?

Help change a trigger that converts products to assets to just update quantity the second time
Hi!
I have a trigger that converts products in an opportunity to assets in the account when the opportunity moves to the stage Closed Won. This part works good.
However, when a product is converted to an asset for that account it gets marked as Converted (Checkbox). If another opportunity (under the same account) with for example additional users for that same product is closed it does not add anything to the assets for the account today.
What I want it to do is: IF Product X is already converted to Asset X on the account - then update the quantity of existing Asset X from the last opportunity Closed Won + quantity from Product X for the newly Closed Won opportunity.
Old Asset X (Quantity10) + New Product X (Quantity 5) = Old Asset X (Quantity 15).
Help please! :)
My Trigger:
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) { for(Opportunity o: trigger.new){ if(o.isWon == true && o.HasOpportunityLineItem == true && o.RecordTypeId == '012200000004fxZ'){ String opptyId = o.Id; OpportunityLineItem[] OLI = [Select Total_Price__c, Cost__c, Quantity, Revenue_Type__c, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_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.Total_Price__c; a.PurchaseDate = o.CloseDate; a.Status = 'A6 - Closed Won'; a.Description = ol.Description; a.Name = ol.PricebookEntry.Product2.Name; ast.add(a); ol.Converted_to_Asset__c = true; } update OLI; insert ast; } } }
Your code is also not bulkified, so it's a good thing you came here to get your code fixed.
Here's how you might go about that:
I don't think it can get any less complex than that, so you're probably going to have to read the code a couple of times. I hope this helps.
All Answers
Your code is also not bulkified, so it's a good thing you came here to get your code fixed.
Here's how you might go about that:
I don't think it can get any less complex than that, so you're probably going to have to read the code a couple of times. I hope this helps.
I get an error with this though on the end (lines.converted_to_asset__c = true;)
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<OpportunityLineItem> at line 36 column 5
I removed the s so that it was line.converted_to_asset__c=true;
Then I could save the trigger, but that might change the meaning of the code?
When trying to save products in an opportunity it gives me an error now:
Apex trigger CreateAssetonClosedWon caused an unexpected exception, contact your administrator: CreateAssetonClosedWon: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.OpportunityId: Trigger.CreateAssetonClosedWon: line 17, column 1
This is line 17: opportunity lineopp = trigger.newmap.get(line.opportunityid);
Any idea of why and how to fix it?
You hae a typo in your code:
Change -- lines.converted_to_asset__c = true;
to--- line.converted_to_asset__c = true;
Woohoo, success! Thank you so much! I need this to only apply for one opportunity record type though to make it perfect - RecordTypeId = '012200000004fxZ'. Where would I add that in the code?
Also, could you help me with the test class aswell? My old one does not work now and I don't know how to fix it. Please, pleeeease, pleeeeeease! :)
You can just add the filter into the query (see how I did it, above).
This test method is only for positive assertion (e.g. it covers only conditions that produce code coverage). You should write similar test method for negative assertions (e.g. make sure it doesn't update assets when it's a different record type).
Thank you for all your help! I love the way you explain things.
Just have to do some final testing but all seems to be working as I want it to, and my new pretty code will soon be ready to move to production :D