How to set "None" Value in Choice field

chanikya
Tera Guru

Hi,

if User is has no location value then display none value , should not display any other Choice Values

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
var user=new GlideRecord('sys_user');
user.addQuery("sys_id", newValue);
user.query();
if(user.next())
{
alert(user.name);
var dep=new GlideRecord("cmn_location");
dep.addQuery("sys_id", user.location);
dep.query();
if(dep.next())
{
alert(dep.name);
    var yy=dep.name;
    g_form.setValue('comments',yy);
    
if(yy!='')
    {
    if(yy=='Americas')
        {
g_form.clearOptions('category');
g_form.addOption('category','ktlo','KTLO');
}
if(yy != 'Americas')
{
g_form.clearOptions('category');
g_form.addOption('category','project_work','Project/Project Task');
g_form.addOption('category','task_work','Task work');
g_form.addOption('category','admin','Admin');
g_form.addOption('category','meeting','Meeting');
g_form.addOption('category','ooo','Out of office');
g_form.addOption('category','training','Training');
g_form.addOption('category','external_labor','External labor');
g_form.addOption('category','time_off','Time off');
g_form.addOption('category','appointment','Appointment');
g_form.addOption('category','call','Phone call');
    
}

if(isLoading && !g_form.isNewRecord()){
g_form.setValue('category', yy);
}        
}
}
    else
        {
            alert('location empty');
            g_form.setValue('category', 'None');
        }
}
}

 

1 ACCEPTED SOLUTION

In the category variable/field, use choice as 'Dropdown with --None--'

 

find_real_file.png


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Chanikya,

I assume the field is drop down with None. If you want to set None then use g_form.setValue('<field>', '');

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

How to set None Value , if user doesn't have any Location .

 

SanjivMeher
Kilo Patron
Kilo Patron

Just change the below marked in bold

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
var user=new GlideRecord('sys_user');
user.addQuery("sys_id", newValue);
user.query();
if(user.next())
{
alert(user.name);
var dep=new GlideRecord("cmn_location");
dep.addQuery("sys_id", user.location);
dep.query();
if(dep.next())
{
alert(dep.name);
    var yy=dep.name;
    g_form.setValue('comments',yy);
    
if(yy!='')
    {
    if(yy=='Americas')
        {
g_form.clearOptions('category');
g_form.addOption('category','ktlo','KTLO');
}
if(yy != 'Americas')
{
g_form.clearOptions('category');
g_form.addOption('category','project_work','Project/Project Task');
g_form.addOption('category','task_work','Task work');
g_form.addOption('category','admin','Admin');
g_form.addOption('category','meeting','Meeting');
g_form.addOption('category','ooo','Out of office');
g_form.addOption('category','training','Training');
g_form.addOption('category','external_labor','External labor');
g_form.addOption('category','time_off','Time off');
g_form.addOption('category','appointment','Appointment');
g_form.addOption('category','call','Phone call');
    
}

if(isLoading && !g_form.isNewRecord()){
g_form.setValue('category', yy);
}        
}
}
    else
        {
            alert('location empty');

g_form.setValue('category', '');

g_form.clearOptions('category');
        }
}
}


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjeev,

 

Thanks for your reply,

if Location is not Americas, then display Options with starting with None Value . So How can i fix it please

if(yy != 'Americas')
{
g_form.clearOptions('category');
g_form.addOption('category','project_work','Project/Project Task');
g_form.addOption('category','task_work','Task work');
g_form.addOption('category','admin','Admin');
g_form.addOption('category','meeting','Meeting');
g_form.addOption('category','ooo','Out of office');
g_form.addOption('category','training','Training');
g_form.addOption('category','external_labor','External labor');
g_form.addOption('category','time_off','Time off');
g_form.addOption('category','appointment','Appointment');
g_form.addOption('category','call','Phone call');
    
}