- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2024 06:59 AM - edited 02-11-2024 10:40 AM
I am developing a button ui action in the ritm record.
in the script i have inserted 2 variables, one takes the sys_id of a knowledge and the other takes the sys_id of a Knowledge article, which are inserted in a link that leads to the creation of a new Knowledge article assigned to the knowledge of the first variable and as parent the Knowledge article of the second variable, even if the link seems correct it only populates the field of the first sysparm_query it encounters, the second ignores it.
if i try to invert the sysparm_query it always has the same behaviour it changes the populated field.
var kb = current.variables.select_a_knowledge;
var kb_article = current.variables.knowledge_article;
action.setRedirectURL ( 'https://<istance>.service-now.com/kb_knowledge.do?sys_id=-1&sysparm_query=parent='+ kb_article +'&sysparm_query=kb_knowledge_base='+ kb +'&sys_target=kb_knowledge&sysparm_query=sys_class_name!=kb_knowledge_block&sysparm_referring_url=kb_knowledge_list.do?sysparm_query=sys_class_name%2521%253Dkb_knowledge_block%255EEQ');
My question is:
how to insert 2 sysparm_queries with 2 different sys_ids in the same url?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2024 11:40 PM - edited 02-12-2024 01:23 AM
how I solved this problem:
I changed the ui action link:
var kb = current.variables.select_a_knowledge;
var kb_article = current.variables.knowledge_article;
action.setRedirectURL ( 'https://<istance name>.service-now.com/kb_knowledge.do?sys_id=-1&sysparm_parent='+ kb_article+'&sysparm_kb='+ kb+'&sysparm_cat_item=e78d38a51b37b554e6267885464bcb98&sys_target=kb_knowledge&sysparm_query=sys_class_name!=kb_knowledge_block&sysparm_referring_url=kb_knowledge_list.do?sysparm_query=sys_class_name%2521%253Dkb_knowledge_block%255EEQ');
// in the link above I have modified the sysparms with unique names, we will need this later, here I have made them easier to read, the names after the sysparm can be of any type they are not tied to a field
sysparm_kb='+ kb
sysparm_parent='+ kb_article
sysparm_cat_item=e78d38a51b37b554e6267885464bcb98
I have made the UI action only visible when the RITM has the catalogue item with 'e78d38a51b37b554e6267885464bcb98' as an item
current.cat_item == 'e78d38a51b37b554e6267885464bcb98'
I do a test run and the link is generated correctly:
https://<istance>.service-now.com/nav_to.do?uri=%2Fkb_knowledge.do%3Fsys_id%3D-1%26sysparm_parent%3Df2765f9fc0a8011b0120ec1b352bf09b%26sysparm_kb%3D6d6c1e521bc48ad4e6267885464bcb2b%26sysparm_cat_item%3De78d38a51b37b554e6267885464bcb98%26sys_target%3Dkb_knowledge%26sysparm_query%3Dsys_class_name!%3Dkb_knowledge_block%26sysparm_referring_url%3Dkb_knowledge_list.do%3Fsysparm_query%3Dsys_class_name!%3Dkb_knowledge_block%5EEQ
but the fields are not yet populated, to do so we remain on the page that opened the UI action --> click on the hamburger menu --> configure --> Client Script
on the newly created client I put this code:
function onLoad() {
var gUrl = new GlideURL();
gUrl.setFromCurrent(); // gets the current url
var cat_item = gUrl.getParam("sysparm_cat_item"); // takes the value found in the link after the equal of sysparm_cat_item
// check that the value is equal to the sys_id of the cat_item
if(cat_item == 'e78d38a51b37b554e6267885464bcb98'){
var parent = gUrl.getParam("sysparm_parent"); //takes the value found in the link after the equal of sysparm_parent
var kb = gUrl.getParam("sysparm_kb"); //takes the value found in the link after the equal of sysparm_kb
//set values in the form
g_form.setValue('parent', parent);
g_form.setValue('kb_knowledge_base', kb);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2024 11:22 AM
how to insert 2 sysparm_queries with 2 different sys_ids in the same url?
The answer is: you can't.
However you can include multiple query conditions into the sole parameter.
E.g:
var $gu = new GlideURL(gs.getProperty('glide.servlet.uri') + 'kb_knowledge.do');
$gu.set('sys_id', '-1');
$gu.set('sysparm_query', getQuery(current));
$gu.set('sys_target', 'kb_knowledge');
$gu.set('sysparm_referring_url', 'kb_knowledge_list.do?sysparm_query=sys_class_name!=kb_knowledge_block^EQ');
gs.debug($gu.toString())
function createCondition (conditions) {
return function (key) {
return key + '=' + GlideStringUtil.escapeQueryTermSeparator(conditions[key]);
};
}
function getQuery (current) {
var conditions = {
'parent': '' + current.variables.knowledge_article,
'kb_knowledge_base': '' + current.variables.select_a_knowledge,
};
return Object.keys(conditions).map(createCondition(conditions)).join('^');
}
This will generate a URL along the lines of:
https://dev190583.service-now.com/kb_knowledge.do?sys_id=-1&sysparm_query=parent%3D7eee6e2993d40210099cfcf08bba106d%5Ekb_knowledge_base%3Dbeee6e2993d40210099cfcf08bba106d&sys_target=kb_knowledge&sysparm_referring_url=kb_knowledge_list.do%3Fsysparm_query%3Dsys_class_name%21%3Dkb_knowledge_block%5EEQ
where interesting is URL parameter
&sysparm_query=parent%3D7eee6e2993d40210099cfcf08bba106d%5Ekb_knowledge_base%3Dbeee6e2993d40210099cfcf08bba106d
which when decoded reveals value:
parent=7eee6e2993d40210099cfcf08bba106d^kb_knowledge_base=beee6e2993d40210099cfcf08bba106d
so - as you can see - it is basically an encoded query.
When applying that encoded query to a GlideRecord, it would read something like:
var $gr = new GlideRecord('...');
$gr.addQuery('parent', '7eee6e2993d40210099cfcf08bba106d');
$gr.addQuery('kb_knowledge_base', 'beee6e2993d40210099cfcf08bba106d');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2024 11:29 AM
B.t.w. if you prefer vernacular JavaScript, the same can be obtained running script:
var url = gs.getProperty('glide.servlet.uri') +
'kb_knowledge.do' +
'?sys_id=' + encodeURIComponent('-1') +
'&sysparm_query=' + encodeURIComponent('parent=7ee...06d^kb_knowledge_base=bee..06d') +
'&sys_target=' + encodeURIComponent('kb_knowledge') +
'&sysparm_referring_url=' + encodeURIComponent('kb_knowledge_list.do?sysparm_query=sys_class_name!=kb_knowledge_block^EQ')
gs.debug(url);
where 7ee...06d and bee..06d are shorthand for the sys_ids of the parent knowledge article and the knowledge base.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2024 11:40 PM - edited 02-12-2024 01:23 AM
how I solved this problem:
I changed the ui action link:
var kb = current.variables.select_a_knowledge;
var kb_article = current.variables.knowledge_article;
action.setRedirectURL ( 'https://<istance name>.service-now.com/kb_knowledge.do?sys_id=-1&sysparm_parent='+ kb_article+'&sysparm_kb='+ kb+'&sysparm_cat_item=e78d38a51b37b554e6267885464bcb98&sys_target=kb_knowledge&sysparm_query=sys_class_name!=kb_knowledge_block&sysparm_referring_url=kb_knowledge_list.do?sysparm_query=sys_class_name%2521%253Dkb_knowledge_block%255EEQ');
// in the link above I have modified the sysparms with unique names, we will need this later, here I have made them easier to read, the names after the sysparm can be of any type they are not tied to a field
sysparm_kb='+ kb
sysparm_parent='+ kb_article
sysparm_cat_item=e78d38a51b37b554e6267885464bcb98
I have made the UI action only visible when the RITM has the catalogue item with 'e78d38a51b37b554e6267885464bcb98' as an item
current.cat_item == 'e78d38a51b37b554e6267885464bcb98'
I do a test run and the link is generated correctly:
https://<istance>.service-now.com/nav_to.do?uri=%2Fkb_knowledge.do%3Fsys_id%3D-1%26sysparm_parent%3Df2765f9fc0a8011b0120ec1b352bf09b%26sysparm_kb%3D6d6c1e521bc48ad4e6267885464bcb2b%26sysparm_cat_item%3De78d38a51b37b554e6267885464bcb98%26sys_target%3Dkb_knowledge%26sysparm_query%3Dsys_class_name!%3Dkb_knowledge_block%26sysparm_referring_url%3Dkb_knowledge_list.do%3Fsysparm_query%3Dsys_class_name!%3Dkb_knowledge_block%5EEQ
but the fields are not yet populated, to do so we remain on the page that opened the UI action --> click on the hamburger menu --> configure --> Client Script
on the newly created client I put this code:
function onLoad() {
var gUrl = new GlideURL();
gUrl.setFromCurrent(); // gets the current url
var cat_item = gUrl.getParam("sysparm_cat_item"); // takes the value found in the link after the equal of sysparm_cat_item
// check that the value is equal to the sys_id of the cat_item
if(cat_item == 'e78d38a51b37b554e6267885464bcb98'){
var parent = gUrl.getParam("sysparm_parent"); //takes the value found in the link after the equal of sysparm_parent
var kb = gUrl.getParam("sysparm_kb"); //takes the value found in the link after the equal of sysparm_kb
//set values in the form
g_form.setValue('parent', parent);
g_form.setValue('kb_knowledge_base', kb);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 12:42 AM
You're welcome, but I don't think you understood correctly what I was trying to say.
It is true that one cannot specify sysparm_queries multiple time, but within the value of parameter sysparm_queries one can specify as many fields and their values as one wants.
For that purpose the syntax of the value for sysparm_queries is:
<fiel name>=<field value>
joined by the ^ (caret) character.
E.g. to specify default values
- Short description: Something went wrong
- Urgency: 3
- Impact 3
- Description: I can no longer print to the known device
one would join using caret the following:
short_description=Something went wrong
urgency=3
impact=3
description=I can no longer print to the known device
the end result being:
short_description=Something went wrong^urgency=3^impact=3^description=I can no longer print to the known device
The idea is that the syntax to specify default values using parameter sysparm_queries is the same as that of the encoded query to filter a GlideRecord.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 12:49 AM
Also just as a side note, the impossibility of specifying a parameter twice has to do with the way URLs work, not with the way SN works.