how to buid a URL for prefilling a field value for one record producer?

Raju Singh1
Tera Expert

how to buid a URL for prefilling a field value for one record producer.

I have one variable called 'user name' in record producer.

I have a URL for that record producer. As per my requirement whenever a new user is created in the system, it sends a notification which contains a URL to that record producer. Now when I click on this URL it should prepopulate a variable called user name with the a new user created.

Could you please help me achieving this requirement.

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Well, you need to put in a parameter in the url to have the sys_id of the user.. like this, here I added sysparm_caller



https://devXXXXX.service-now.com/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=66c313e...



Then you need a onLoad Catalog client script that puts that info into the variable.. in this case I'm putting it into a variable called "caller_id"



find_real_file.png


View solution in original post

11 REPLIES 11

Can you please make the newHire field as reference field to sys_user table and check.


New Hire is already a reference field and it works fine.. but I need to remove that..



My client want instead new_hire which I have created a new field to populate automatically with the username whenever a new user is created.



I am able to populate that but I am getting a sys_id (a 32 bit character) in that place. I am struggling to convert it into a user_name field.


Can you try with GlideAjax when you get the sys_id then call the script include (glideajax) to get the user name and then save that value to field.  



see if this helps.



client script:


var userDetails = new GlideAjax("getUserData");


userDetails.addParam("sysparm_name", "getUserDetails");


userDetails.addParam("sysparm_sys_id", getParmVal('sysparm_user'));


userDetails.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


var answer = serverResponse.responseXML.documentElement.getAttribute("answer");


alert(answer);


g_form.setValue('new_hire', answer);


}



Script Include:


var getUserData = Class.create();


getUserData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


getUserDetails: function() {


var gr = new GlideRecord('sys_user');


gr.get('sys_id', this.getParameter('sysparm_sys_id'));


return gr.getValue('name');


},


type: 'getUserData'


});


thanks Shishir,



I was able to achieve this requirement with getreference and the script is as follows:



function onLoad() {  

var newHire = getParmVal('sysparm_user');
alert(newHire);
g_form.setValue('u_name',newHire);  
//var obj=gs.getUser(newHire);
//alert(obj);
// g_form.setValue('u_name',obj.user_name);  

var gr=new GlideRecord('sys_user');
gr.addQuery('u_name', gr.getDisplayValue());
gr.query();



getValu(newHire);
}  


function getParmVal(name){
      var url = document.URL.parseQuery();
      if(url[name]){
              return decodeURI(url[name]);
      }
      else{
              return;
      }
}



/*function getValu(newHire){
var y = new GlideRecord(newHire);  
      y.addQuery('sys_id',searchSys_id);  
      y.addActiveQuery();  
      y.query();  
      while(y.next())  
      {  
              if(y.sys_id == newHire ) // NEED this double check or else log will spit out alot lot of rubbish    
              {  
                    alert("we r good   "+ y.user_name);
              }  
      }
     
}*/
     
function getParmVal(name) {  
alert(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]);  


}
alert(results);

    }  




Now,, I want to populate the 2nd value called as location.. based on the username displaying on the form.   Could you please help me how to achieve this as I am not able to populate it.. I am trying this OnChange Scirpt



function onChange(control, oldValue, newValue, isLoading) {
//   if (isLoading || newValue == '') {
//         return;
  // }


    //Type appropriate comment here, and begin script below
/*alert("abc");
userobject = g_form.getReference('u_name');
g_form.setValue('u_test_location','userobject.u_locations');         //Here u_test_locaitons is the field on the record producer . and u_locations is a field on Sys_user table
alert('userobject.u_locations');*/

   
}


GlideRecord query is not recommended in client script, please check if this helps to get the location details.



Client Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}


var gajax = new GlideAjax('LocationAjax');


gajax.addParam('sysparm_name', 'ajaxFunction_LocationAjax');


gajax.addParam('sysparm_user', newValue);


gajax.getXML(callBackLocation);


function callBackLocation(response) {


var answer = response.responseXML.documentElement.getAttribute('answer');


alert(answer);


g_form.setValue('location', answer);


}


}



Script Include:


var LocationAjax = Class.create();


LocationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


ajaxFunction_LocationAjax : function() {


var ag = new GlideRecord('sys_user');


if(ag.get(this.getParameter('sysparm_user')))


return ag.location;


},


type : 'LocationAjax'


});