How can I debug the query string below to see what values are actually passed to the server? If I do system.debug ('Query is: '+contactsFromQ_List2 ); I do not see it in a clean string that I can easily read.

contactsFromQ_List2 = [Select contact__r.Training_Period__c,Name, Shal_Type_Community__c, Contact__c, Contact_Name__c, a.Supervisor_Name__c, 
                                                                            a.RecordTypeId, a.Months_in_Shut__c,  a.Id, 
                                                                            a.Coordinator_Name__c, a.Active__c 
                                                                            From Additional_Contact_Role__c a 
                                                                            Where a.RecordTypeId = :shalRTId
                                                                            AND Contact__c != Null
                                                                            AND CALENDAR_YEAR(a.From_Date__c) = :yearChosen
                                                                            AND Contact__c IN :currentContactIds_Set
                                                                            AND contact__r.Training_Period__c = :chosenTrainingPeriod
                                                                            Order by Shal_Type_Community__c];
  • You could just write the variables to system.debug themselves, alternatively you could use database.query and dynamic SOQL (and debug the query string) - but this seems unnecessary from your example.
    – Girbot
    Jun 23, 2016 at 12:54

3 Answers 3


If it's the results that you want then I like to use JSON.serialize(). It will output as a JSON object, you can then use various JSON tools to view what was returned.

System.debug ('Query is: '+ JSON.serialize(contactsFromQ_List2) );

  • I pasted it in the code right after the query but it just gave me a blank string.
    – Apex N-u-b
    Jun 23, 2016 at 14:47
  • @ApexN-u-b You should have at least found Query is: []? Which would indicate an empty list, nothing was returned from the query.
    – dBeltowski
    Jun 23, 2016 at 14:59
  • You are correct.
    – Apex N-u-b
    Jun 26, 2016 at 10:07

You'd have to debug the individual variables that are used. Debugging contactsFromQ_List2 will give you the results of the query, not the query itself. There is no way to debug the actual query that was processed by the server, because even the profiling section will only show the literal query used.

For example:

String name = 'test';
Account[] accounts = [SELECT Id FROM Account WHERE Name = :name];

Logged output:

08:45:01.30 (30559267)|CUMULATIVE_PROFILING|SOQL operations|
AnonymousBlock: line 2, column 1: [SELECT Id FROM Account WHERE Name = :name]: executed 1 time in 24 ms

If the query string is too large, the debug logs may only display a portion of it followed by "..." to indicate that the output has been truncated.

To view the full query string in the debug logs, you can use the System.debug(LoggingLevel level, Object obj) method with a logging level of LoggingLevel.DEBUG and the String.valueOf(Object obj) method to convert the query string to a string value.

Here's an example code snippet that shows how to do this:

String debugString = 'dataQuery: ' + String.valueOf(dataQuery); System.debug(LoggingLevel.DEBUG, debugString);

This will output the full query string in the debug logs with a logging level of DEBUG. You can adjust the logging level to your preference, depending on the severity of the message.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .