• smohyee@ihrdc.com
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies

I'm writing a simple trigger/class that, upon creation, editing or deletion of a Contract, counts all the 'Active' contracts underneath the related Account and updates the appropriate field on the Account record. 

 

I was up all last night trying to figure out the test failure errors I was getting, to no avail. I have a feeling somone on here will recognize the problem within 30 seconds =). By the way, I'm a newbie trying to learn, so any comments as to style, form and functionality are appreciated!

 

The error message is: 

 

Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contract_Triggers: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Contract_Handler.Count_Active_Contracts: line 14, column 1
Trigger.Contract_Triggers: line 11, column 1: []
Stack Trace	
Class.Contract_Test_Data.createAccountWithContracts: line 16, column 1 Class.Contract_Test_Methods.testContractCount_case1: line 9, column 1

 

I believe the problem lies somewhere between the actual Class and the Test Class responsible for creating test data. 

 

Here is the current form of the Contract_Handler class:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Account acc)

/*This method is passed an Account object, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses.*/ Integer count = 0; Date today = Date.today(); //ATTENTION: I Believe the issue has to do with the following for loop, due to acc.Contracts not having any //members (I tried a SOQL For Loop first and got the same error). for (Contract c : acc.Contracts){ if(today >= c.StartDate && today <= c.EndDate ){ count++; } } acc.Active_Contracts__c = count; update acc; return count; } }

 

Here is the test data I'm generating. ONLY TEST CASE == 1 is being tested right now, so I've omitted the code for the other cases:

 

@isTest
public class Contract_Test_Data{

    public static Account createAccountWithContracts(Integer testCase){
        /*3 test cases, based on types of contracts associated with account:
			1)Two Contracts, neither are active, one is expired, one is upcoming 
			2)Three Contracts, one active, one expired, one upcoming 
			3)Two Contracts, both active
		*/
        
        Account testAccount = createAccountRecord();
 
        
        if(testCase == 1){

            insert new Contract[]{new Contract(AccountId = testAccount.Id,
                                               Account = testAccount,
                			       StartDate = date.parse('01/01/2009'),
                                               ContractTerm = 12), 
                     	         new Contract(AccountId = testAccount.Id, 
                                               Account = testAccount,
                			       StartDate = date.parse('10/01/2014'),
                                               ContractTerm = 12)};
            
        }

        return testAccount;        	
    }
    
    
    private static Account createAccountRecord(){
        Account acc = new Account(Name = 'Test');
        
        insert acc;
        return acc;        
    }

    
}

 

 

My test trigger calls Contract_Test_Data.createAccountWithContracts(1), which causes the creation of the test Account record, along with two Contract records which will both qualify as INACTIVE. Inserting these contract records triggers a for loop through those contracts, with the method Contract_Handler.Count_Active_Contracts being passed their parent account.

 

 

An aside on Checkpoints:  Debuggin in the Developer Console seems to be flaky and a pain. I tried setting checkpoints in my various triggers/classes, and only got results a few times when running the tests. Also, I tried to add an action script to the checkpoints to run APEX code (a system.debug call on some variables), but I have no idea where those logs would appear. Documentation seems scarce, can anyone point me to anything other than the one paragraph on the Developer Console help page? 

 

 

Thank you all for reading, and for your help!

I'm writing a simple trigger/class that, upon creation, editing or deletion of a Contract, counts all the 'Active' contracts underneath the related Account and updates the appropriate field on the Account record. 

 

I was up all last night trying to figure out the test failure errors I was getting, to no avail. I have a feeling somone on here will recognize the problem within 30 seconds =). By the way, I'm a newbie trying to learn, so any comments as to style, form and functionality are appreciated!

 

The error message is: 

 

Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contract_Triggers: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Contract_Handler.Count_Active_Contracts: line 14, column 1
Trigger.Contract_Triggers: line 11, column 1: []
Stack Trace	
Class.Contract_Test_Data.createAccountWithContracts: line 16, column 1 Class.Contract_Test_Methods.testContractCount_case1: line 9, column 1

 

I believe the problem lies somewhere between the actual Class and the Test Class responsible for creating test data. 

 

Here is the current form of the Contract_Handler class:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Account acc)

/*This method is passed an Account object, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses. - Sam Mohyee 7/18/2013*/ Integer count = 0; Date today = Date.today(); //ATTENTION: I Believe the issue has to do with this for loop, due to acc.Contracts not having any members (I tried a SOQL For Loop first and got the same error). for (Contract c : acc.Contracts){ if(today >= c.StartDate && today <= c.EndDate ){ count++; } } acc.Active_Contracts__c = count; update acc; return count; } }

 

Here is the test data I'm generating. ONLY TEST CASE == 1 is being tested right now, so I've omitted the code for the other cases:

 

@isTest
public class Contract_Test_Data{

    public static Account createAccountWithContracts(Integer testCase){
        /*3 test cases, based on types of contracts associated with account:
			1)Two Contracts, neither are active, one is expired, one is upcoming 
			2)Three Contracts, one active, one expired, one upcoming 
			3)Two Contracts, both active
		*/
        
        Account testAccount = createAccountRecord();
 
        
        if(testCase == 1){

            insert new Contract[]{new Contract(AccountId = testAccount.Id,
                                               Account = testAccount,
                			       StartDate = date.parse('01/01/2009'),
                                               ContractTerm = 12), 
                     	         new Contract(AccountId = testAccount.Id, 
                                               Account = testAccount,
                			       StartDate = date.parse('10/01/2014'),
                                               ContractTerm = 12)};
            
        }

        return testAccount;        	
    }
    
    
    private static Account createAccountRecord(){
        Account acc = new Account(Name = 'Test');
        
        insert acc;
        return acc;        
    }

    
}

 

 

My test trigger calls Contract_Test_Data.createAccountWithContracts(1), which causes the creation of the test Account record, along with two Contract records which will both qualify as INACTIVE. 

I'm writing a simple trigger/class that, upon creation, editing or deletion of a Contract, counts all the 'Active' contracts underneath the related Account and updates the appropriate field on the Account record. 

 

I was up all last night trying to figure out the test failure errors I was getting, to no avail. I have a feeling somone on here will recognize the problem within 30 seconds =). By the way, I'm a newbie trying to learn, so any comments as to style, form and functionality are appreciated!

 

The error message is: 

 

Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contract_Triggers: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Contract_Handler.Count_Active_Contracts: line 14, column 1
Trigger.Contract_Triggers: line 11, column 1: []
Stack Trace	
Class.Contract_Test_Data.createAccountWithContracts: line 16, column 1 Class.Contract_Test_Methods.testContractCount_case1: line 9, column 1

 

I believe the problem lies somewhere between the actual Class and the Test Class responsible for creating test data. 

 

Here is the current form of the Contract_Handler class:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Account acc)

/*This method is passed an Account object, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses.*/ Integer count = 0; Date today = Date.today(); //ATTENTION: I Believe the issue has to do with the following for loop, due to acc.Contracts not having any //members (I tried a SOQL For Loop first and got the same error). for (Contract c : acc.Contracts){ if(today >= c.StartDate && today <= c.EndDate ){ count++; } } acc.Active_Contracts__c = count; update acc; return count; } }

 

Here is the test data I'm generating. ONLY TEST CASE == 1 is being tested right now, so I've omitted the code for the other cases:

 

@isTest
public class Contract_Test_Data{

    public static Account createAccountWithContracts(Integer testCase){
        /*3 test cases, based on types of contracts associated with account:
			1)Two Contracts, neither are active, one is expired, one is upcoming 
			2)Three Contracts, one active, one expired, one upcoming 
			3)Two Contracts, both active
		*/
        
        Account testAccount = createAccountRecord();
 
        
        if(testCase == 1){

            insert new Contract[]{new Contract(AccountId = testAccount.Id,
                                               Account = testAccount,
                			       StartDate = date.parse('01/01/2009'),
                                               ContractTerm = 12), 
                     	         new Contract(AccountId = testAccount.Id, 
                                               Account = testAccount,
                			       StartDate = date.parse('10/01/2014'),
                                               ContractTerm = 12)};
            
        }

        return testAccount;        	
    }
    
    
    private static Account createAccountRecord(){
        Account acc = new Account(Name = 'Test');
        
        insert acc;
        return acc;        
    }

    
}

 

 

My test trigger calls Contract_Test_Data.createAccountWithContracts(1), which causes the creation of the test Account record, along with two Contract records which will both qualify as INACTIVE. Inserting these contract records triggers a for loop through those contracts, with the method Contract_Handler.Count_Active_Contracts being passed their parent account.

 

 

An aside on Checkpoints:  Debuggin in the Developer Console seems to be flaky and a pain. I tried setting checkpoints in my various triggers/classes, and only got results a few times when running the tests. Also, I tried to add an action script to the checkpoints to run APEX code (a system.debug call on some variables), but I have no idea where those logs would appear. Documentation seems scarce, can anyone point me to anything other than the one paragraph on the Developer Console help page? 

 

 

Thank you all for reading, and for your help!

I'm writing a simple trigger/class that, upon creation, editing or deletion of a Contract, counts all the 'Active' contracts underneath the related Account and updates the appropriate field on the Account record. 

 

I was up all last night trying to figure out the test failure errors I was getting, to no avail. I have a feeling somone on here will recognize the problem within 30 seconds =). By the way, I'm a newbie trying to learn, so any comments as to style, form and functionality are appreciated!

 

The error message is: 

 

Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contract_Triggers: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Contract_Handler.Count_Active_Contracts: line 14, column 1
Trigger.Contract_Triggers: line 11, column 1: []
Stack Trace	
Class.Contract_Test_Data.createAccountWithContracts: line 16, column 1 Class.Contract_Test_Methods.testContractCount_case1: line 9, column 1

 

I believe the problem lies somewhere between the actual Class and the Test Class responsible for creating test data. 

 

Here is the current form of the Contract_Handler class:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Account acc)

/*This method is passed an Account object, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses. - Sam Mohyee 7/18/2013*/ Integer count = 0; Date today = Date.today(); //ATTENTION: I Believe the issue has to do with this for loop, due to acc.Contracts not having any members (I tried a SOQL For Loop first and got the same error). for (Contract c : acc.Contracts){ if(today >= c.StartDate && today <= c.EndDate ){ count++; } } acc.Active_Contracts__c = count; update acc; return count; } }

 

Here is the test data I'm generating. ONLY TEST CASE == 1 is being tested right now, so I've omitted the code for the other cases:

 

@isTest
public class Contract_Test_Data{

    public static Account createAccountWithContracts(Integer testCase){
        /*3 test cases, based on types of contracts associated with account:
			1)Two Contracts, neither are active, one is expired, one is upcoming 
			2)Three Contracts, one active, one expired, one upcoming 
			3)Two Contracts, both active
		*/
        
        Account testAccount = createAccountRecord();
 
        
        if(testCase == 1){

            insert new Contract[]{new Contract(AccountId = testAccount.Id,
                                               Account = testAccount,
                			       StartDate = date.parse('01/01/2009'),
                                               ContractTerm = 12), 
                     	         new Contract(AccountId = testAccount.Id, 
                                               Account = testAccount,
                			       StartDate = date.parse('10/01/2014'),
                                               ContractTerm = 12)};
            
        }

        return testAccount;        	
    }
    
    
    private static Account createAccountRecord(){
        Account acc = new Account(Name = 'Test');
        
        insert acc;
        return acc;        
    }

    
}

 

 

My test trigger calls Contract_Test_Data.createAccountWithContracts(1), which causes the creation of the test Account record, along with two Contract records which will both qualify as INACTIVE. 

I want to concatenate two object IDs in order to make a hash value for a map, but this doesn't work:

 

conversionMap.put(entry.Pricebook2Id + entry.Product2Id), entry);

If I could convert the IDs to strings first, then I should be able to concatenate them in this way.  But, while there is a function to convert a string to an ID, I can't find any way to convert an ID to a string.  Anybody know how to do that?  Or, a better way to do what I'm trying to do?