0

My scenario : I am creating visualforce page for custom object. I have one Picklist field(say, city__c). If user choose ---other--- option from the picklist, I must enable a textbox and Add button. When user type some text and click on add button the value should appear on options of picklist without reloading the page. Then user can choose the option that he added to picklist and can save the record.

  1. I heard it is possible only by calling metadata api to add a value to picklist field. So anyone is there to show me How to call this metadata in vfp controller?
  2. Is it possible to call a metadata without reloading the page? Because when Add button is clicked I want to add that value instantly to picklist without reloading the page(like using actionsupport).

It would be very much helpful if I found the solution.

  • I believe this can be done by using javascript. – Hope Feb 19 '16 at 8:30
  • @Hope - code snippet with example is more helpful – mohammed azarudeen Feb 19 '16 at 8:31
  • I don't know anything about your use case, but have you considered another object with a lookup relationship. If this is only a city name, and no other data, then it wouldn't make sense, but are there other related values or data, they could all go there. Just a thought. – pchittum Feb 19 '16 at 8:35
  • @peter - City is one of the field in a object. This object has lookup field too. – mohammed azarudeen Feb 19 '16 at 8:49
1

You can add picklist values in your controller class so you can create a custom picklist on your page and add values that you need there (Also from your real picklist field).

<apex:selectlist value="{!somevalue}" multiselect="false" size="1">
    <apex:selectoptions value="{!someCollection}"/>
</apex:selectlist>

And if you don't want the entire page to be reloaded you will need to use apex:actionRegion to only reload a part of your page.

  • Thanks for your interest. I shouldn't add manually in a controller class. If I add Manually I can get the added values in that vfp only. So I cannot use anywhere else. Is there any way out there to save in a real picklist field and use it permanently?. Ofcourse there are action components to reRender the block. – mohammed azarudeen Feb 19 '16 at 10:45
  • You might want to check this post salesforce.stackexchange.com/questions/49057/… there is some relevant information on what you need. – Oles Malkov Feb 19 '16 at 10:46
1

Well by default if they enter a value and it is assigned to the picklist and teh picklist is unrestricted - you will see that the going to add an inactive value ( see below example ):

Picklist:

Unrestricted Picklist

Initial Picklist State

Example 1 ( using a controller ):

<apex:page standardController="Report_Type__c" extensions="Test" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!Report_Type__c.Name}" required="true" />
                <apex:pageBlockSectionItem />
                <apex:inputField value="{!Report_Type__c.Sample_Picklist__c}" />
                <apex:inputText  value="{!newValue}" />

            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller Code:

public class Test{

    public String newValue { get; set; }
    public Report_Type__c obj { get; set; }
    ApexPages.standardController sc { get; set; }

    public Test( ApexPages.standardController controller ){
        sc = controller;
        obj = ( Report_Type__c )controller.getRecord();
    }

    public PageReference Save(){

        obj = ( Report_Type__c )sc.getRecord();

        if( obj.Sample_Picklist__c == null && !String.isBlank( newValue )){ obj.Sample_Picklist__c = newValue;    }

        try{
            Database.insert( obj );
        }
        catch( Exception ex ){
            System.debug( 'Error saving Report Type: ' + ex.getMessage() );
        }

        PageReference result = Page.Test;
        if( obj.Id != null ){
            result.getParameters().put( 'id', obj.Id );
        }
        result.setRedirect( true );

        return result;
    }
}

Results:

Before Save of Test Page

Result Using controller Save

Option 2 ( just Visual force and standard Controller ) :

Page Code:

<apex:page standardController="Report_Type__c"  >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!Report_Type__c.Name}" required="true" />
                <apex:pageBlockSectionItem />
                <apex:inputField value="{!Report_Type__c.Sample_Picklist__c}" />
                <apex:inputText  value="{!Report_Type__c.Sample_Picklist__c}" />

            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Results:

Standard Controller Before Save Result standard Controller

You can also populate a picklist within the controller by looping through Schema.PicklistEntry[] and adding each value - if they add one in text box - you can add it to the list and return null instead of a new page reference - this will also show your list - if you are looking to add new values for All users to select moving forward - you can try something like the code in this guy's question to do this before returning your page after saving. This should update the value with the value that the user entered ( given you do a little variable swapping from what he has - same concept )

Hope it helps answer your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.