Activity Forums Salesforce® Discussions How to control the data of a Picklist in Salesforce?

  • How to control the data of a Picklist in Salesforce?

    Posted by Rahul on January 3, 2018 at 11:57 am

    Hi, I am new in sfdc development. I have a Salesforce query...
    In case object, i have two fields, 1 is date time and another is picklist in which i have add time slote values like 00:00 - 01:00, 01:00 - 02:00,............, 23:00 - 24:00.

    Now  i Want that is i have put any datetime value like 03/01/2018 14:28 then in picklist only show slote values as datetime fields time like( starting with 14:00-15:00, 15:00-16:00,.... , 23:00-24:00) .

    How to handle this? Please help me.

    Subhendu replied 6 years, 3 months ago 3 Members · 4 Replies
  • 4 Replies
  • Charan Tej

    Member
    January 3, 2018 at 4:00 pm

    you need a develop a Salesforce Trigger on case to populate this.

    for(case c: trigger.new){

    if(c.date_field__c.hour()==1)
    c.picklist_field__c = '00:00-01:00';

    //you need to write logic this
    //you can even simplify this but as a newbie you can start like this
    }

  • Rahul

    Member
    January 4, 2018 at 8:46 am

    Thanks for reply.
    But through this code, it will update that picklist value after saving the case. but i want that as i will select the datetime , same time it will update to picklist before save  that case.

  • Subhendu

    Member
    January 5, 2018 at 12:08 pm

    Hi Rahul,

    As per my understanding, you are trying to have values in picklist depending upon the value in datetime field and you need to refresh it if the value in that field is changed. You can achieve this on a visualforce page. You will need to override standard buttons on Case Object to call that visualforce page.  The flow of the code will be somehow like-

    • Have an <apex:actionSupport> that will call the controller method to get picklist values on OnChange event of that datetime input field.
    • Pass the value of datetime field using <apex:param> to controller.
    • Calculate the values that you need to show in picklist based on the value entered in datetime field in the controller method and generate a list of the desired picklist values.
    • Assign the list of picklist values to a Property defined in the controller. Have that property called on that picklist field.

    In such way, the picklist values will populate on the fly while editing the record. You can either create a custom controller or you can set standard controller as Case on the page and implement this logic in page extension.

    Hope this gives you an idea of how you can achieve what you aspire.

  • Subhendu

    Member
    January 5, 2018 at 12:18 pm

    In continuation of my previous reply, you also have to call the actiosupport on html-oninput event as OnChange only works when you select any value. html-oninput event fires as soon as you select any value.

Log In to reply.

Popular Salesforce Blogs