+ Start a Discussion
Sascha DeinertSascha Deinert 

Show accounts of selected user

Hi,

I want to code a visual force which shows me the accounts of the selected user. 
I have the code below, but I don't know how can I create a list of account of the selected user.

I read something about the actionsuppert onchange, but I'm not sure how it works.
 
<apex:page controller="VTP3_class">

<apex:form >

<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:OutputPanel >
            <apex:selectList value="{!SelectedUserId}" size="1" multiselect="false"  >
                <apex:selectOptions value="{!ListOfUser}" />
            </apex:selectList>
        </apex:OutputPanel>
    </apex:pageBlockSection>
</apex:pageBlock>

<apex:pageblock >
    <apex:pageblocktable value="{!AccList}" var="Acc">
        <apex:column headervalue="Unternehmen">
            <apex:outputfield value="{!Acc.Name}" />
        </apex:column>
        <apex:column headervalue="Straße">
            <apex:inputfield value="{!Acc.BillingStreet}" />
        </apex:column>
        <apex:column headervalue="Stadt">
            <apex:inputfield value="{!Acc.BillingCity}" />
        </apex:column>
    </apex:pageblocktable>
</apex:pageblock>

</apex:form>

</apex:page>
public class VTP3_class {

Public String selecteduserId {set;get;}
Public List<Account> AccList {get;set;}

Public VTP3_class()  {

AccList = [SELECT Name, BillingStreet, BillingPostalCode, BillingCity FROM Account WHERE OwnerId = :SelectedUserId];

}

Public List<SelectOption> getListOfUser() {
    List<User> Users = [SELECT Id, Username, Name FROM User] ;
    List<SelectOption> UserOptionList = new List<SelectOption>();
    UserOptionList .add(new SelectOption( ' ' ,'---Select---'));
    for(User u : Users ) {
        UserOptionList .add(new SelectOption(u.Id , u.Name));
    }
              return UserOptionList ;
    }
}

Thanks,
Sascha
Best Answer chosen by Sascha Deinert
sandeep sankhlasandeep sankhla
Hi ,

You can create a method and then onchange of user from user list you can call that method ...

Something like, you need to additionally add these..
 
Public void fetchAllAccounts()  {
AccList  = new list<Account>();

AccList = [SELECT Name, BillingStreet, BillingPostalCode, BillingCity FROM Account WHERE OwnerId = :SelectedUserId];

 

}

onchange of user you will call this method and rerender the block ..

<apex:actionfunction name="fetchAll" action="{!fetchAllAccounts}" rerender = "pageblockid"/>



<apex:selectList value="{!SelectedUserId}" size="1" multiselect="false" onchange="fetchAll();" >

             <apex:selectOptions value="{!ListOfUser}" />

          </apex:selectList>

Please implement above and let me kbow if it solves your need..

Thanks

All Answers

sandeep sankhlasandeep sankhla
Hi ,

You can create a method and then onchange of user from user list you can call that method ...

Something like, you need to additionally add these..
 
Public void fetchAllAccounts()  {
AccList  = new list<Account>();

AccList = [SELECT Name, BillingStreet, BillingPostalCode, BillingCity FROM Account WHERE OwnerId = :SelectedUserId];

 

}

onchange of user you will call this method and rerender the block ..

<apex:actionfunction name="fetchAll" action="{!fetchAllAccounts}" rerender = "pageblockid"/>



<apex:selectList value="{!SelectedUserId}" size="1" multiselect="false" onchange="fetchAll();" >

             <apex:selectOptions value="{!ListOfUser}" />

          </apex:selectList>

Please implement above and let me kbow if it solves your need..

Thanks
This was selected as the best answer
Sascha DeinertSascha Deinert
Hi,

Thanks for your response, it is working but I have a new question.
If I click a accountname the page should show me a list of opportunities of this account.
I code someting (see below), but it doesn't work.
 
<apex:page controller="VTP3_class">

<apex:form >

<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:OutputPanel >
            <apex:selectList value="{!SelectedUserId}" size="1" multiselect="false" onchange="AFunction()">
                <apex:selectOptions value="{!ListOfUser}" />
            </apex:selectList>
        </apex:OutputPanel>
    </apex:pageBlockSection>
</apex:pageBlock>

<apex:actionFunction name="AFunction" action="{!fetchAccounts}" rerender="pbAcc"/>
<apex:pageblock id="pbAcc">

<div id="AccountList" style="height:200px;">

<apex:outputpanel style="overflow:scroll;height:200px;" layout="block">

    <apex:pageblocktable value="{!AccList}" var="Acc">
        <apex:column headervalue="Unternehmen">
        <apex:outputLink value="{!SelectedAccountId}" onclick="OFunction()">{!Acc.Name}</apex:outputLink>
        </apex:column>
        <apex:column headervalue="Straße">
            <apex:outputfield value="{!Acc.BillingStreet}" />
        </apex:column>
        <apex:column headervalue="Stadt">
            <apex:outputfield value="{!Acc.BillingCity}" />
        </apex:column>
    </apex:pageblocktable>
    
 </apex:outputpanel>
   
</div>

</apex:pageblock>

<apex:actionFunction name="OFunction" action="{!fetchOpps}" rerender="pbOpp"/>
<apex:pageblock id="pbOpp">

<div id="OpportunityList" style="height:200px;">

<apex:outputpanel style="overflow:scroll;height:200px;" layout="block">

    <apex:pageblocktable value="{!OppList}" var="Opp">
        <apex:column headervalue="Beschreibung">
        <apex:outputLink value="{!Opp.id}">{!Opp.Description}</apex:outputLink>
        </apex:column>
        <apex:column headervalue="Betrag">
            <apex:outputfield value="{!Opp.Amount}" />
        </apex:column>
    </apex:pageblocktable>
    
 </apex:outputpanel>
   
</div>

</apex:pageblock>



</apex:form>

</apex:page>
public class VTP3_class {

Public String SelectedUserId {set;get;}
Public String SelectedAccountId {get;set;}
Public List<Account> AccList {get;set;}
Public List<Opportunity> OppList {get;set;}

Public VTP3_class()  {
    AccList = new List<Account>();
    OppList = new List<Opportunity>();
}

public void fetchAccounts() {
    AccList = new List<Account>();
    AccList = [SELECT Name, BillingStreet, BillingPostalCode, BillingCity FROM Account WHERE OwnerId = :SelectedUserId];
}

public void fetchOpps() {
    OppList = new List<Opportunity>();
    OppList = [SELECT Description, Amount FROM Opportunity WHERE AccountID = :SelectedAccountId];
}

Public List<SelectOption> getListOfUser() {
    List<User> Users = [SELECT Id, Username, Name FROM User] ;
    List<SelectOption> UserOptionList = new List<SelectOption>();
    UserOptionList .add(new SelectOption( ' ' ,'---Select---'));
    for(User u : Users ) {
        UserOptionList .add(new SelectOption(u.Id , u.Name));
    }
              return UserOptionList ;
    }
}

Thanks,
Sascha
RatanRatan
Hi  Sascha Deinert,

try with this.

 
<apex:page controller="VTP3_class">

<apex:form >

<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:OutputPanel >
            <apex:selectList value="{!SelectedUserId}" size="1" multiselect="false" onchange="AFunction();">
                <apex:selectOptions value="{!ListOfUser}" />
            </apex:selectList>
        </apex:OutputPanel>
    </apex:pageBlockSection>
</apex:pageBlock>

<apex:actionFunction name="AFunction" action="{!fetchAccounts}" rerender="pbAcc" />
<apex:pageblock id="pbAcc">
<div id="AccountList" style="height:200px;">
    <apex:outputpanel style="overflow:scroll;height:200px;" layout="block">
        <apex:pageblocktable value="{!AccList}" var="Acc">
            <apex:column headervalue="Unternehmen">
            <a onclick="OFunction('{!Acc.id}')" >{!Acc.Name}</a>
            </apex:column>
            <apex:column headervalue="Straße">
                <apex:outputfield value="{!Acc.BillingStreet}" />
            </apex:column>
            <apex:column headervalue="Stadt">
                <apex:outputfield value="{!Acc.BillingCity}" />
            </apex:column>
        </apex:pageblocktable>
     </apex:outputpanel>
    </div>
    </apex:pageblock>
    
    <apex:actionFunction name="OFunction" action="{!fetchOpps}" rerender="pbOpp">
        <apex:param name="myParam" value="" assignTo="{!SelectedAccountId}"/>
    </apex:actionFunction>
    <apex:pageblock id="pbOpp">
        <div id="OpportunityList" style="height:200px;">
            <apex:outputpanel style="overflow:scroll;height:200px;" layout="block">
                <apex:pageblocktable value="{!OppList}" var="Opp">
                    <apex:column headervalue="Beschreibung">
                    <apex:outputLink value="{!Opp.id}">{!Opp.Description}</apex:outputLink>
                    </apex:column>
                    <apex:column headervalue="Betrag">
                        <apex:outputfield value="{!Opp.Amount}" />
                    </apex:column>
                </apex:pageblocktable>
             </apex:outputpanel>
        </div>
    </apex:pageblock>
</apex:form>
</apex:page>
 
public class VTP3_class {

Public String SelectedUserId {set;get;}
Public String SelectedAccountId {get;set;}
Public List<Account> AccList {get;set;}
Public List<Opportunity> OppList {get;set;}

Public VTP3_class()  {
    AccList = new List<Account>();
    OppList = new List<Opportunity>();
}

public void fetchAccounts() {
    AccList = new List<Account>();
    AccList = [SELECT Name, BillingStreet, BillingPostalCode, BillingCity FROM Account WHERE OwnerId = :SelectedUserId];
}

public void fetchOpps() {
    OppList = new List<Opportunity>();
    OppList = [SELECT Description, Amount FROM Opportunity WHERE AccountID = :SelectedAccountId];
}

Public List<SelectOption> getListOfUser() {
    List<User> Users = [SELECT Id, Username, Name FROM User] ;
    List<SelectOption> UserOptionList = new List<SelectOption>();
    UserOptionList .add(new SelectOption( ' ' ,'---Select---'));
    for(User u : Users ) {
        UserOptionList .add(new SelectOption(u.Id , u.Name));
    }
              return UserOptionList ;
    }
}