• jladders
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Any help/idea will be more than appreciated! I have a trigger that inserts a new opportunity record once stage is set to closedwon, it works fine until i try to set the newly inserted record to closewon. I get the following error message
 
System.NullPointerException: Attempt to de-reference a null object: Trigger.RenewalOpportunity: line 9, column 48
 
contract_length__c is a number field while contract_activation_date is a date field. below is the code I have so far
 
Trigger
Code:
trigger RenewalOpportunity on Opportunity (after update) {
                        
    for (Integer i = 0; i < Trigger.new.size(); i++) {
               
       if (Trigger.new[i].isWon == true && Trigger.old[i].isWon == false && Trigger.new[i].RenewalOpportunity__c == null) {
            
            Opportunity renewalOpp = Trigger.new[i].clone(false);

            Double x = Trigger.old[i].Contract_Length__c;
            Integer y = x.intValue();
            
            renewalOpp.Name = 'Renewal - '+  Trigger.old[i].Name;
            renewalOpp.closedate = Trigger.old[i].Contract_Activation_Date__c.addmonths(y);
            renewalOpp.stagename = '0 - Very Early Stage';
            renewalOpp.probability = 0;
            renewalOpp.ForecastCategoryName = 'Pipeline';
            renewalOpp.Contract_Activation_Date__c = null;
            renewalOpp.Contract_Length__c = null;


            insert renewalOpp;
            
      if (Trigger.old[i].Name.startsWith('Renewal - ')) {
          renewalOpp.Name = Trigger.old[i].Name;
          update renewalOpp;
          }

      if (Trigger.new[i].isWon == true && Trigger.old[i].isWon == false && Trigger.new[i].RenewalOpportunity__c == null) {
            Opportunity newOpp = [select ID from Opportunity where id = :Trigger.new[i].ID];
            newOpp.RenewalOpportunity__c = renewalOpp.ID;
            update newOpp;
            }            
      }
   }
}

 
Unit Test
 
Code:
public class CreateOpportunity {

    static testMethod void testCreateOpportunityRenewal(){
    // Test opportunities moving from isWon = false to isWon = true
    try {
        
        Double Contract_Length = 13;
        Double x = Contract_Length;
        Integer y = x.intValue();
        
        Date today = System.today();
        Date contract_activation_date = System.today();
        Date closeDate = contract_activation_date.addmonths(y);
                 
        Opportunity opportunity = new Opportunity();
        
        opportunity.name = 'old opportunity name';
        opportunity.Contract_Length__c = 13;
        opportunity.contract_activation_date__c = today;
        opportunity.closedate = today;
        opportunity.stagename = 'Closed Lost';
        opportunity.probability = 1;
        opportunity.ForecastCategoryName = 'Closed';
                
        insert opportunity;
        
        System.assertEquals(opportunity.name, 'old opportunity name');

        opportunity.stagename = 'Closed Won';
        update opportunity;
        
        Opportunity updatedOpportunity = [select ID,RenewalOpportunity__c from Opportunity where id = :opportunity.ID];
        
        // go get new opportunity
        System.debug('opportunityId: ' + updatedOpportunity.RenewalOpportunity__c);
        Opportunity newOpportunity = [select ID, Name, Contract_Length__c, contract_activation_date__c, closedate, stagename, probability, forecastCategoryName from Opportunity where id = :updatedOpportunity.RenewalOpportunity__c];
        
        System.assertNotEquals(newOpportunity, null);
        
        System.assertEquals(newOpportunity.name, 'Renewal - old opportunity name');
        System.assertEquals(newOpportunity.Contract_Length__c, 12);        
        System.assertEquals(newOpportunity.contract_activation_date__c, today);
        System.assertEquals(newOpportunity.closedate, closeDate);
        System.assertEquals(newOpportunity.stagename, '0 - Very Early Stage');
        System.assertEquals(newOpportunity.probability, 0);
        System.assertEquals(newOpportunity.ForecastCategoryName, 'Pipeline');
        
        delete opportunity;
    }    
    catch (DmlException e) {
        System.assert(false);
    }

    // Test opportunities moving from isWon = false to isWon = false
    try {

        Double Contract_Length = 13;
        Double x = Contract_Length;
        Integer y = x.intValue();
        
        Date today = System.today();
        Date contract_activation_date = System.today();
        Date closeDate = contract_activation_date.addmonths(y);
            
        Opportunity opportunity = new Opportunity();
        opportunity.Contract_Length__c = 13;
        opportunity.contract_activation_date__c = today;
        opportunity.name = 'old opportunity name';
        opportunity.closedate = today;
        opportunity.stagename = 'Closed Lost';
        opportunity.probability = 1;
        opportunity.ForecastCategoryName = 'Closed';

        insert opportunity;
        
        System.assertEquals(opportunity.name, 'old opportunity name');
        
        opportunity.stagename = 'Final Steps';
        update opportunity;
        
        Opportunity updatedOpportunity = [select ID,RenewalOpportunity__c from Opportunity where id = :opportunity.ID];
        try
        {
            Opportunity newOpportunity = [select ID from Opportunity where id = :updatedOpportunity.RenewalOpportunity__c];
            System.assert(false);
        }
        catch (Exception e) {
        }
    
        delete opportunity;
    }    
    catch (DmlException e) {
        System.assert(false);
    }
  }
}

 
Thanks in adavance :)
Any help/idea will be more than appreciated! I have a trigger that inserts a new opportunity record once stage is set to closedwon, it works fine until i try to set the newly inserted record to closewon. I get the following error message
 
System.NullPointerException: Attempt to de-reference a null object: Trigger.RenewalOpportunity: line 9, column 48
 
contract_length__c is a number field while contract_activation_date is a date field. below is the code I have so far
 
Trigger
Code:
trigger RenewalOpportunity on Opportunity (after update) {
                        
    for (Integer i = 0; i < Trigger.new.size(); i++) {
               
       if (Trigger.new[i].isWon == true && Trigger.old[i].isWon == false && Trigger.new[i].RenewalOpportunity__c == null) {
            
            Opportunity renewalOpp = Trigger.new[i].clone(false);

            Double x = Trigger.old[i].Contract_Length__c;
            Integer y = x.intValue();
            
            renewalOpp.Name = 'Renewal - '+  Trigger.old[i].Name;
            renewalOpp.closedate = Trigger.old[i].Contract_Activation_Date__c.addmonths(y);
            renewalOpp.stagename = '0 - Very Early Stage';
            renewalOpp.probability = 0;
            renewalOpp.ForecastCategoryName = 'Pipeline';
            renewalOpp.Contract_Activation_Date__c = null;
            renewalOpp.Contract_Length__c = null;


            insert renewalOpp;
            
      if (Trigger.old[i].Name.startsWith('Renewal - ')) {
          renewalOpp.Name = Trigger.old[i].Name;
          update renewalOpp;
          }

      if (Trigger.new[i].isWon == true && Trigger.old[i].isWon == false && Trigger.new[i].RenewalOpportunity__c == null) {
            Opportunity newOpp = [select ID from Opportunity where id = :Trigger.new[i].ID];
            newOpp.RenewalOpportunity__c = renewalOpp.ID;
            update newOpp;
            }            
      }
   }
}

 
Unit Test
 
Code:
public class CreateOpportunity {

    static testMethod void testCreateOpportunityRenewal(){
    // Test opportunities moving from isWon = false to isWon = true
    try {
        
        Double Contract_Length = 13;
        Double x = Contract_Length;
        Integer y = x.intValue();
        
        Date today = System.today();
        Date contract_activation_date = System.today();
        Date closeDate = contract_activation_date.addmonths(y);
                 
        Opportunity opportunity = new Opportunity();
        
        opportunity.name = 'old opportunity name';
        opportunity.Contract_Length__c = 13;
        opportunity.contract_activation_date__c = today;
        opportunity.closedate = today;
        opportunity.stagename = 'Closed Lost';
        opportunity.probability = 1;
        opportunity.ForecastCategoryName = 'Closed';
                
        insert opportunity;
        
        System.assertEquals(opportunity.name, 'old opportunity name');

        opportunity.stagename = 'Closed Won';
        update opportunity;
        
        Opportunity updatedOpportunity = [select ID,RenewalOpportunity__c from Opportunity where id = :opportunity.ID];
        
        // go get new opportunity
        System.debug('opportunityId: ' + updatedOpportunity.RenewalOpportunity__c);
        Opportunity newOpportunity = [select ID, Name, Contract_Length__c, contract_activation_date__c, closedate, stagename, probability, forecastCategoryName from Opportunity where id = :updatedOpportunity.RenewalOpportunity__c];
        
        System.assertNotEquals(newOpportunity, null);
        
        System.assertEquals(newOpportunity.name, 'Renewal - old opportunity name');
        System.assertEquals(newOpportunity.Contract_Length__c, 12);        
        System.assertEquals(newOpportunity.contract_activation_date__c, today);
        System.assertEquals(newOpportunity.closedate, closeDate);
        System.assertEquals(newOpportunity.stagename, '0 - Very Early Stage');
        System.assertEquals(newOpportunity.probability, 0);
        System.assertEquals(newOpportunity.ForecastCategoryName, 'Pipeline');
        
        delete opportunity;
    }    
    catch (DmlException e) {
        System.assert(false);
    }

    // Test opportunities moving from isWon = false to isWon = false
    try {

        Double Contract_Length = 13;
        Double x = Contract_Length;
        Integer y = x.intValue();
        
        Date today = System.today();
        Date contract_activation_date = System.today();
        Date closeDate = contract_activation_date.addmonths(y);
            
        Opportunity opportunity = new Opportunity();
        opportunity.Contract_Length__c = 13;
        opportunity.contract_activation_date__c = today;
        opportunity.name = 'old opportunity name';
        opportunity.closedate = today;
        opportunity.stagename = 'Closed Lost';
        opportunity.probability = 1;
        opportunity.ForecastCategoryName = 'Closed';

        insert opportunity;
        
        System.assertEquals(opportunity.name, 'old opportunity name');
        
        opportunity.stagename = 'Final Steps';
        update opportunity;
        
        Opportunity updatedOpportunity = [select ID,RenewalOpportunity__c from Opportunity where id = :opportunity.ID];
        try
        {
            Opportunity newOpportunity = [select ID from Opportunity where id = :updatedOpportunity.RenewalOpportunity__c];
            System.assert(false);
        }
        catch (Exception e) {
        }
    
        delete opportunity;
    }    
    catch (DmlException e) {
        System.assert(false);
    }
  }
}

 
Thanks in adavance :)