Server Script If Statements

yundlu316
Kilo Guru

Hi Experts, I'm trying to code a widget in Service Portal that serves up the correct Knowledge Article based on a person's role and location.   There's an added layer of complexity when either the Role or Location or even both do not match.   In those cases, I would like them to be served up with a default article.   Here are some of the scenarios:

find_real_file.png

                                                                   

RolesLocationKnowledge Articles
AnalystWashington, DCKB 100
AnalystNew York, NYKB 200
AnalystSan Francisco, CAKB 300
Washington, DCKB 400
AnalystKB 500

KB 100 - 300 are pretty self explanatory.   If those combinations are the same as the User, then they get served with those articles.   However, if a User is an Analyst but not in any of those three cities, I would like them to be served with KB 500.   If a User is in DC but is not an Analyst, I want them to be served with KB 400.   Finally, if the User is not an Analyst and not in one of those three cities, I want them to be served with KB 600.  

I have a beginner's understanding of angular js, so getting the Knowledge Article values for the first three cases I think looks like this:

var hr = new GlideRecord('my_hr_cases');

hr.addQuery('hr_profile', gs.getUserID());

var ka = new GlideRecord('my_knowledge');

if(hr.hr_profile.user.location == ka.location && hr.position.role == ka.role)

{

data.article = ka.knowledge_article.getHTMLValue();

}

else if()

I'm stuck in how to expand on this so the other scenarios would provide the correct Knowledge Article values.   My end step is to dynamically set URL based on these combinations with HTML:

<a id="KA_link" href="/kb_view.do?sysparm_article={{data.article}}" target="_blank">The Right Article</a>

Any help is greatly appreciated.

Thanks!

10 REPLIES 10

I am a little confused about what you are trying to accomplish are you trying to create a table to hold all of these records that fit your above mentioned criteria?


I had the exact same situation where  I was calling a REST webservice, and based upon the response of the REST I needed to look up some values and then display those values  to a form. 

I created a support table and I looked up the value received from REST into this support table using GlideRecord and then used another GlideRecord to display values on the form.

The support table is a much better and scalable solution as it allows me to manage the entries in it independent of the REST service and core business logic of my application.  

I ran into a situation where I needed to use a similar script however I have a challenge. The difference here is that the user will be selecting one of 4 options and I am unsuccessful in having the script run:




Select box choices are:


Memorial Support Services - 2900 Corporate Way, Miramar


Memorial Training Center - 2990 Executive Way, Miramar


Memorial Information Technology - 3051 N. Commerce Parkway,Miramar


Community Youth Svc - 7031 Taft ST, Hollywood




=========This is the script ==========




// answer = ifScript();
//gs.log("current facility " + current.variables.facility);
//gs.log("current facility 2 " + current.variables.facility.getDisplayValue());
answer = '';
// function ifScript() {
if (current.variables.facility.name == 'Memorial Support Services - 2900 Corporate Way, Miramar') {
  answer = 'yes';
 
}


  if (current.variables.facility.name == 'Memorial Training Center - 2990 Executive Way, Miramar') {
  answer = 'yes';
  }


  if (current.variables.facility.name == 'Memorial Information Technology - 3051 N. Commerce Parkway,Miramar') {
  answer = 'yes';
 
  }


  if (current.variables.facility.name == 'Community Youth Svc - 7031 Taft ST, Hollywood') {
  answer = 'yes';
 
  }


      else {
      answer = 'no';
     
      }


========================



I think I need to incorporate a 'or' condition in the script.


FYI: I can get this to work using conditions however I want to use a script.


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Maybe this doesn't fit your use case, but have you considered User Criteria.   That would allow you to entitle the articles to the right audience with standard platform capabilities. (Create a user criteria record && Select user criteria for a knowledge base )


Screen Shot 2017-03-04 at 8.13.44 PM.png


I would go for this as well.. problem is that if you aint on Istanbul, User Criteria is only working per knowledge and not per article. Perhaps it worth waiting for Istanbul upgrade?



//Göran