Thursday, March 7, 2013

Fill in catalog item variables from a URL

Ever wanted to fill in variables for your Service Now catalog items from a URL. We had to build this to fix a business process problem and I thought I'd share it.

First we built a UI action on the originating table where we were passing values from this table into the catalog item form. The script would grab the values we wanted to pass into the catalog item.

var instanceURL = gs.getProperty("glide.servlet.uri");
//the sysparm_id below is the sys_id of the catalog item you want to order
var url = instanceURL + 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=299dbe0b0a0a3c4501c8951df102a2ae&manager_request=' + current.sys_id + '&manager_name=' + current.u_manager_name + '&start_date=' + current.u_start_date.getDisplayValue() + '&hr_comments=' + current.u_hr_comments;
action.setRedirectURL(url);
view raw gistfile1.js hosted with ❤ by GitHub

Next we had to put an onLoad script into the catalog item to handle capturing the values and passing them into the correct variables in the catalog item.

function onLoad() {
g_form.setValue('manager_request', gup('manager_request'));
g_form.setValue('manager', gup('manager_name'));
g_form.setValue('start_date', decodeURI(gup('start_date')));
g_form.setValue('hr_comments', decodeURI(gup('hr_comments')));
}
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ){
return "";
}
else{
return results[1];
}
}
view raw gistfile1.js hosted with ❤ by GitHub

1 comment:

  1. Thank you for sharing this article,it is will be helpful and very Unique..

    Keep updating...

    ServiceNow Training

    ReplyDelete