+ Start a Discussion
cml9cml9 

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
 
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;
            }
      
      
    }
   
  
}

 
James LoghryJames Loghry
Hi there.

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:
 
@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;
        
        
       Test.startTest();
       List<Opportunity> ol = new List<Opportunity>();
       ol.add(
           new Opportunity(
       
                Name = 'My Opps',
               StageName = 'New',
               CloseDate = Date.today().addDays(7),
               Contact__c = c.Id,
               AccountId = a.Id,
               Amount = 100,
               Total_Amount_w_o_GST__c = 50.0
           )
       );

       ol.add(
           new Opportunity(
       
                Name = 'My Opps',
               StageName = 'New',
               CloseDate = Date.today().addDays(7),
               Contact__c = c.Id,
               AccountId = a.Id,
               Amount = 100,
               Total_Amount_w_o_GST__c = 75.0
           )
       );     
       insert ol;
        
       //Tests Line 4 - 32
       ol.get(0).IsContracted__c = true;
       ol.get(0).StageName = 'Sold';
       ol.get(0).Payment_Frequency__c = 'Monthly - Locked In (6)';

       //Tests Line 34 - 60ish
       ol.get(1).IsContracted__c = true;
       ol.get(1).StageName = 'Sold';
       ol.get(1).Payment_Frequency__c = 'Monthly - Locked in (12)';
       update ol;
       Test.stopTest();

       //Assert that your opportunities were created correctly.
       List<Opportunity> sixMonthCreatedOpps = [Select Id From Opportunity Where Name like 'Monthly Contracted %' And Amount = 50.0];
       System.assertEquals(6,sixMonthCreatedopps.size());

        List<Opportunity> twelveMonthCreatedOpps = [Select Id From Opportunity Where Name like 'Monthly Contracted %' And Amount = 75.0];
        System.assertEquals(12,sixMonthCreatedopps.size());

   }
}

 
James LoghryJames Loghry
I should also note that in your trigger, you have a insert that's inside a for loop (two actually).  You'll want to use lists and move your insert calls outside of the for loop.  This will help "bulkify" your trigger and avoid DML governor limits / exceptions.