Prepopulate fields on a service catalog item using a URL

Colleen1
Tera Contributor

I am new to ServiceNow but I have been asked to prepopulate a catalog item's variables from a URL that is passed to the end user in an email.  From my searches on the ServiceNow community, I came across two helpful articles and followed their guidance: 

3 Ways to Populate Values via URL  and   Populating Values through Service Portal

But for some reason, I cannot get my variables to appear on the form.  I am trying to pass only 3 variables - requested for, expiration date, and comments.  Below is my client script, set with 'onLoad', and the URL I am using.    The image is a screenshot of my catalog item I am trying to populate.  Any assistance here would be greatly appreciated.

https://<instance>.service-now.com/nav_to.do?uri=com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=d0431cdddbe22e004db470d9af9619ef&sysparm_requestedfor=e7dcc734db612300177bab8b4b961961&sysparm_expdte=03202019&sysparm_comments=TEST

function onLoad() {
     //Use the 'getParameterValue' function below to get the parameter values from the URL   

    var requested_for = getParameterValue('sysparm_requestedfor');
    var requestedfor  = g_form.getReference('requested_for').requested_for;
    var comments = decodeURI(getParameterValue('sysparm_comments'));
    var expdate = decodeURI(getParameterValue('sysparm_expdte'));
       if (requested_for) {
         g_form.setValue('requested_for', requestedfor);
       }
   if (comments) {
               g_form.setValue('sc_all_items_notes', comments);
       }
   if (expdate) {
               g_form.setValue('new_expiration_date', new GlideDateTime(expdate));//date_variable
       }
}

function getParameterValue(name) {

       name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
       var regexS = "[\\?&]" + name + "=([^&#]*)";
       var regex = new RegExp(regexS);
       var results = regex.exec(top.location);
       if (results == null) {
               return "";
       } else {
               return unescape(results[1]);
       }
}

find_real_file.png

 

1 ACCEPTED SOLUTION

I don't think getParameterValue works anymore. We tried using that long ago and had to switch to GlideURL.

What happens if you paste this url into your browser and click enter: https://<instance>.service-now.com/nav_to.do?uri=com.glideapp.servicecatalog_cat_item_view.do?syspar...

a) does the catalog item open? if yes, we know sysparm_id is working

b) if the catalog item opens, copy the URL from your browser. Does it match what you entered or is it truncated? If it matches, the URL is working, just need to fix the client script.

View solution in original post

14 REPLIES 14

Michael Fry1
Kilo Patron

Suggestion in one of the article: I found that there is a checkbox in London on Catalog Client Scripts called Isolate Script which is enabled by default. My OnLoad Catalog Client Script would not trigger until I turned that off. -  did you try this?

Colleen1
Tera Contributor

Good catch.  I was not aware of this and set the Isolate Script to false.  But this still isn't working. I have tried my catalog client script applied to 'a variable set' with the identified variable set and then with applies to "A Catalog item' with the catalog item from the drop down - both are not working.  I even added an alert to make sure this was script was running and I am at least getting that.   Not sure why this isn't working.  Here is my catalog client script but I think it is getting closer.  I am not sure why this isn't populating the variables.  Any assistance would be greatly appreciated.

 

find_real_file.png

What happens if you use something like this:

var gUrl = new GlideURL();
gUrl.setFromCurrent();
var user = decodeURI(gUrl.getParam("sysparm_requestedfor"));
if (user!='undefined')
g_form.setValue('requested_for', user);

Colleen1
Tera Contributor

Michael,

Thank you for your suggestion and quick turnaround.  I tried your code and still not working.  It has to be close but something just isn't working.  Are there other setup details I might be missing??  The Catalog Script?  The Catalog Item?  I have a specific variable set for this.  I have tried the code applying to the catalog item and then to the variable set but still same result.  Any other suggestions?