+ Start a Discussion
dsh210@lehigh.edudsh210@lehigh.edu 

Refresh SOQL Query

So earlier I was trying to hide/show elements to implement a filter system. However, it was brought to my attention that it would be far easier to rerun the query I used to get the elements in the first place, just use a WHERE clause with the filter. However, I am wondering how to get the page to update with these new results. I am storing the filter in a global Apex variable and running this constructor:

 

 

    public DefinitionSetCtrl(ApexPages.StandardSetController controller) {
        Database.QueryLocator ql;
        ID modelDefId = ApexPages.currentPage().getParameters().get('ModelDefId');
        if(Filter == null){
        ql = Database.getQueryLocator(
            [select Name, Attribute_Type__c, Is_Required__c, Default_Checkbox_Value__c,
                                 FROM Attribute_Definition__c
             WHERE My_Definition__c = :modelDefId             ]
        );}
        else {
        ql = Database.getQueryLocator(
            [select Name, Attribute_Type__c, Is_Required__c, Default_Checkbox_Value__c,
                                 FROM Attribute_Definition__c
             WHERE My_Definition__c = :modelDefId AND Attribute_Type__c = :Filter
             ]
        );}
        StdSetCtrl = new ApexPages.StandardSetController(ql);
        DefList = (Attribute_Definition__c[])StdSetCtrl.getRecords();
}

 The filter gets updated when the user selects a type to filter on, but I am wondering how to get the page to rerun that query and display the new selection. When it runs again it should see the new Filter value and jump into that else to display only the records of type Filter.

 

Any suggestions are much appreciated, thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
dsh210@lehigh.edudsh210@lehigh.edu

The issue I was running into was server evaluation vs. page evaluation. Since all Apex methods are evaluated on page load, I could not store the search text in an apex variable, since the user was going to change it while viewing the page.

 

Also, for some reason when evaluating links, it kept trying to go to na7.salesforce.com rather than visual.force.com, so I would just get broken links.

 

My solution was as follows:

 

SearchString = document.location.protocol + "//" + document.location.host + "/apex/MyPage?ModelDefId={!ModelDefId}&Filter={!Filter}&Search=" + SearchVariable;

 

I placed an onclick on my <apex:outputLink> that launched the following function:

 

 

function getURL() {
                SearchString = document.location.protocol + "//" + document.location.host + "/apex/MyPage?ModelDefId={!ModelDefId}&Filter={!Filter}&Search=" + SearchVariable;
                window.open(SearchString, "_parent");
}

 

 

This cleared things right up and the page works great now!

 

P.S. The Filter and Search parameters are evaluated in my Apex constructors and the page query is run appropriately to search for like items.

 

Hope this helps anyone with a similar issue and a shoutout to imuino2 for the help.

All Answers

imuino2imuino2

You can for example make a function and call it when the user clicks on the filter or change the selection (in case you are using a picklist)

and reload the whole page passing the new filter as a parameter. So when the page reloads it reads the parameter and makes the query with the filter.

 

You can do it without reloading the whole page too, with a component for example.

 

If you need more information or want more details please let me know.

 

Ignacio.

dsh210@lehigh.edudsh210@lehigh.edu

Thanks that is exactly what I ended up doing, using URL parameter passing. The only outstanding issue I have is that I need to get a javascript variable into the "value" string of my outputLink, since the variable is set after the user enters a search string.

 

value="http://myURL?Search= + MyVariable"

 

Something along those lines. Does anyone know if this is even possible, I have been trying many things but none have been successful yet.

imuino2imuino2

I suggest you use an apex:commandButton that on click, calls a function that returns a PageReference where you can add the parameter to the url.

For more information about the apex:commandButton go to http://yourorgurl/apexpages/apexcomponents.apexp

and to know more about PageReference go here.

Ignacio.

dsh210@lehigh.edudsh210@lehigh.edu

The issue I was running into was server evaluation vs. page evaluation. Since all Apex methods are evaluated on page load, I could not store the search text in an apex variable, since the user was going to change it while viewing the page.

 

Also, for some reason when evaluating links, it kept trying to go to na7.salesforce.com rather than visual.force.com, so I would just get broken links.

 

My solution was as follows:

 

SearchString = document.location.protocol + "//" + document.location.host + "/apex/MyPage?ModelDefId={!ModelDefId}&Filter={!Filter}&Search=" + SearchVariable;

 

I placed an onclick on my <apex:outputLink> that launched the following function:

 

 

function getURL() {
                SearchString = document.location.protocol + "//" + document.location.host + "/apex/MyPage?ModelDefId={!ModelDefId}&Filter={!Filter}&Search=" + SearchVariable;
                window.open(SearchString, "_parent");
}

 

 

This cleared things right up and the page works great now!

 

P.S. The Filter and Search parameters are evaluated in my Apex constructors and the page query is run appropriately to search for like items.

 

Hope this helps anyone with a similar issue and a shoutout to imuino2 for the help.

This was selected as the best answer