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

Colleen1
Tera Contributor

I entered the URL and updated to my instance.  Also, updated the code you provided to make sure it was using "getParam" and not "getParameterValue".  So after that and updating one of the '&' in the URL, it worked!!!!  Thank you so very much!

Nice work!

TJ29
Tera Contributor

Hi Colleen - sorry to bring up an old post! Do you have the final iteration of this solution to hand please? We've got a similar requirement where product teams would like to have a catalog item prepopulate them as the assignment group.

 

Cheers

Colleen1
Tera Contributor

Michael,

 

You greatly helped with the populating of the variables from a URL but for some reason the new expiration date value is not storing what is passed in but the other two fields are.  It is defaulting to today's date?  From what I have read online, the date fields in ServiceNow can be quite picky.  Below is my dilemma:

Here is the URL that I am testing with and is currently pulling the values onto the form (screenshot below).

https://<my instance>.service-now.com/sp?id=sc_cat_item&sys_id=d0431cdddbe22e004db470d9af9619ef&sysp...

find_real_file.png

I do not touch any of the fields they are what I want so I click submit.  When I do, here is the RITM record:

find_real_file.png

Here is my code from the Catalog Client Script that is running.  It populates all 3 variables but the date is not sticking?  BUT if I update the pre-populated date with a new date value, it stores it then??

function onLoad() {
var User = getParameterValue('sysparm_requestedfor');

var usr = new GlideAjax('FindRequestedFor');
usr.addParam('sysparm_name', 'getRequestedFor');
usr.addParam('sysparm_user', User);
usr.getXML(getUserParse);

function getUserParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(User != undefined || User != '') g_form.setValue('requested_for', answer);
}

var Date = getParameterValue('sysparm_expdte');
var comments = getParameterValue('sysparm_comments');

if(Date != undefined || Date != '') g_form.setValue('new_expiration_date', Date);
if(comments != undefined || comments != '') g_form.setValue('sc_all_items_notes', comments);
}

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]);
}
}

 

Any assistance would be greatly appreciated.  This last thing is preventing me from migrating my code to Production.

laxman godishel
Tera Contributor

auto populate URL of selected RITM in one more catalog item