• ronaldheft
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I'm working with an organization that has a large amount of Apex code developed with previous contractors, and I've run into some issues with getting my new code playing nice with the existing Apex.

 

I've written a new controller which requires an Account object for testing. I'm creating the Account object as follows:

public static Account getAccount(){
    return new Account(
        Name = 'Test Acc EL CH FB',
        Squad_Class__c = 'CH - Cheer',
        Squad_Type__c = 'EL - Elementary',
        Squad_Group__c = 'FB - Football',
        BillingStreet = 'Test Bill Address',
        BillingCity = 'Test Bill City',
        BillingState = 'CA',
        BillingCountry = 'US',
        Billing_County__c = 'US',
        BillingPostalCode  = '523622',
        RecordTypeId = [select Id from RecordType where SObjectType ='Account' and Name = 'Squads/Teams' Limit 1][0].Id
    );
}

 

This is called from the test as so:

Account acc = AllStarTestUtils.getAccount();
insert acc;

 

On insert an existing Trigger written by a previous contractor is run:

trigger NewAccountTrigger on Account (before insert) {
    AccountSettings__c acctSettings = AccountSettings__c.getOrgDefaults();
    Double CustAutoNum = acctSettings.DB2_Customer_Number_Auto__c;
    ...

 

When I run my test I'm seeing the code bomb out on line 3 of the existing Trigger with the following exception:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewAccountTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.NewAccountTrigger: line 3, column 1: []

 

From what I can tell, the custom settings object AccountSettings__c is null when running the test. I've verified the custom setting exists in my sandbox, and when I output the following code in the Debug Console, the logs are returning the expected value:

System.debug(AccountSettings__c.getOrgDefaults());
System.debug(AccountSettings__c.getOrgDefaults().DB2_Customer_Number_Auto__c);

 

So, it looks like everything should be working fine, but obviously this null pointer is causing problems. I read in the documentation for getOrgDefaults there was a change in the what getOrgDefaults returns when empty. In v21 or prior a NULL was returned. The existing Trigger is versioned at v20 and my new code is versioned at v24. If I bump my new code down to v20 the test passes, but it does not work in reverse; if I increase the existing trigger to v24 I still get a null pointer.

 

I should mention this Trigger is working fine outside the context of the test. Any ideas?

I want to create a public force.com site where the users are not required to be logged in and would use Guest user licenses.

I want to store their selections made on the site for the duration of the session and save the selection in the force.com object.

 

In most of the earlier posts, I see that there is discussion on maintaining session for logged in users, but there is no information about session management for unauthenticated users.

 

Is there a way out?

 

Ady

  • November 22, 2011
  • Like
  • 0