1

I am running a test case , I have upgraded my Salesforce Sandbox to latest version Api (26.0). Running SOQL individually give me results but while using Console I ran in error where SOQL comes out with 'Zero' results.

I would like to know, if we upgrade to new version, there could be possibility that I cannot pull the data from the Console, I believe there should be some annotation or some workaround that permit console (using new Api) to access the data.

Please advice, you can correct me if I am wrong

Login_Credentials__c enterprise = [
    SELECT Id,Name, RecordTypeId,  Account__c, Contact__c, Product__c, User_name__c, 
        Password__c,Item__c,Contact__r.Email 
    FROM Login_Credentials__c 
    WHERE Item__c =: 'ENTERPRISE' LIMIT 1
];

SQOL query comes with Zero results in Console

  • The annotation to see data in the sandbox is @isTest(SeeAllData=true) – techtrekker Nov 16 '12 at 22:28
4

It sounds like maybe you haven't refreshed your sandbox for a while. A change was made in API version 24.0 to isolate data by default when running unit tests. It is possible to still access the data records, if you must, by altering the annotation to @IsTest(SeeAllData=true). The isolation makes test code a bit easier to write because you do not have to worry about keeping the existing records from interfering with your test case.

A best practice for apex testing is to create all data used within the test method (or load it with the new loadData method). Some reasons for this are to eliminate org dependency when moving the code from sandbox to sandbox to ... to production (you would have to also move the data for the test to pass) and when you know what the input is you can know what the expected output is (difficult to determine with unknown input).

Try creating a new Login_Credentials__c record for the assignment to enterprise instead making a query for it -

Login_Credentials__c enterprise = new Login_Credentials__c(
    Name='xyz', RecordTypeId='123...', etc);

This is not so much part of the solution now, but because of the way you wrote the query assignment (I used to do the same thing), I think you should look over the question Handling a Query Zero Results for some tips when querying for only 1 record. It changed me around.

  • I am aware about @IsTest(SeeAllData==true), I fixed the issue though thanks for comment – Harshit Pandey Nov 17 '12 at 1:25
  • Thanks @Zjeh for the update, i upgraded sandbox and using the test data here which resolved my issue for now – Harshit Pandey Dec 18 '12 at 16:47
3

Using the dynamic reference =: with a string literal 'ENTERPRISE' won't work, I believe. Use Item__c = 'ENTERPRISE'. Also, are you using double-underscores?

Also, don't forget that in newer API versions, test code doesn't see existing data, and must create its own data. So a query in the console and in test code will effectively be in totally different orgs (data-wise).

SOQL is backwards-compatible, so you won't lose anything in the query when your org goes to a new version or you run Apex in a new API version. I can't see any other reasons why your query will return fewer results in the console than in your test data.

  • I did a quick exec with a : and literal. I thought it would have been a problem too, but it works fine. Execute Anonymous: division div = [select id from division where name = :'global']; 18:30:45.041 (41634000)|EXECUTION_STARTED 18:30:45.041 (41646000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 18:30:45.042 (42885000)|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|select id from division where name = :tmpVar1 18:30:45.044 (44569000)|SOQL_EXECUTE_END|[1]|Rows:1 – zjeh Nov 16 '12 at 23:34
  • I got it running - it was that I need to create my own data. – Harshit Pandey Nov 17 '12 at 1:24
1

Please create Test Data in your Test Class then Query the same for the Update .In test class its a best practice to not to use data from database.

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.