You need to sign in to do that
Don't have an account?
Chris Leszka 5
Moving a trigger to Production
I created a trigger in my sandbox that creates an Asset for all renewable products in closedwon opportunity. I'm not a developer in any way, so my process for building the trigger was to search the web for some code that was close to what I needed, and modify it to match my needs. That said, the trigger works perfectly in the sandbox, but when i try to move it to production, it won't let me deploy it because it fails the test classes. I don't have any test classes built specific to this trigger, and don't really know how to/where to even begin, so i'm hoping someone here can help me.
Here is my trigger code:
Here is my trigger code:
trigger CreateAssetonClosedWon on Opportunity (after update) { map<id,map<id,asset>> assets = new map<id,map<id,asset>>(); set<id> prodids = new set<id>(); opportunitylineitem[] lines = [select id, totalprice, servicedate, service_end_date__c, quantity, pricebookentry.product2id, pricebookentry.product2.name, description, opportunityid from opportunitylineitem where opportunityid in :trigger.new and product2.business_group__c = 'TECH' and opportunity.StageName = 'Closed - Booked' and converted_to_asset__c = false]; for(opportunitylineitem line:lines) prodids.add(line.pricebookentry.product2id); for(opportunity record:trigger.new) assets.put(record.accountid,new map<id,asset>()); for(asset record:[select id, accountid, product2id, quantity, price, purchasedate, status, installdate, UsageEndDate, description, name from asset where accountid in :assets.keyset() and product2id in :prodids]) { assets.get(record.accountid).put(record.product2id,record); if(record.quantity==null) record.quantity=0; if(record.price==null) record.price=0; } for(opportunitylineitem line:lines) { opportunity lineopp = trigger.newmap.get(line.opportunityid); asset ast = assets.get(lineopp.accountid).get(line.pricebookentry.product2id); if(ast==null) ast = new asset(quantity=0,price=0); ast.accountid = lineopp.accountid; ast.product2id = line.pricebookentry.product2id; ast.quantity += line.quantity; ast.price += line.TotalPrice; ast.purchasedate = lineopp.closedate; ast.status = 'Purchased'; ast.description = line.description; ast.installdate = line.servicedate; ast.usageenddate = line.service_end_date__c; ast.source_opportunity__c = lineopp.id; ast.name = line.pricebookentry.product2.name; assets.get(lineopp.accountid).put(line.pricebookentry.product2id,ast); } asset[] updates = new asset[0]; for(id accountid:assets.keyset()) updates.addall(assets.get(accountid).values()); upsert updates; for(opportunitylineitem line:lines) line.converted_to_asset__c = true; update lines; }
Hi Chris,
To cover above trigger, you need to create Test_data for following objects:
1. Account.
2. Product
3. PricebookEntry
4. Opportunity
5. OpportunityLineItem
Hope this will help you, let me know if you need more assistance on this.
Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Please find below sample create test data:
Let me know if you face issues in this.
Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com