Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I've been trying to understand the example at

http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_sample_standard.htm#pages...

This is from VF Developer's Guide.

I have no luck. I tried to learn the code by understanding every line before typing it in (in other words, instead of copying and pasting). When I got all the code in and ran it, I got this error:

SObject row was retrieved via SOQL without querying the requested field: Account.BillingCity

So instead I created new files and simply copied and pasted the code in again, and got the same error. That means it's not my typo. Something is not working with the code properly. However, I couldn't figure out what the errors are.

I have already executed the reset() before calling the addFields() function on the controller. Is that not good enough?

share|improve this question
2  
Which line of apex you are gettting the error ?Adding the line we may help you to reach to fix . –  Mohith Shrivastava Jan 11 '13 at 19:34
2  
There's a lot of examples on that page. Can you post the code or point to which code you are using on the page? –  Daniel Hoechst Jan 11 '13 at 20:29
    
He is talking about the first two code blocks, as they are the only ones that reference the BillingCity field. –  Phil Rymek Jan 11 '13 at 20:59
    
Hi there I'm referring to the section called "Using Dynamic References for a User-Customizable Page", with DynamicCustomizableListHandler class and 2 pages: DynamicCustomizableList and CustomizeDynamicList. –  kingkoo Jan 11 '13 at 21:11
    
If you copy and paste the code into an Apex class and 2 pages maybe you can see if you can re-create the same problem I have. I have the system admin access so I don't think it has to do with security or permission. –  kingkoo Jan 11 '13 at 21:12
show 2 more comments

3 Answers 3

I believe that typically this means that the record returned doesn't contain the field you're trying to access. Blocked by security?

share|improve this answer
    
Hi William, it's my understanding that it is supposed to be dynamic visualforce, so the reset() and addFields() method are supposed to tell the controller, I'm resetting the list of fields I want to retrieve. (that means the list of fields don't have to be available during page load). This is a new function introduced back in 2011. –  kingkoo Jan 11 '13 at 21:13
add comment

"Using Dynamic References for a User-Customizable Page", with DynamicCustomizableListHandler class and 2 pages: DynamicCustomizableList and CustomizeDynamicList.

It works for me in my DE org.

enter image description here

Here's what I did:

Navigated to /apex/CustomizeDynamicList that has a table of the Account records with the selected fields. I clicked the Customize List button and it took me to the DynamicCustomizableList where I selected the BillingCounty field, moved it to the right, and clicked the Show These Fields button. That took me back to the CustomizeDynamicList button and displayed the Account Name and Billing City.

Here's my code:

DynamicCustomizableList page

<apex:page standardController="Account" recordSetVar="accountList"  extensions="DynamicCustomizableListHandler">
    <br/>
    <apex:form >

    <!-- View selection widget, uses StandardController methods --> 

    <apex:pageBlock >
        <apex:outputLabel value="Select Accounts View: " for="viewsList"/>
        <apex:selectList id="viewsList" size="1" value="{!filterId}">
            <apex:actionSupport event="onchange" rerender="theTable"/>
            <apex:selectOptions value="{!listViewOptions}"/>
        </apex:selectList>
    </apex:pageblock>

    <!-- This list of accounts has customizable columns --> 

    <apex:pageBlock title="Accounts" mode="edit">
        <apex:pageMessages />
        <apex:panelGroup id="theTable">
            <apex:pageBlockTable value="{!accountList}" var="acct">
                <apex:column value="{!acct.Name}"/>
                <!-- This is the dynamic reference part --> 

                <apex:repeat value="{!displayFields}" var="f">
                    <apex:column value="{!acct[f]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:panelGroup>
    </apex:pageBlock>

    <br/>
    <apex:commandButton value="Customize List" action="{!customize}"/>

    </apex:form>
</apex:page>

CustomizeDynamicList page

<apex:page standardController="Account" recordSetVar="ignored"  extensions="DynamicCustomizableListHandler">
    <br/>
    <apex:form >

    <apex:pageBlock title="Select Fields to Display" id="selectionBlock">
        <apex:pageMessages />
        <apex:panelGrid columns="3">
            <apex:selectList id="unselected_list" required="false"  value="{!selected}" multiselect="true" size="20" style="width:250px">
                <apex:selectOptions value="{!unSelectedOptions}"/>
            </apex:selectList>
            <apex:panelGroup >
                <apex:commandButton value=">>"  action="{!doAdd}" rerender="selectionBlock"/>
                <br/>
                <apex:commandButton value="<<"  action="{!doRemove}" rerender="selectionBlock"/>
            </apex:panelGroup>
            <apex:selectList id="selected_list" required="false"  value="{!unselected}" multiselect="true" size="20" style="width:250px">
                <apex:selectOptions value="{!selectedOptions}"/>
            </apex:selectList>
        </apex:panelGrid>
        <em>Note: Fields marked <strong>*</strong> are inaccessible to your account</em>
    </apex:pageBlock>

    <br/>
    <apex:commandButton value="Show These Fields" action="{!show}"/>

    </apex:form>

</apex:page>

DynamicCustomizableListHandler class

public class DynamicCustomizableListHandler {

    // Resources we need to hold on to across requests 

    private ApexPages.StandardSetController controller;
    private PageReference savePage;

    // This is the state for the list "app" 

    private Set<String> unSelectedNames = new Set<String>();
    private Set<String> selectedNames = new Set<String>();
    private Set<String> inaccessibleNames = new Set<String>();

    public DynamicCustomizableListHandler(ApexPages.StandardSetController controller) {
        this.controller = controller;
        loadFieldsWithVisibility();
    }

    // Initial load of the fields lists 

    private void loadFieldsWithVisibility() {
        Map<String, Schema.SobjectField> fields = 
            Schema.SobjectType.Account.fields.getMap();
        for (String s : fields.keySet()) {
            if (s != 'Name') {  // name is always displayed  

                unSelectedNames.add(s);
            }
            if (!fields.get(s).getDescribe().isAccessible()) {
                inaccessibleNames.add(s);
            }
        }
    }

    // The fields to show in the list 

    // This is what we generate the dynamic references from 

    public List<String> getDisplayFields() { 
        List<String> displayFields = new List<String>(selectedNames);
        displayFields.sort();
        return displayFields;
    }

    // Nav: go to  screen 

    public PageReference customize() {
        savePage = ApexPages.currentPage(); 
        return Page.customizedynamiclist;
    }

    // Nav: return to list view 

    public PageReference show() {
        // This forces a re-query with the new fields list 

        controller.reset();
        controller.addFields(getDisplayFields());
        return savePage; 
    }

    // Create the select options for the two select lists on the page 

    public List<SelectOption> getSelectedOptions() { 
        return selectOptionsFromSet(selectedNames);
    }
    public List<SelectOption> getUnSelectedOptions() { 
        return selectOptionsFromSet(unSelectedNames);
    }

    private List<SelectOption> selectOptionsFromSet(Set<String> opts) {
        List<String> optionsList = new List<String>(opts);
        optionsList.sort();
        List<SelectOption> options = new List<SelectOption>();
        for (String s : optionsList) {
            options.add(new 
                SelectOption(s, decorateName(s), inaccessibleNames.contains(s)));
        }
        return options;
    }

    private String decorateName(String s) {
        return inaccessibleNames.contains(s) ? '*' + s : s;
    }

    // These properties receive the customization form postback data 

    // Each time the [<<] or [>>] button is clicked, these get the contents 

    // of the respective selection lists from the form 

    public transient List<String> selected   { get; set; }
    public transient List<String> unselected { get; set; }

    // Handle the actual button clicks. Page gets updated via a 

    // rerender on the form 

    public void doAdd() {
        moveFields(selected, selectedNames, unSelectedNames);
    }
    public void doRemove() {
        moveFields(unselected, unSelectedNames, selectedNames);
    }

    private void moveFields(List<String> items, 
            Set<String> moveTo, Set<String> removeFrom) {
        for (String s: items) {
            if( ! inaccessibleNames.contains(s)) {
                moveTo.add(s);
                removeFrom.remove(s);
            }
        }
    }

}

Nothing is actually saved in the DB, so it's a stretch, but assuming your code matches mine, maybe something got hosed in your view state while you were editing and the page was using a list of fields that didn't match up to what you selected. Like I said, I have no problem using the above code to add and remove fields onto/off of the DynamicCustomizableList page in my DE.

share|improve this answer
1  
I copy this exact code to my DE org and I get the SObject row was retrieved via SOQL without querying the requested field error. There must be some difference between the orgs. –  Daniel Hoechst Jan 12 '13 at 4:56
    
@Peter Knolle, I copied and pasted your code and still got the same error. –  kingkoo Jan 14 '13 at 15:58
1  
@DanielHoechst, exactly the same error. And I'm starting to think it's something to do with the orgs too! –  kingkoo Jan 14 '13 at 15:59
    
Something more strange I found out, but at least I could find a way to display the data. @DanielHoechst since you were able to replicate my error, please try this method and see if it helps. I go to the page and first change the filter so that by default I have no accounts showing (mine is New Last Week). Then I went to "Customize List" and then selected some field and then clicked "Show These Fields". This time again no records are displayed but you'll see the columns. Then change the filter to show "All Accounts". For me this is how I can show records. Does it work for you? –  kingkoo Jan 14 '13 at 22:09
1  
@kingkoo, yes I see the same behavior. To make it even weirder, I can then further add more fields without it erroring. This is a head scratcher. –  Daniel Hoechst Jan 15 '13 at 0:13
show 1 more comment

I tried copying the code into two of my DE orgs. The code worked on a newer org on NA9, but my oldest org on NA12 didn't work. I suspect it has to do with the age of the org. I've seen other things not work in older DE orgs.

share|improve this answer
1  
I was thinking this same thing, too, and experimented. My DE is on na14 and is winter '13 and the api version of the pages and class is 26. I created that DE just a couple of weeks ago on January 3rd 2013. I tested it out from another DE on na14 that I've had for more than a year and it worked there as well, but it's probably only about 1-1.5 years old. @kingcoo I wonder how old your instance is and if you create a newer one if it works on that? –  Peter Knolle Jan 12 '13 at 11:53
    
Hi guys, I just checked. My org (with the error) was created 15 Dec 2012, so not even a month ago. So I went to another org that I created just last week, and yay it worked!!! What's going on?? It cost me the entire day last week on this and I never would've thought it had to do with the org. More importantly, is there anything that I can do to the older org to make the code work? Thanks a lot guys!! –  kingkoo Jan 14 '13 at 16:07
    
I also tried this: I went back to the older old I created on 15 Dec 2012 (one with error), changed the API version for the classes and apex pages to v21 instead of the current v26. And the error persisted. So I think the error is not about the API version but something else, but I can't quite figure out what that is. Again, the most important thing for me is to know if there's anything I can change to that org to make the code work. Thanks..... –  kingkoo Jan 14 '13 at 16:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.