2

I have overridden the new Opportunity page with a Visualforce page, which means there are 3 main routes for the Opportunity to be created:

  1. From an Account page
  2. From a Contact page
  3. From an Opportunity page

I have a dynamically generated picklist of Contacts, so the user can select a Contact at the point of Opportunity creation (creating the OpportunityContactRole). If the user selects route 2 I would like to set the value of this picklist to match the Contact record they clicked through from - the user should still be able to change if it desired.

The Contact Id is passed through so this is available for use. Page/Picklist snippet:

<apex:selectList value="{!SelectedCon}" size="1" label="Select Contact">
    <apex:selectOptions value="{!con}"/>
    <apex:actionSupport event="onchange" reRender="test"/>
</apex:selectList>

Controller snippet:

Public List<SelectOption> getCon(){
List<SelectOption> conOptions = new List<SelectOption>();

conOptions.add(new SelectOption('','Please select a Contact'));
List<Contact> conList = new List<Contact>([SELECT Id, Name FROM Contact Where AccountId = :AccId]);
For (Contact cn : conList){
    conOptions.add(new SelectOption(cn.Id,cn.Name));           
}        
conOptions.add(new SelectOption('New Contact','New Contact')); //Option to create a new Contact
return conOptions;
}

How can I set the picklist to match the passed conid upon pageload? Happy to look at javascript but I have very little experience of it.

1 Answer 1

1

If conId is being passed on pageload can you try

 Public List<SelectOption> getCon(){
 List<SelectOption> conOptions = new List<SelectOption>();      
 if(conid != null){
    string conname = [select id, name from contact where id =: 'conid' limit 1].name;
    conOptions.add(new(selectoption(conid,conname);
 }
 else{
    conOptions.add(new SelectOption('','Please select a Contact'));
    List<Contact> conList = new List<Contact>([SELECT Id, Name FROM Contact Where AccountId = :AccId]);
    For (Contact cn : conList){
        conOptions.add(new SelectOption(cn.Id,cn.Name));           
    }        
    conOptions.add(new SelectOption('New Contact','New Contact')); //Option to create a new Contact
}    
return conOptions;
    }
1
  • 1
    Thanks, that helped. I modified it slightly so the list was still generated but the conid option was first instead of 'Please select a Contact'
    – Girbot
    Oct 14, 2014 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .