+ Start a Discussion
Prabhat Singh 56Prabhat Singh 56 

Problems with sandbox data

Hello Team,

 Steps of reproduce:

I develop custom apex class, and another test class with 2 test methods. In one test method i'm quering for my company brand and geting an error: System.NullPointerException: Attempt to de-reference a null object
Class.MyTest.verifyChat2Lead: line 47, column 1.

Query I'm running is: select Id, Name from Company_Brand__c where Name='SEA'

but when I run this query from developer console i get 1 result just as i expect

Note: I created sandbox for development of custom APEX class. In testMethod I query for Custom object but get no result, but same query in developer console get 1 result.

Please help me out to solve this.

I will select the best answer.

Thanks
Prabhat

Thanks
Best Answer chosen by Prabhat Singh 56
Amit Singh 1Amit Singh 1
Hi Prabhat,

You need to insert Company_Brand__c Object record in Order to make SOQL query.
Company_Brand__c c = new Company_Brand__c(Name='SEA');
insert c;
Thanks,
Amit Singh
 

All Answers

Amit Singh 1Amit Singh 1
Hi Prabhat,

You need to insert Company_Brand__c Object record in Order to make SOQL query.
Company_Brand__c c = new Company_Brand__c(Name='SEA');
insert c;
Thanks,
Amit Singh
 
This was selected as the best answer
Arshadulla.ShariffArshadulla.Shariff
Hello Prabhat,

The best practice followed in Test cases is to Create Record before querying.
Company_Brand__c  cb = new Company_Brand__c (Name='SEA');
insert cb;

please refer the following link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm.
Prabhat Singh 56Prabhat Singh 56
Hello @AMit Singh

Thanks