Simple If/Else If Client Script Help

Adam Peterson
Kilo Sage

ServiceNow Scripting Experts,

I need help with my simple client script. It it OnChange and field is Knowledge Base.

What I want to happen is when the select their knowledge base, I want to assign their roles to it so only their department can see their knowledge base.

When I create a new article and put in Retail Tech in the Knowledge Base field - it does work as expected and assign the roles to 'retail technology'. BUT

if I put in Operations & Support - the roles goes to "retail technology" and not "operations_support". What am I doing wrong!?

Yes, I am new to scripting so please forgive me if I am missing something simple.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

var gr = new GlideRecord("kb_knowledge_base");

  if (gr.kb_knowledge_base = "Retail Tech"){

  g_form.setValue("roles", "retail technology");

  }

  else if (gr.kb_knowledge_base = "Operations & Support"){

  g_form.setValue("roles", "operations_support");

  }

}

Thanks in advance!

1 ACCEPTED SOLUTION

Hi Adam,



Without doing a call to the server to find the name for your sys_id in-hand, I think you could use the following client-side:



        var checkValue = g_form.getDisplayBox('kb_knowledge_base').value;


        if (checkValue == "Operations & Support"){


                  g_form.setValue("roles", "operations_support");


        }




This is for example only, please make sure to fill in with your correct values for comparisons, etc.



See how that works.


-Brian


View solution in original post

10 REPLIES 10

Brian Dailey1
Kilo Sage

Hi Adam,



If you mean to be checking the value of Knowledge Base selected on the form by the user, you should be using "newValue" instead of the "gr.kb_knowledgebase" in your comparison.   e.g.




if (newValue == "Retail Tech"){


        g_form.setValue("roles", "retail technology");


  }


  else if (newValue == "Operations & Support"){


        g_form.setValue("roles", "operations_support");


  }



You probably won't be using the GlideRecord part at all in this client-side script.




Thanks,


-Brian




Edit:   Sorry, forgot the extra "=" for the comparisons...


Thanks for your reply Brian. I did what you told me to do and it was still not working. I set up an alert so it would tell me the newValue. It was displaying the sys_id instead of value name. So when I put this in:



if (newValue == "1779ff616f05564080bc8bc44b3ee4f8"){


  g_form.setValue("roles", "operations_support");


  }



then it works.



How can I change the sys_id to the value name so I don't have to go find the sys_id for each knowledgebase? An example would be awesome. Thanks for your help!


Hi Adam,



Without doing a call to the server to find the name for your sys_id in-hand, I think you could use the following client-side:



        var checkValue = g_form.getDisplayBox('kb_knowledge_base').value;


        if (checkValue == "Operations & Support"){


                  g_form.setValue("roles", "operations_support");


        }




This is for example only, please make sure to fill in with your correct values for comparisons, etc.



See how that works.


-Brian


Rahul Jain11
Kilo Guru

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }



var gr = new GlideRecord("kb_knowledge_base");



  if (gr.getValue('kb_knowledge_base ') == "Retail Tech"){


  g_form.setValue("roles", "retail technology");


  } else if (gr.getValue('kb_knowledge_base ') == "Operations & Support"){


  g_form.setValue("roles", "operations_support");


  }


}



Please use == to compare and getValue() function to get the value