+ Start a Discussion
rtuck2009rtuck2009 

Code Assistance

I am trying to accomplish the following, using a custom controller along with a VisualForce Page.

 

I have a series of accounts that act as "Retail Stores", each store can schedule appointments on any given day.   A VisualForce page that does two things currently, it has a drop down list where each store is listed.  You pick the store you want, click Search, and the appointments for that location appear.  The code that grabs those "Locations" is as follows:  (I believe)

 

Customer Controller Code:

 

public List<SelectOption> getLocations() {
        List<SelectOption> locations = new List<SelectOption>();
        for (Account acct : [SELECT Id, Name FROM Account WHERE (Type = 'Retail')]) {
            locations.add(new SelectOption(acct.Id, acct.Name));
        }
        return locations;
    }

 

VisualForce Page Code:

 

<apex:pageBlock title="Criteria">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!refresh}" value=" Search "/>
            </apex:pageBlockButtons>
            <apex:PageBlockSection columns="1" title="1. Location">
                <apex:PageBlockSectionItem >
                    <apex:outputLabel value="Location" for="location"/>
                    <apex:selectList value="{!locationId}" id="location" size="1" multiselect="false">
                        <apex:selectOptions value="{!Locations}" />
                    </apex:selectList>
                </apex:PageBlockSectionItem>
            </apex:PageBlockSection>

 

What I am trying to accomplish is updating the above code where it still pulls the accounts as listed above, but adds an additional element by looking at the current users ID, seeing what "Location" they are associated with and defaulting that location's name in the drop down list.  Currently, it is done alphabetically. 

 

I am not sure if this has been explained well enough, and if not, my apologies.  is there anyone that can offer any insight or assistance with this issue?  Thanks in advance.

 

ndganindgani

try this:

 

In your controller's constructor you can get the current user's location:

string userLocation = [select Id from User where id = :UserInfo.getUserId()].Location__c; 

 

then your query will look like this:

 

for (Account acct : [SELECT Id, Name FROM Account WHERE (Type = 'Retail') AND Location__c =: userLocation])

 

and you got the account affiliated with the location of your user.

 

i would take the query out of the for statement and put an 'if' before asking if 'userLocation != '' && userLocation != null'. 

 

rtuck2009rtuck2009

Thank you.  Not sure what I am doing wrong, but I get errors when using location__c.

 

Is there additional information I can provide that might help you help me?  :)

ndganindgani

Hi rtuck2009

 

Location__c was just an example of a custom field in user and account that i assumed you have.

It all depends what field in User and Account you want to filter by.

for example, if you want to display only the retail accounts that are in the same City like your user

you would do something like this:

 

string userCity = [select Id from User where id = :UserInfo.getUserId()].City; 

 

and the second query:

 

for (Account acct : [SELECT Id, Name FROM Account WHERE (Type = 'Retail') AND City =: userCity]) 

 

this will retreive the accounts in the same city as the user.

 

same can be done for state,country etc.

 

the point is you can query data from the current user using the UserInfo.getUserId()

and then filter your accounts according to it.