2

I have written a following trigger code -

trigger manageEmailCases on Case (before insert)
{
    for(Case thisCase : trigger.new) 
    {
        if(thisCase.Origin == 'Call-Center')
        {
            // system.debug('Origin is Call Center');
            // Check for invalid email account
            if(!thisCase.SuppliedEmail.contains('directlineinc'))
            {
                if(!Test.isRunningTest())
                {
                    thisCase.addError('Not saving e2c');
                    // thisCase.Support_Alert__c.addError('Not saving email2case');
                }
            }
        }
    }
}

I have this following EXISTING testMethod written for some ANOTHER class -

@IsTest(SeeAllData=false) public static void testHandleExistingCase ()
{
    // Set up the object.
    CallCenterCaseNotification testObj = new CallCenterCaseNotification();
    testObj.populateSreContactInfo(Datetime.valueOf('2013-08-20 16:35:00'));

    String description = 'account_name: testcorp customer_name: sammmmmm ' +
                     'customer_phone: +1 (707) 477-3205 ' +
                     'customer_email: something' +
                     'service_impacted: yes case_id:123 ' +
                     'case_description: testing';
    // Set up parameters for the call to method.
    Case existingCase = new Case();
    existingCase.Origin = 'Call-Center';
    existingCase.Subject = 'Test case';
    existingCase.Status = 'Closed';
    // Select an SRE to use as the OwnerId
    User testOwner = [SELECT Id FROM User WHERE UserRoleId = '00E60000001GX26EAG' LIMIT 1];
    existingCase.OwnerId = testOwner.Id;// testObj.UNASSIGNED_OWNER_ID;
    existingCase.Description = description;
    insert existingCase;  // Need to insert, because methods do a Query for this
                          // case object. So cannot mock out.                   
    String customerInfoForSMS = '\ncustomer_name \n@ account_name \n' +
                                ' 123-234-1234';
    testObj.handleExistingCase(new Case[] {existingCase}, existingCase,
                               'no', 'P3 - Normal', customerInfoForSMS,
                               'someemail@test.com');
    }
}

When I test the class which has this testMethod, it gives me following error, which is also not allowing me to deploy -

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

Kindly advise what could be wrong with my trigger written above.

2

The trouble is in the following line:

 if(!thisCase.SuppliedEmail.contains('directlineinc'))

In your test you didn't initatlized this field thus this field is null and you tried to call contains() on null.

You have to fill this field in your test:

existingCase.SuppliedEmail= 'test@test.com';

But more correct way for fix this trouble is checking that the SuppliedEmail has a value

if(thisCase.SuppliedEmail != null && !thisCase.SuppliedEmail.contains('directlineinc'))
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.