How to add a button to the Requested Item form?

Matt Cordero1
Tera Guru

Hello all,

I have a Client Script that I am using to try to add a button next to a variable, called "Requested By".  NOTE: The variable is within a variable set.

function onLoad() {
try{
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var varControl = form.getControl("requested_by").id;

$(varControl).insert({
after: '<span><a id="icon-check-circle" class="btn btn-default sc-ref-spacer icon-check-circle sn-tooltip-basic" title="" data-original-title="Check availability"></a></span>'
});
}
catch(e){
alert(e.message);
}
}

Ideally, the button would appear here, inside the Variables, that's why I am trying to add it via Client Script.

find_real_file.png

I am currently getting this error message:

find_real_file.png

 

Any ideas or suggestions?

 

1 ACCEPTED SOLUTION

Matt Cordero1
Tera Guru

Oh man, oh man, I swear I do my best programming in the morning!! 😃

Here is the result, a button that pulls of the related Requested Items, based on the "requested by" variable, WITHIN the variables collection.  This will work for every service catalog item that has a "requested_by" variable (which is most of them) and DOES NOT require a change/update to each service catalog item.

find_real_file.png

Modal window of Related Request Items by Requested For user

find_real_file.png

Client Script code:

function onLoad() {
try{
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var varControl = "lookup." + form.getControl("requested_by").id;
if(varControl)
{
$(varControl).insert({
after: '<span><a onclick="showRelatedRequestItems()" id="icon-check-circle" class="btn btn-default sc-ref-spacer icon-tree-right sn-tooltip-basic" title="" data-original-title="Show related Request Items"></a></span>'
});
}
}
catch(e){
alert(e.message);
}
}

//onclick event to fire when the button is clicked
function showRelatedRequestItems() {
try {
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;

var displayValue = g_form.getDisplayBox("requested_by").value;
var title = 'Showing Requested Items for: ' + displayValue;
var query = 'request.requested_for=' + form.getValue("requested_by");
var gdw = new GlideModal('show_list');
gdw.setTitle(title);
gdw.setSize(750);
gdw.setPreference('table', 'sc_req_item_list');
gdw.setPreference('sysparm_query', query);
gdw.render();
} catch (e) {
jslog('error showing related list');
jslog(e);
}
}

View solution in original post

5 REPLIES 5

DirkRedeker
Mega Sage
Hi the $ sign looks like jQuery, which is no default on ServiceNow. That's why $ is undefined. BR Dirk

vkachineni
Kilo Sage
Kilo Sage

Do you have isolate script off on the client script?

find_real_file.png

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Isolate script is checked.  For the purposes of the script, should it be on or off?

Manish Vinayak1
Tera Guru

Hi Matt,

Have you considered using a UI Macro / Macro variable to show the button? It can be hidden / shown based on UI policies, you won't even need DOM manipulation using client scripts. You just need to create a UI Macro, and associate it with a Macro variable in your catalog item. That UI macro can have the HTML code, and the javascript / jQuery logic for the button.

 

But if you want to use your client script to add that button, as already suggested change Isolate Script value to False; and try changing "$" to "$j" or "jQuery", i.e.

$(varControl).insert 

to

$j(varControl).insert

or

jQuery(varControl).insert

 

"$" operator is used in jelly, that's why for jQuery you would have to use $j or jQuery as the operator.

 

Hope this helps!

Cheers,

Manish