function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
force_archforce_arch 

SOQL Query in Webservice Class failing to return any records

Hi,

 

I have developed a new webservice class to return a set of account records. Following is the code:

 

global class AccountMinesiteWebService
{

// Object to be returned by the webservice
global class Minesite{
webservice String msId;
webservice String msName;
webservice String msMineType;
webservice String msMineral;
webservice String msExternalId;
webservice String msAccountStatus;
webservice String msPhone;
webservice String msFax;
webservice String msParentName;
webservice String msIsActive;
webservice String msMineLatitude;
webservice String msMineLongitude;
}


webservice static List<Minesite> minesiteDetails(){
List<Minesite> minesites = new List<Minesite>();
Minesite ms;

//Query for the minesite Accounts
List<Account> accountList = new List<Account>([SELECT Id, Name, Mineral__c, External_ID__c, Mine_Type__c,
Account_Status__c, Phone, Mine_Latitude__c, Mine_Longitude__c,
Fax, Parent.Name, Is_Active__c, Jitterbit_Update__c
FROM Account
WHERE Type = 'Mine Site' AND Jitterbit_Update__c = 'YES']);

// Add all the minesite Accounts to the webservice class object that will be sent out to the external system
for(Account acc: accountList){
ms = new Minesite();
ms.msId = acc.Id;
ms.msName = acc.Name;
ms.msMineType = acc.Mine_Type__c;
ms.msMineral = acc.Mineral__c;
ms.msExternalId = acc.External_ID__c;
ms.msAccountStatus = acc.Account_Status__c;
ms.msPhone = acc.Phone;
ms.msFax = acc.Fax;
ms.msParentName = acc.Parent.Name;
ms.msIsActive = String.valueOf(acc.Is_Active__c);
ms.msMineLatitude = acc.Mine_Latitude__c;
ms.msMineLongitude = acc.Mine_Longitude__c;
minesites.add(ms);
}

return minesites;

}

public static testMethod void testAccountMinesiteWebService(){
List<Minesite> testMinesites = new List<Minesite>();
testMinesites = AccountMinesiteWebService.minesiteDetails();
}
}

 

When I run the test, it is just covering 18% of the class. The reason is, it is not entering the for loop at all (which means the accountList has no records in it). But I executed the same query in developer console, there it is successfully returning the records that satisfy the query conditions and it is entering the for loop as well. I can't figure out why the same query is failing to return any records in the webservice class (there are some account records in our sandbox that satisfy the query conditions). Please help me if there is any mistake in my code! Thanks much.

ForceCoderForceCoder

What is the API version of the test class?  Any 24 and after don't have access to the org's data, by default.  You could throw the SeeAllData annotation on it and see if that works.  

 

Ideally, you should insert the data yourself as part of your test setup, though, so that your tests don't depend on actual data. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

 

@isTest(SeeAllData=true)
tomcollinstomcollins

According to this Apex documentation, tests don't have access to the records in your sandbox.  You should be dynamically generating records as part of your test cases.

 

This is a good thing, as you can write your test to cover a wider range of conditions (e.g., no matching records, one record, multiple records, etc) by setting up different record sets before each test run, and confirming proper output for all cases.

 

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_testing_data_access.htm

ForceCoderForceCoder

Agree 100%...have seen problems with tests that depend on data.  

Something happens and the data isn't there anymore and the tests won't pass...

Have to create the data if you want the tests to pass in a non full copy sandbox...

Can have synchronization issues with tests running in parallel...

You need to set up data before you can deploy to production...

You deploy on top of "someon else's" old code that was written to depend on data and it fails!

 

It is also best practice to surround your code under test with

Test.startTest();

and 

Test.stopTest();