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
Roelof WalRoelof Wal 

Apex code works, but not during test

I have the following code:
public with sharing class getCustomvalues {

        public final Account account;
        public List<String> myitems {get; set;}
        public List<Custom_items__c> myResult {get;set;}
    
        public getCustomvalues() {
            Id id = ApexPages.currentPage().getParameters().get('id');
            account =  [SELECT Account.Name, items__c FROM Account WHERE Id = :id];
            myitems = new List<String>();
            if (account.items__c== null) {
                 myResult =null;  
                //System.debug(LoggingLevel.ERROR, 'result will be empty');
            } else {
                myitems.addAll(account.items__c.split(';'));
System.debug('myResult Query=select Name,Sn__c,Rn__c from Custom_items__c where Name in '+myitems+' order by Ordering_number__c');
                   myResult =[select Name,Sn__c,Rn__c from Custom_items__c where Name in :myitems order by Ordering_number__c];
            }
        }

        public Account getAccount() {
        
            return account;
        }
}
I wrote a test class that works good, but for some reason the myResult object stays empty.
Looking at the test logs I see that  (account.items__c== null) { is not true during the test, so it goes to the } else { section and there myitems is filled and contains data. 

One thing I noticed was that the debug log showed the following for the query
select Name,Sn__c,Rn__c from Custom_items__c where Name in (thevalue) order by Ordering_number__c
This looks wrong because it should be
select Name,Sn__c,Rn__c from Custom_items__c where Name in ('thevalue') order by Ordering_number__c
So instead of (thevalue) it should be ('thevalue') to be correct. 
On the other hand, the code works correct in the normal situations. 


 
Best Answer chosen by Roelof Wal
Roelof WalRoelof Wal
aha....
Within test class you need to fill the Opportunity object first since it is not retrieving real data from the system 

All Answers

Jon WilsonJon Wilson
The debug log doesn't put quotes around the values because your debug statement just called "toString" on a List<String> (this is unrelated to your SOQL query). The actual query uses a data binding which should handle the values just fine.
Roelof WalRoelof Wal
Thanks john,
But I was only trying to see with the debug statement what could be wrong and what possible reason there could be for the code to not "fill" myResult. 

This is what the Raw log shows:
09:29:16.28 (1117577411)|HEAP_ALLOCATE|[20]|Bytes:130
09:29:16.28 (1117595804)|SYSTEM_METHOD_ENTRY|[20]|System.debug(ANY)
09:29:16.28 (1117605157)|USER_DEBUG|[20]|DEBUG|myResult Query=select Name,Sn__c,Rn__c from Custom_items__c where Name in (theaccount_text) order by Ordering_number__c
09:29:16.28 (1117612379)|SYSTEM_METHOD_EXIT|[20]|System.debug(ANY)
09:29:16.28 (1117616157)|STATEMENT_EXECUTE|[21]
09:29:16.28 (1117618938)|HEAP_ALLOCATE|[21]|Bytes:129
09:29:16.28 (1117636486)|HEAP_ALLOCATE|[21]|Bytes:4
09:29:16.28 (1117647451)|SYSTEM_METHOD_ENTRY|[21]|getCustomvalues.__sfdc_myitems()
09:29:16.28 (1117745045)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"myResult":"0x28ff1c9b","myitems":"0x30942bbb","quote":"0x28e23800"}|0x7e5a8b84
09:29:16.28 (1117761254)|SYSTEM_METHOD_EXIT|[21]|getCustomvalues.__sfdc_myitems()
09:29:16.28 (1118300170)|SOQL_EXECUTE_BEGIN|[21]|Aggregations:0|SELECT Name,Sn__c,Rn__c FROM Custom_items__c WHERE Name IN :tmpVar1 ORDER BY Ordering_number__c ASC NULLS FIRST
09:29:16.28 (1123010619)|SOQL_EXECUTE_END|[21]|Rows:0

09:29:16.28 (1124002388)|USER_DEBUG|[20]|DEBUG|getCustomvalues:[myResult=(), myitems=(theaccount_text), account=Account:{Name=boerharms, items__c =theaccount_text, Id=0Q05r000000CNiCCAW}]

 
Roelof WalRoelof Wal
I am lost. 
Whatever I try the result of myResult is always NULL. 
Even when I remove the variable from the query and change the whole query to a standard SalesForce object like Account, it always results NULL. 
public with sharing class getCustomvalues {

		public final Account account;
    	public List<String> myitems {get; set;}
    	public List<Opportunity> myResult {get;set;}
    
    	public getCustomvalues() {
            Id id = ApexPages.currentPage().getParameters().get('id');
            account =  [SELECT Account.Name, id FROM Account WHERE Id = :id];
            myitems = new List<String>();
            if (account.id== null) {
             	myResult =null;  
            } else {
           		myResult =[select Name,id from Opportunity];
            }
        }

        public Account getAccount() {
        
    	    return account;
	    }
}
10:32:42.520 (1910938833)|STATEMENT_EXECUTE|[14]
10:32:42.520 (1910941980)|STATEMENT_EXECUTE|[16]
10:32:42.520 (1910948210)|HEAP_ALLOCATE|[16]|Bytes:32
10:32:42.520 (1910972532)|HEAP_ALLOCATE|[16]|Bytes:4
10:32:42.520 (1911301073)|SOQL_EXECUTE_BEGIN|[16]|Aggregations:0|SELECT Name, id FROM Opportunity
10:32:42.520 (1923081558)|SOQL_EXECUTE_END|[16]|Rows:0  <---- nothing returns from above query. But I have 100+ results when I run the query from Query editor? 
10:32:42.520 (1923123344)|HEAP_ALLOCATE|[16]|Bytes:4
10:32:42.520 (1923152572)|HEAP_ALLOCATE|[16]|Bytes:0
10:32:42.520 (1923192372)|HEAP_ALLOCATE|[16]|Bytes:4


 
Roelof WalRoelof Wal
aha....
Within test class you need to fill the Opportunity object first since it is not retrieving real data from the system 
This was selected as the best answer