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

Hi Adam,



Basically as per your script you are trying to do GlideRecord to the kb_knowledge_base table to search for the field "kb_knowledge_base" which doesn't exist. OOB there is no field on the table you mentioned.


However I am assuming that you want to do the operation on "kb_knowledge" table. Here is the updated script which should work.



Script :


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


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


          return;


    }



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



  if (kb_Base = "Retail Tech"){


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


  }


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


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


  }


}



I hope this helps


Hi Pradeep,



Actually, I think GlideRecord was mistakenly used here.   I believe he's just trying to set one value (role) on his form when another (knowledge base) is selected using and OnChange client-script, and can probably get it done by using the comparison and g_form




-Brian


Hi Adam,



The code I mentioned earlier should work for you to get the display value.


Please let me know if you have any questions.


Yep.   Sorry, didn't see that in your post earlier.   Was focused on his using GlideRecord.




-Brian


So if I change the field 'Knowledge Base' value to Retail Tech, it works great and adds the correct role. If I then go back and change the knowledge base value to operations and support, it doesn't change out the role.It still has old role in there. Shouldn't it change it out because this is an onChange client script?



Thanks!