function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VFVF 

Action support for dropdowns

Hello i am using the action support method in order to fill a drop down on onchange event in another dropdown.The problem is that the values are being repeated in the dropdown.

elow is the design and code for the issue: 

 

code:

 

<apex:selectList id="campaignIds" value="{!status}" size="1">

<apex:actionSupport event="onchange" reRender="statusid" action="{!getStatus}" status="status"/>

  <apex:selectOption itemLabel="--None--" itemValue="--None--" ></apex:selectOption>

  <!--<apex:actionSupport event="onchange" reRender="statusid" action="{!getItemsList}" status="status"/>-->

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

  </apex:selectList>

  <apex:actionStatus id="status" startText="Loading..."></apex:actionStatus>

 

<apex:selectList id="statusid"  size="1">

  <apex:selectOption itemLabel="--None--" itemValue="--None--" ></apex:selectOption>

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

  </apex:selectList>

</td>

 

 

here is the controller code for the above design:

 

 

public class CampaignCreation

{

List<Campaign> campaign = new List<Campaign>();

List<Selectoption> options = new List<Selectoption>();

List<Selectoption> optionsList = new List<Selectoption>();

List<CampaignMemberStatus> campMemberStatus = new List<CampaignMemberStatus>();

String campaignId = null;

private String pStatus= null;

    public CampaignCreation(ApexPages.StandardController controller) {

 

    }

public CampaignCreation()

{

//Campaign campaignobj = [select id,Name from Campaign where IsActive=true];

//CampaignMemberStatus campMemberStatus = [Select Label From CampaignMemberStatus WHERE CampaignId ='campaignId'];

 

 

}

public String status

    {

        get { return pStatus; }

        set { pStatus= value; }

    } 

public List<SelectOption> getItems()

{

campaign = [select id,Name from Campaign where IsActive=true];

//String campaignId = campaign[0].Id;

for(Integer i=0;i< campaign.size();i++)

{

options.add(new SelectOption(String.valueOf(campaign[i].id),String.valueOf(campaign[i].Name)));

}

return options;

}

public List<SelectOption> getItemsList()

{

campMemberStatus = [Select Label From CampaignMemberStatus WHERE CampaignId =:pStatus];

system.debug(campMemberStatus.size());

for(Integer i=0;i< campMemberStatus.size();i++)

{

//this.pStatus = campaign[i].Id;

optionsList.add(new SelectOption( String.valueOf(campMemberStatus[i].Label),String.valueOf(campMemberStatus[i].Label)));

}

return optionsList;

}

public void getStatus()

{

getItemsList();

}

 

}

 

can any one help me out of this issue

Thanks,

shaan 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

OptionsList is a variable that's declared in your class as a private member. You have getItemsList() query a list of new items and add them to the existing list. Since the list is never cleared, all of the items end up getting added each time getitemsList is called. The reason why it is duplicated is because getStatus() is calling getItemsList, then when the page retrieves the list of items, it also calls getItemsList(), which causes the values to be duplicated.

 

To correct this behavior, change the variable from a class variable to a function-local variable, or simply clear the list each time you call getItemsList().

 

Given your code, the quickest modification you could make would be as follows:

 

 

public List<SelectOption> getItemsList() { // clear the options list before adding new options. optionsList.clear(); campMemberStatus = [Select Label From CampaignMemberStatus WHERE CampaignId =:Status]; system.debug(campMemberStatus.size()); for(Integer i=0;i< campMemberStatus.size();i++) { //this.pStatus = campaign[i].Id; optionsList.add(new SelectOption( String.valueOf(campMemberStatus[i].Label),String.valueOf(campMemberStatus[i].Label))); } return optionsList; }

 

 While I'm on the subject, your code can be optimized slightly by rewriting this function as follows:

 

 

public List<SelectOption> getItemsList() { List<SelectOption> optionsList = new List<SelectOption>(); for(CampaignMemberStatus memberstatus: [select label from campaignmemberstatus where campaignid = :status]) { optionsList.add(new SelectOption(String.valueOf(memberstatus.Label), String.valueOf(memberstatus.Label))); } return optionsList; }

 

This will reduce your viewstate size (because the list effectively evaporates after it's rendered, so it doesn't use viewspace space), and the for-each style loop reduces the complexity of the loop and the overall size of the heap (this syntax actually internally uses query() and queryMore() to get batches of records, which is far more efficient).

 

Other than those minor suggestions I mentioned, your code appears to work beautifully otherwise.

 

All Answers

Edwin VijayEdwin Vijay
Can you try placing the selectlist inside a outputpanel and rerendering the outputpanel...
VFVF

Thanks for ur reply Edwin1 , i tried placing the select list inside the ouput panel and also rendered the panl as:

 

<apex:actionSupport event="onchange" reRender="panel" action="{!getStatus}" status="status"/>

 

 

<apex:outputPanel id="panel">

<apex:selectList id="statusid"  size="1">

  <apex:selectOption itemLabel="--None--" itemValue="--None--" ></apex:selectOption>

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

  </apex:selectList>

  </apex:outputPanel>

 

The values still are being repeated inside the drop down(statusid) could you pls check my code if any thing is going wrong ....

Thanks

shaan 

 

 

 

sfdcfoxsfdcfox

OptionsList is a variable that's declared in your class as a private member. You have getItemsList() query a list of new items and add them to the existing list. Since the list is never cleared, all of the items end up getting added each time getitemsList is called. The reason why it is duplicated is because getStatus() is calling getItemsList, then when the page retrieves the list of items, it also calls getItemsList(), which causes the values to be duplicated.

 

To correct this behavior, change the variable from a class variable to a function-local variable, or simply clear the list each time you call getItemsList().

 

Given your code, the quickest modification you could make would be as follows:

 

 

public List<SelectOption> getItemsList() { // clear the options list before adding new options. optionsList.clear(); campMemberStatus = [Select Label From CampaignMemberStatus WHERE CampaignId =:Status]; system.debug(campMemberStatus.size()); for(Integer i=0;i< campMemberStatus.size();i++) { //this.pStatus = campaign[i].Id; optionsList.add(new SelectOption( String.valueOf(campMemberStatus[i].Label),String.valueOf(campMemberStatus[i].Label))); } return optionsList; }

 

 While I'm on the subject, your code can be optimized slightly by rewriting this function as follows:

 

 

public List<SelectOption> getItemsList() { List<SelectOption> optionsList = new List<SelectOption>(); for(CampaignMemberStatus memberstatus: [select label from campaignmemberstatus where campaignid = :status]) { optionsList.add(new SelectOption(String.valueOf(memberstatus.Label), String.valueOf(memberstatus.Label))); } return optionsList; }

 

This will reduce your viewstate size (because the list effectively evaporates after it's rendered, so it doesn't use viewspace space), and the for-each style loop reduces the complexity of the loop and the overall size of the heap (this syntax actually internally uses query() and queryMore() to get batches of records, which is far more efficient).

 

Other than those minor suggestions I mentioned, your code appears to work beautifully otherwise.

 

This was selected as the best answer
VFVF

Heyy that solved the problem u helped me again 

i forgot to clear the options and thus they got repeated again and again.

Any ways that was my mistake thanks for helping me out.

 

Thanks & Regards

shaan