Sunday, 20 July 2014

How to add Dynamic Picklist values in Salesforce!!!!

Hi guys!

Its been a long time since I have Written anything. As, I am a also a kind of beginner in salesforce . am also practicing these days with you guys. So , I was just surfing the internet and I found that what if we have to add Dynamic picklist values.
Although there are number of blogs which are available to above scenario. I have also tried the same . So , I thought it would be great to share with you people.

So , Lets start it ...

I have a requirement that an object i.e is called as DynamicObject which has records . I also have a page named as dynamic page, where I have a text  field . Now, Whenever I type any valuer in that text box , then it should get added to the picklist that is shown on visualforce page  . And behind the salesforce it should get added as an record in DynamicObjects .

Use case of this scenario:

There are many websites where it is asked to add your school name and  and in this whole universe unfortunately your school name doesn't exist in the picklist value. So in that case visitor needs to type it  manually. Done! .  But what if another person who is from same school needs to write again . To avoid this situation lets include this functionality of adding value to the picklist. So if the next person comes and tries to find his school name should get in the picklist .


Here is the code ,

Public class dynamicPicklist{
    public string value{get;set;}
    public List<SelectOption> getoptionss(){
          List<SelectOption> options = new List<SelectOption>();
          List<AnkushTest__DynamicObjects__c> citylist = new List<AnkushTest__DynamicObjects__c>();
           valuelist = [Select Id, AnkushTest__PicklistValue__c FROM AnkushTest__DynamicObjects__c];
          options.add(new SelectOption('--None--','--None--'));
          for (Integer j=0;j<citylist.size();j++)
              {
                  options.add(new SelectOption(valuelist[j].AnkushTest__PicklistValue__c,valuelist[j].AnkushTest__PicklistValue__c));
              }
          return options;
    }

    public pageReference Saves()
        {
          AnkushTest__DynamicObjects__c newrec = new AnkushTest__DynamicObjects__c (AnkushTest__PicklistValue__c = newpicklistvalue);
          insert newrec;
         // newpicklistvalue=NULL;
          return null;
        
    }
        public String newpicklistvalue{get; set;}
            Public string getvaluess(){
        
        return  newpicklistvalue;
        
    }

}




<apex:page controller="dynamicPicklist" sidebar="false">
    <apex:pageBlock >
        <apex:form >
            <apex:pageBlockSection title="Dynamic picklist" columns="1">
              <apex:pageblocksectionItem >
                  <apex:outputlabel value="value" for="values" />
                  <apex:selectList value="{!value}" size="1" id="values">
                   <!--  <apex:actionSupport event="onchange" reRender="newpicklistvalue1" />-->
                      <apex:selectOptions value="{!optionss}"/>
                  </apex:selectList>
              </apex:pageblocksectionItem>
             <apex:commandButton action="{!Saves}" value="Add" id="theButton"/>
             <apex:inputText value="{!newpicklistvalue}" label="New value " id="newvalue"/>
             <apex:outputtext value="The name entered is {!valuess}"  id="newpicklistvalue1"/>
         </apex:pageBlockSection>
        </apex:form>
  </apex:pageBlock>
</apex:page>

Just try .. And if u r stuck atny point Just leave me a message at ankushsalesforce@gmail.com


Happy Coding!!!
Cheers
































No comments:

Post a Comment

oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...