I am trying to update business unit field in business_unit__c custom object. for this we have used custom settings in trigger and if manually any sales rep is creating based on their role we created a new process builder to update the field. Now, I am getting this error in test class if process builder is active.

Below is my test class:

private class BusinessUnitTriggerHandlerTest 
    static testMethod void testCheckDuplicate() 

        Profile profile = [Select Id from Profile where name = 'ORT Direct Sales User'];
        User nonAdminUser = new User( ProfileId = profile.Id, Username = System.now().millisecond() + 'test2@test.com.dev',UserRoleId = '00E1G000000IX0IUAW',
                                    Alias = 'batman', Email='bruce.wayne@wayneenterprises.com', EmailEncodingKey='UTF-8',Firstname='Bruce',
                                    Lastname='Wayne',LanguageLocaleKey='en_US',LocaleSidKey='en_US',TimeZoneSidKey='America/Chicago' );
        System.runAs(new User( Id = UserInfo.getUserId() ))

        Account acc = TestUtility.createAccount( TestUtility.default_account_rt, false );
        acc.Name = 'sfdcpoint';
        acc.Account_Status__c = 'Active';
        insert acc; 

            Contact cont = TestUtility.createContact( TestUtility.default_contact_rt , acc, false ); 
            cont.MailingStreet = 'Test Street' ;
            cont.MailingCity = 'Minneapolis';
            cont.MailingState = 'MN';
            cont.MailingPostalCode = '55347';
            cont.MailingCountry = 'United States' ; 
            insert cont;

            BusinessUnits__c bu = new BusinessUnits__c( Contact__c = cont.Id, LineOfBusiness__c = 'Central Title Division' );
            bu.Sales_Rep__r.UserRole.DeveloperName = 'Central_Title_Agency1';
            insert bu ; 

                BusinessUnits__c bu1 = new BusinessUnits__c( Contact__c = cont.Id, LineOfBusiness__c = 'Central Title Division' );
                bu1.Sales_Rep__r.UserRole.DeveloperName= 'Central_Title_Agency1';
                insert bu1 ;  
            catch(DmlException de )



The error message I am receiving is:

System.NullPointerException: Attempt to de-reference a null object Class.BusinessUnitTriggerHandlerTest.testCheckDuplicate: line 41, column 1 Line 41 in my test class is: bu.Sales_Rep__r.UserRole.DeveloperName = 'Central_Title_Agency1'


This relationship field bu.Sales_Rep__r will only be populated in your test if:

  • A relationship has been established by some logic that sets the Sales_Rep__c field first
  • The test code then does a query that includes the related object

Otherwise bu.Sales_Rep__r will be null resulting in the error you see.

  • I tried that but I was not able to save successfully, it's throwing me "Variable doesn't exists error". can you expand your answer with any example,? – Ordev Jul 17 '19 at 20:53

You need to query for the User Role the same way you queried for the Profile in line 7 of your test method and assign that variable to bu.Sales_Rep__r.UserRole.

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.