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
Denise CrosbyDenise Crosby 

add "+ New Contact" to SelectOption list

Hello. I have an Apex class and Visual Force page to create an event, where the user first selects the Account and then the page will display only the Contacts associated with that Account. I need my List<SelectionOption> to show the "+ New Contact" option that appears when you create a regular event in Salesforce. Is there a way to do this?
 
public List<SelectOption> contactOptions
    {
        get
        {
            if ( contactOptions == null && dummyContact.AccountId != null )
            {
                contactOptions = new List<SelectOption>();
                contactOptions.add( new SelectOption( 'null', '--None--' ) );
                for ( Contact contact :
                    [   SELECT  Id, Name
                        FROM    Contact
                        WHERE   AccountId = :dummyContact.AccountId
                    ]
                    )
                {
                    contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
                }
            }
            return contactOptions;
        }
        private set;
    }

 
Best Answer chosen by Denise Crosby
Amit Chaudhary 8Amit Chaudhary 8
On selectOption you need to add actionSupport and need to apex Method to create new contact base on picklist value

<apex:actionSupport event="onChange" action="{!createNewContact}" reRender="tableShow"/>


<apex:selectList value="{!SelectedContact}" size="1">
<apex:actionSupport event="onChange" action="{!createNewContact}" reRender="tableShow"/>
<apex:selectOptions value="{!contactOptions}"/>
</apex:selectList>


Method should be like below

public pageReference createNewContact()
{
     if(SelectedContact=='New Contact')
    {
           return new pageReference('/003/e');
    }
    return null
}


Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try like below
public List<SelectOption> contactOptions
    {
        get
        {
            if ( contactOptions == null && dummyContact.AccountId != null )
            {
                contactOptions = new List<SelectOption>();
                contactOptions.add( new SelectOption( 'null', '--None--' ) );
                for ( Contact contact :
                    [   SELECT  Id, Name
                        FROM    Contact
                        WHERE   AccountId = :dummyContact.AccountId
                    ]
                    )
                {
                    contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
                }
				contactOptions.add( new SelectOption( 'New Contact', '+ New Contact' ) );

            }
            return contactOptions;
        }
        private set;
    }
Let us know if this will help you
 
Denise CrosbyDenise Crosby
Hi Amit,
Thanks for your reply. Yes, that works to add "+ New Contact" to the drop down but I need to figure out how to navigate to the New Contact page and navigate back after the new contact is added. I am new to coding Apex, so please let me know if you have any advice on this. Thank you.
Amit Chaudhary 8Amit Chaudhary 8
On selectOption you need to add actionSupport and need to apex Method to create new contact base on picklist value

<apex:actionSupport event="onChange" action="{!createNewContact}" reRender="tableShow"/>


<apex:selectList value="{!SelectedContact}" size="1">
<apex:actionSupport event="onChange" action="{!createNewContact}" reRender="tableShow"/>
<apex:selectOptions value="{!contactOptions}"/>
</apex:selectList>


Method should be like below

public pageReference createNewContact()
{
     if(SelectedContact=='New Contact')
    {
           return new pageReference('/003/e');
    }
    return null
}


Let us know if this will help you
 
This was selected as the best answer
Denise CrosbyDenise Crosby
Sorry for the newbie questions, but I'm having a problem figuring out what url to use to navigate to the new contact page. I also need to pass the Account id to the page, so it is pre-populated. After saving the contact, I need to navigate back to my custom page. Thank you for your wonderful help.
 
<apex:selectList label="{!contactLabel}" value="{!contactId}" size="1" rendered="{!showContactOptions}">
        <apex:selectOptions value="{!contactOptions}" />
        <apex:actionSupport action="{!changeContact}" event="onchange" rerender="eventInformation" /></apex:selectList>


public pageReference changeContact()
    {
        if (ev.WhoId == null || ev.WhoId =='null') 
        	{
                ev.WhoId = null;
         		return null;
            }
        else if (ev.WhoId == '0')
        	{
            	ev.WhoId = null;
                return new pageReference('/sObject/Contact/list?filterName=Recent');                
            }
        else 
        	{
                ev.WhoId = Id.valueOf( contactId );
                return null;
            }
    }

 
Denise CrosbyDenise Crosby
I think I've decided to use a commandButton. This seems a lot simplier. I will just have to figure out how to get back to my original page after saving.

   <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact)}"/>
Denise CrosbyDenise Crosby
Thank you Amit!!