How do you link to a record producer and pass some of the parameters in the url?

mahudg2
Kilo Expert

On a front page I want to be able to have several links to one record producer and pass some of the data in the url so some of the fields can be pre-filled. I've done this with incidents before with the sysparm_query by adding sysparm_query=priority=1^incident_state=3 to the url

 

But I can't seem to get it to work with record producers. Keep in mind that I'm using custom variables.

 

Is this possible?

1 ACCEPTED SOLUTION

andrew_venables
ServiceNow Employee
ServiceNow Employee

Use an onLoad Catalog Client Script like the below:



function onLoad() {


  var cmdb_ci = getParmVal('sysparm_cmdb_ci');


  if (cmdb_ci) {


        g_form.setValue('cmdb_ci', cmdb_ci);


  }


}




function getParmVal(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 unescape(results[1]);


  }


}





and you can pass in like:



var params = 'sysparm_cmdb_ci=' + current.sys_id;


var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=c60ea0806f159500d9c316ff8d3ee400';


action.setRedirectURL(url + '&' + params);


View solution in original post

9 REPLIES 9

Thank you so much, this worked beautifully!


Thanks very much for this helpful code, Andrew. Just to say in the getParmVal() code, in the context I needed (a link to a pre-populated record producer from a CMS page), it was necessary to replace window.location.href with document.referrer, so




var results = regex.exec( window.location.href ); //old




var results = regex.exec( document.referrer );   //new




I prefer using the way proposed in: Parse URL Parameters in a Client Script - ServiceNow Guru



Is there a reason you are implementing your own parsing function?


Bob
Giga Contributor

For anyone trying to do what this question is asking, try what Julian is suggesting unless you want to practice your coding chops. mark.stanger this is the second time I have used your code to perform a function I was looking to do. Thank you very much for writing and explaining such good stuff!



julianhoch great call!


Hello Andrew and thanks for the script, it works very well, however if I am passing a parameter that is made of a paragraph with multiple lines (example Description) the copied values are transferred into the new form as one single line with no line jumps