Server Script If Statements
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 01:57 PM
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:
Roles | Location | Knowledge Articles |
Analyst | Washington, DC | KB 100 |
Analyst | New York, NY | KB 200 |
Analyst | San Francisco, CA | KB 300 |
Washington, DC | KB 400 | |
Analyst | KB 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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2017 02:49 PM
I made an if structure based on your description. There is probably a way to refactor it but this should still work:
if(role == "Analyst" && location == "Washington, DC") {
//code to serve up KB 100
}else if(role == "Analyst" && location == "New York, NY") {
//code to serve up KB 200
}else if(role == "Analyst" && location == "San Francisco, CA") {
//code to serve up KB 300
}else if(role == "Analyst" && (locaton != "Washington, DC"
&& location != "New York, NY"
&& location != "San Francisco, CA")) {
//code to serve up KB 500
}else if(role != "Analyst" && location == "Washington, DC") {
//code to serve up KB 400
}else if(role != "Analyst" && (locaton != "Washington, DC"
&& location != "New York, NY"
&& location != "San Francisco, CA")) {
//code to serve up KB 600
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2017 11:08 AM
Thanks Justin, but is there a way to write this script so that it's dynamic? I want to base it on the value of location and role without having to hard code Washington DC or New York, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2017 11:29 AM
Hi David,
Yes what you described is possible. However in my experience the best way to approach that problem is to generate a support table.Your new custom table will hold the value of the locations that you are trying to "single-out" you can either accomplish this using hard-coded/display_value string inputs (ie: "Washington, DC", "New York, NY"...) to represent the location record. What I would highly suggest is that instead you use a reference to the location table on your custom support table. Once the table is assembled, query the table and create a GlideRecord of your "locations". Depending on the situation once I have called my GlideRecord I will iterate through my responses with a while loop in correlation of using IF statements you will be able to achieve the same functionality but with a more dynamic solution that scales much more nicely. Please let know if you have any more questions and I will be happy to work through them with you.
Best Regards,
Philip Engles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2017 11:38 AM
Hi Philip, thanks much for your reply. I've actually done exactly as you've described. I have a new table that has three reference fields (location, occupational role, and knowledge article). Essentially this table just connects those three fields together so we know which articles go to what person. For my query, I am creating a GlideRecord for this new table and for HR Case. When a HR Case's location and role matches the new table, I want the correct knowledge article (KA) to be presented. If the location does not match or is missing, I want to serve them a default Roles-based article. If the opposite happens, I want them to see a default locations-based article. And finally if the user does not have a role or a location that matches, I want them to see a general article. My issue is getting the code to show those three scenarios where either the role or location or both are missing. Any help you can provide is greatly appreciated!