0

I' have written a trigger on lead, very simple. I'am using the custom setting for the first tine to avoid hard coding or query to get ids. I haven't experience testing custom setting.

I have a problem with my test class,i get the following error:

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

reference line for the error is l.OwnerId=cs.OwnerId__c;

Plase can you take a look to my test class code:

MY TRIGGER:

    trigger LeadSetOwner on Lead (before update) {

       //GET USER ID AND RECORD TYPE ID FROM CUSTOM SETTINGS.
       SetOwner__c cs = SetOwner__c.getValues('standardset');

        for (Lead l: Trigger.new){

            //GET OLD LEAD'S RECORD
            Lead  oldLead = Trigger.oldMap.get(l.Id);

            //CHECK IF LEAD STATUS IS CHANGED TO 'Archived' OR 'False'        
            if ((l.Status=='Archived' || l.Status=='False'||l.Status=='Lead - Never') && (oldLead.Status!= l.Status)) {                                              
                     l.Archived_Original_Owner__c = l.OwnerId;
                       // ASSIGN LEAD TO THE USER Anna Forsgren Kap
                     l.OwnerId=cs.OwnerId__c;
                       //SAVE OLD RECORD TYPE AND SET RECORD TYPE ARCHIVED
                     l.OldRecordType__c=oldLead.recordtypeId;
                     l.recordtypeid=cs.RecordtypeID__c;
                    }                

             else  if ((l.Status<>'Archived' || l.Status<>'False'||l.Status<>'Lead - Never' )  && (oldLead.Status != l.Status)&&(l.Archived_Original_Owner__c!=null) ) {                        
                 l.OwnerId= l.Archived_Original_Owner__c;
                // SET OLD RECORD TYPE
                // CONDITION OLD RECORD TYPE NOT NULL
                If (oldlead.OldRecordType__c!=null){
                l.recordtypeid=oldlead.OldRecordType__c;
                                                   }                      
            }
        }
     }


MY TEST CLASS:

@isTest
private class TestLeadSetOwner {


    static testMethod void TestLeadSetOwner() {

        Test.setMock(WebserviceMock.class, new DevQlikViewWebServiceMockImpl());


        Profile p = [SELECT Id From Profile WHERE Name='System Administrator' limit 1];
        User u2 =new User( Alias = 'newUser1' ,
                        Email ='newuser123@testorg.com',
                        EmailEncodingKey = 'UTF-8',
                        LastName = 'Testing',
                        LanguageLocaleKey='en_US',
                        LocaleSidKey='en_US', 
                        UserName='newuser123@testorg.com',
                        ProfileId=p.Id,
                        TimeZoneSidKey = 'America/Los_Angeles'); 

        // TEST CUSTOM SETTING 

         SetOwner__c mycs = SetOwner__c.getValues('standardset'); 
         if(mycs == null) {             
             mycs = new SetOwner__c(Name= 'CustomValues');
             mycs.OwnerId__c ='00520000000z3PrAAI';
             mycs.RecordtypeID__c='012L00000004QsrIAE';
             insert mycs;
         }     

        //CREATE A LEAD 1
       // System.runAs(u2) {               
            Lead lead = createNewLead(mycs.OwnerId__c);                      
            lead.Archived_Original_Owner__c = mycs.OwnerId__c;           
            insert lead;

            //CREATE A LEAD 2
            Lead lead2 = createNewLead(mycs.OwnerId__c);
            insert lead2;

            test.startTest();

            lead.Status = 'Unqualified';
            lead.OldRecordType__c= mycs.RecordtypeID__c;
            update lead;


            lead2.Status = 'false';
            update lead2;

            lead.Status = 'Contacted';
            lead.OwnerId=lead.Archived_Original_Owner__c;
            update lead;

            test.stopTest();        
        }
   // }

    private static Lead createNewLead(Id ownerId) {
        Lead lead = new Lead();
        lead.OwnerId = ownerId;
        lead.LastName = 'last';
        lead.FirstName = 'First';
        lead.Company = 'Company';
        lead.Status = 'NewStatus';
        lead.IsUnreadByOwner = True;
        lead.Country='Sweden';
        lead.Email='shshdkjd@fkfk.fi';
        lead.Phone='3333';
        lead.MobilePhone='3333';
        lead.Switchboard__c='3333';
        return lead;        
    }


}

Thank you very much. BR.

1
  • yes, dummy data.This because i want avoid the use of seealldata=true in my test class
    – Enry
    Jun 18 '13 at 17:06
1

You are not inserting records into your customsetting with the name 'standardset' as your looking for a record with name 'standardset' but your test record inserts with name 'CustomValues'

if(mycs == null) {             
         mycs = new SetOwner__c(Name= 'CustomValues');
         // Above line should be changed to 
         mycs = new SetOwner__c(Name= 'standardset');
         mycs.OwnerId__c ='00520000000z3PrAAI';
         mycs.RecordtypeID__c='012L00000004QsrIAE';
         insert mycs;
     }  

Note: Its a bad practice to hardcode ids. Create your own test data by inserting records in the test class and use those ids instead or if you have seeAllData= true then you can query it from existing data.

3
  • Thank you very much it works! I have tried this solution how second option. My first working test was with a query and seealldata=true. But i didn't want make a query in my test class.I know that I have always try to avoid the use of seealldata = true because yours is a test dependent on data in the sandbox.So i have tried to use fake ids in my test class.What do you think about? thanks.
    – Enry
    Jun 18 '13 at 17:05
  • 1
    RecordTypeIds you should get by querying the SObject RecordType - the recordType.developerName should be invariant between your PROD and sandbox. Recordtypes are part of configuration data. The ownerId comes from User records. If you need a specific User with certain field values, create it in your test method. If you don't care, use UserInfo.getUserId() to get the running user
    – cropredy
    Jun 18 '13 at 23:57
  • hardcoding recordtypeIds is also bad practice
    – peevesy
    Jul 9 '14 at 13:24

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.