+ Start a Discussion

How to set the selected value of the dropdown in visual force page?

I have a lookup field which i am displaying on the visual force page as an '<apex:selectlist/>'

Code is as below:

<apex:selectList id="CampName" value="{!CampaignName}" size="1" >
                  <apex:selectOptions value="{!Campaigns}"></apex:selectOptions>

In the apex class i am setting the values of the selectlist as below:

 public List<selectOption> getCampaigns()
        List<selectOption> optionList = new List<selectOption>();
        optionList.add(new selectOption('', '- None -'));  
        for (Campaign__c a : [SELECT Id, name FROM Campaign__c where Status__c='Active' order by Name])
            optionList.add(new selectOption(a.Id, a.Name));  
        return optionList;  

Now suppose i have the campaign name as query paramenter to this visual force page and if i retrieve it as follows:

String strQueryString = ApexPages.CurrentPage().getParameters().get('Campaign');


how will i set this value in the strQueryString to the 'CampName' list selected value?

For e.g: If the Parameter has value 'Test' and i have a value in the  'CampName' list as 'Test' how will i show this value 'Test' as set in the list when the page gets loaded?


Best Answer chosen by Admin (Salesforce Developers) 

make sure that both Ids are identical i.e., both are 15 digit or 18 digit

All Answers



If ApexPages.CurrentPage().getParameters().get('Campaign') returns Campaign Id then just assign this Id to CampaignName, like

CampaignName = ApexPages.CurrentPage().getParameters().get('Campaign');

then selectlist automatically display the Querystring value as selected.


I passed the ID as window.open( '/apex/CreateNewJob?Campaign={!JobSuite__Campaign__c.Id}','_blank'); on the click of the button and in the constructor I have written the code in the constructor as follows:

CampaignName = ApexPages.CurrentPage().getParameters().get('Campaign');

And the remaining code is the same ,but still i was not able to set the Campaign value selected in the dropdown.


Please relpy,

Thanks in advance.


make sure that both Ids are identical i.e., both are 15 digit or 18 digit

This was selected as the best answer

Thanks Anusha,

I got the reason,  when I pass it through the query string it is 15 digit while the id of the dropdown is 18 digit.


Can you please tell me either how I can change the Campaign id which I am passing through the javascript to 18 digit or how I can change the Campaign id in the drop don to 15 digits so that i can set the value from the query string to the dropdown selected value?


Please reply,


Thanks in advance.


try this in JS,

CampaignId = CampaignId.substring(0,15);


I got the solution,


I used        Id idCampaign = ApexPages.CurrentPage().getParameters().get('Campaign');

and then i set the CampaignName = idCampaign;


Thanks for the reply Anusha.