You need to sign in to do that
Don't have an account?
cml9
Loop trigger in opportunity when Opportunity was Updated
Hi Masters,
I have successfully created this trigger and it work as what I suppose to but on my test I only have 5 percent coverage. As per my understanding I need to insert a new Opportunity first then update it.Lastly, create another Opportunity. Please correct me if this is wrong. Below is my trigger and my suppose test that is not working.
Thanks!
Trigger
Test
I have successfully created this trigger and it work as what I suppose to but on my test I only have 5 percent coverage. As per my understanding I need to insert a new Opportunity first then update it.Lastly, create another Opportunity. Please correct me if this is wrong. Below is my trigger and my suppose test that is not working.
Thanks!
Trigger
trigger Contracted_Opportunities on Opportunity (after update) { for(Opportunity o: trigger.new){ if (o.isContracted__c == True && o.StageName=='Sold') { if (o.Payment_Frequency__c == 'Monthly - Locked In (6)'){ integer x = 5; double z = double.valueOf(o.Total_Amount_w_o_GST__c); date closedate = date.valueOf(o.CloseDate); integer payment = 2; integer num = 1; for (integer y =x; y>=1;y--) { Opportunity newOpp = new Opportunity(); newOpp.Name = 'Monthly Contracted '+ payment; newOpp.Amount=o.Total_Amount_w_o_GST__c; newOpp.StageName='Sold'; newOpp.AccountId=o.AccountId; newOpp.Contact__c=o.Contact__c; newOpp.CloseDate =closedate.addMonths(1); insert NewOpp; closedate = closeDate.addMonths(1); payment++; num++; } } if (o.Payment_Frequency__c == 'Monthly - Locked In (12)'){ integer x = 11; double z = double.valueOf(o.Total_Amount_w_o_GST__c); date closedate = date.valueOf(o.CloseDate); integer payment = 2; integer num = 1; for (integer y =x; y>=1;y--) { Opportunity newOpp = new Opportunity(); newOpp.Name = 'Monthly Contracted '+ payment; newOpp.Amount=o.Total_Amount_w_o_GST__c; newOpp.StageName='Sold'; newOpp.AccountId=o.AccountId; newOpp.Contact__c=o.Contact__c; newOpp.CloseDate =closedate.addMonths(1); insert NewOpp; closedate = closeDate.addMonths(1); payment++; num++; } } } } }
Test
@isTest public class ContractedOppsTest { static testMethod void insertNewOpps() { Account a = new Account(); a.Name = 'Account'; insert a; Contact c = new Contact(); c.LastName = 'Kash'; c.AccountId = a.Id; insert c; List<Opportunity> o = new List<Opportunity>(); o.add(new Opportunity( Name = 'My Opps', StageName = 'New', CloseDate = Date.today().addDays(7), Contact__c = c.Id, AccountId = a.Id, Amount = 100)); insert o; List <Opportunity> oppids = [ select id from Opportunity where Id = :o[0].Id]; for (Opportunity UpOpp: oppids) { UpOpp.isContracted__c = False; UpOpp.Payment_Frequency__c = 'Monthly - Locked In (6)'; UpOpp.StageName = 'Sold'; update UpOpp; Opportunity NewOpp = new Opportunity(); NewOpp.Name='New Opp1'; NewOpp.StageName='Sold'; Newopp.Amount=upOpp.Total_Amount_w_o_GST__c; NewOpp.Contact__c=upopp.Contact__c; NewOpp.AccountId=upopp.AccountId; insert NewOpp; } } }
To put it simply, your trigger is doing what it's asked to do. You have a few conditional if statements in your code, so you will need to insert or update records to meet that criteria. You should also add System assert calls to make sure your code is working as you intend it to. I've taken a stab at both of these for you below: