What this trigger is to do:
Get a related list of Opportunity Line Items IF the stage on Opportunity = Closed Won. Once I get this related list, I need to create a new Opportunity with the account field on Opportunity being equal to the Dealership__c field on Opportunity Line Item. So, for example, a dealership 'Test Dealership 1' (Which is an account type lookup) will have some opportunity line items associated with it in the Related List on an Opportunity like so:
- Order Item: Wifi
- Quantity: 2
- Ratecard: 30
- Sales Price: 360
- Total Price: 720
- Dealership: Test Dealership 1
- Order Item: Mobile
- Quantity: 4
- Ratecard: 69
- Sales Price: 828
- Total Price: 3312
- Dealership: Test Dealership 1
And there can also be another Dealership 'Test Dealership 2'
- Order Item: Laptop
- Quantity: 8
- Ratecard: 630
- Sales Price: 630
- Total Price: 5040
- Dealership: Test Dealership 2
What needs to happen:
One new opportunity created with account name = Dealership 1, this opportunity should have a related list of opportunity line items with products Wifi and Mobile.
Then another new opportunity should be created with account name = Dealership 2 with a related list of products containing Laptop as an opportunity line item.
Here is my code:
trigger ParentOppToChild on Opportunity (after update) { Map<ID, Opportunity> oppList = new Map<ID, Opportunity>(); List<OpportunityLineItem> listOfOpportunityLineItems = new List<OpportunityLineItem>(); for(Opportunity opp: Trigger.new) { if(opp.StageName == 'Closed Won') { for(OpportunityLineItem oli : [Select OpportunityId, Quantity From OpportunityLineItem Where OpportunityId In : oppList.keyset()]) { Opportunity OppObj = oppList.get(oli.OpportunityId); listOfOpportunityLineItems.add(oli); } Update listOfOpportunityLineItems; for(OpportunityLineItem lineItem : listOfOpportunityLineItems) { if(lineItem.Dealership__c != Null) { Opportunity newOpp = new Opportunity(); newOpp.Account_holder_name__c = lineItem.Dealership__c; newOpp.Name = lineItem.Dealership__c; newOpp.CloseDate = Date.today(); newOpp.StageName = 'Prospecting'; newOpp.ForecastCategoryName = 'Commit'; } } } } } Is my code on the right track to doing this? If anyone can help or let me know what I basically need to do I would really appreciate it. Thanks in advance!
Test class:
@isTest public class ParentOppToChildTest { static testMethod void triggerTestMethod() { test.startTest(); Id pricebookId = Test.getStandardPricebookId(); //Create product Product2 prod = new Product2(Name = 'Product X', ProductCode = 'Pro-X', isActive = true); insert prod; //Create pricebook entry PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId,Product2Id = prod.Id,UnitPrice = 100.00,IsActive = true); insert pbEntry; Opportunity oppty = new Opportunity(); Opportunity opp = new Opportunity(AccountId='0012500000C5G06', Name='Test Opp',StageName='Closed Won', CloseDate=Date.today()); insert opp; //Create opportunity line item OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, Dealership__c = opp.AccountId, Quantity = 5,PricebookEntryId = pbEntry.Id,TotalPrice = 5 * pbEntry.UnitPrice); insert oli; system.debug(oli.Dealership__c); test.stopTest(); } } Test coverage with this test class is 57%