I am experiencing performance issues on my Production instance with SOQL queries being executed by a Communities user. I'd like to finetune the SOQL Query performance and have done some tests in the Execute Anonymous window.

However, the problem seems to be in user-specific authorizations and/or sharing settings.

So my question is: How can I run SOQL queries as a Communities user without having to deploy dummy code into my Production environment?

  • Log in as a Communities User and do what would cause the code in question to fire. – dphil Apr 11 '14 at 14:28
  • I'd like to be able to test/tune SOQL queries without putting code on production. So like 'Execute Anonymous' but with a non-system user. – Guy Clairbois Apr 11 '14 at 14:55
  • O. No that is not possible I believe. You should be able to do these tests fine in your sandbox or DE though as long as you have the permissions set up the same. – dphil Apr 11 '14 at 15:13
  • OK. Still requires saving code to those instances, though (no dynamic way like execute anonymous). Thanks for your help :-) – Guy Clairbois Apr 11 '14 at 15:20

I would say you create/deploy test class with the soql you want to test optimise and make your soql configurable in custom settings so you can try tweak it to try and test different versions without need to deploy again. Most importantly, make sure you run your test as the Community User, by using system.runAs().

Finally, open Developer Console with debug logs in the test class and checkpoints you can analyse performance of overall execution.

| improve this answer | |

In the end I resolved this by creating a 'soqltester' vf page/controller. I can give a partner user (even on production) temporary access and run any query I want.

vf page

<apex:page showHeader="true" sidebar="true" controller="SOQLTesterController" title="SOQL tester">
    <apex:form >

    <apex:inputTextarea value="{!theQuery}" cols="200"/>
    <apex:commandButton action="{!doQuery}" value="Do Query" rerender="resultsPanel" status="queryStatus"/>
            <apex:actionStatus id="queryStatus"  stopText="">
                <apex:facet name="start">
                   <img src="/img/loading.gif"/> Querying....
    <br />
    <br />
    <br />
    <apex:outputPanel id="resultsPanel">
        <apex:pageMessages />
        <apex:pageBlock rendered="{!soqlResult != null}">
            <apex:outputLabel value="Query results:" />
            <apex:pageBlockTable var="item" value="{!soqlResult}">
                <apex:repeat var="fieldname" value="{!fields}">
                    <apex:column >
                        <apex:facet name="header">


public with sharing class SOQLTesterController {

public SOQLTesterController() {


public String theQuery {get;set;}

public pageReference doQuery(){

        soqlResult = Database.query(theQuery);
    } catch (Exception e){
        Apexpages.addMessage(New Apexpages.Message(ApexPages.severity.ERROR,e.getMessage()));
        return null;

        Map<String, Object> queriedFieldValues = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(soqlResult[0]));
        fields = new List<String>();
        dumpFields('', queriedFieldValues);

    return null;

public List<String> fields {get;set;}

public List<sObject> soqlResult {get;set;}

public void dumpFields(String relatedField, Map<String, Object> queriedFieldValues)
    for(String queriedFieldName : queriedFieldValues.keySet())
        // Skip this information, its not a field
        // Check if the queried value represents a related field reference?
        Object queriedFieldValue = queriedFieldValues.get(queriedFieldName);
        if(queriedFieldValue instanceof Map<String,Object>){
            system.debug((Map<String, Object>) queriedFieldValue);
            dumpFields(relatedField+queriedFieldName + '.', (Map<String, Object>) queriedFieldValue);
        } else{
            System.debug(relatedField + queriedFieldName + ' = ' + queriedFieldValue);
            fields.add(relatedField + '' + queriedFieldName);


| improve this answer | |

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.