Auto populate choice from dropdown

Rutuja K
Giga Guru

I have a requirement to set default value for dropdown when there is only one choice available.

 

For some records there can be multiple choices available, for some there can be only one. If there is 1 choice available for selection then I want it to auto-populate so that user don't need to select it manually.

 

Is it possible? Can someone help me with how to do it? 

 

Thank You!

Rutuja

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hello @Rutuja K ,

 

You can easily achieve this by using a client script.

Hint:

    1. Server-Side Choice Count:
      1. Create a Script Include function to count the number of choices available for a specific record.
      2. Use GlideRecord to query the relevant table and field based on the record ID or name.
      3. Return the count of retrieved records.

     

    1. Asynchronous Server-Side Call:
      1. From the client script, use GlideAjax to make an asynchronous call to the Script Include function.
      2. Pass the record ID or name as a parameter to the function.
      3. Handle the response in the onSuccess callback function.

     

    1. Conditional Value Setting:
      1. In the onSuccess callback, check the received choice count.
      2. If only one choice is available, use g_form.setValue to set the value of the dropdown field to the first choice.

If I misunderstood something, please provide me with more information to help me understand your requirement better.

View solution in original post

6 REPLIES 6

Hi Prasad, 

 can you please provide more details on Asynchronous Server-Side Call step.

I have created SI:

gethost:function(id)
  {var bu_ser=new GlideRecord('cmdb_ci_business_app');
bu_ser.addEncodedQuery('number='+id);
bu_ser.query();
while(bu_ser.next())
{
        var type=new GlideRecord('u_host_rel');
   type.addEncodedQuery('u_cloud_type_val='+bu_ser.u_cloud+'^u_install='+bu_ser.install);
   type.query();
   var host=[];
   while(type.next())
   {
host.push(type.u_host_value.toString());
   }
   gs.log('ADI APM TEST '+host);
   return 'sys_idIN' +host;
}
 
 
Thanks!
Rutuja

Community Alums
Not applicable

Hi Rutuja,

 

An asynchronous server-side call is like sending a message and doing other things while waiting for a reply. It helps keep everything running smoothly without waiting around. You can use the GlideAjax class to call the script include function. 
Please refer to this detailed article on How to call script include from client script with an example.
Follow the hints provided in the trail.