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

Nevermind - I believe it is working correctly.