jasonbutz.info

ServiceNow - Passing Data

ServiceNow

At work we came across an interesting issue when dealing with ServiceNow this week. We are working on implimenting Incident and needed to be able to create a change or a request from the incident. The new record needed to have its parent field set to reference the inicident. Some of the original code does this for requests by redirecting the user to the Service Catalog and setting a parameter in the URL that is meant to will in a field on the form. Unfortunatly that doesn’t work in our case. Our changes are generated by using a wizard, and our requests are done with a record producer. We figured out a way to work around the issue and pass the values to the wizard and record producer. I thought it was something people might like to see.

Here is what the Create Request UI action looks like:

// Update saves incidents before going to the catalog homepage
current.update();
// Build the URL
var url = "com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=abcdef0123456789&incident_sysid=";
url += current.sys_id;
// Redirect the user
action.setRedirectURL(url);

What I am doing here is setting the URL for the record producer. Then I am adding a custom parameter on the end, incident_sysid, and setting it to the incident’s sys_id. You can do the same thing for a wizard. Then on the first panel of the wizard, or on the record producer, you need to add a field to put the value into. I added the field and then used a UI Policy to hide the field. Then I created an onLoad Client Script that takes the parameter from the URL and fills in the form field. Here is the client script I am using with both the wizard and record producer.

function onLoad()
{
   var url = window.location.href;
   var match = url.match(/&incident_sysid=([a-zA-Z0-9]+)/);
   var sysid = match[1];
   g_form.setValue('parent', sysid);
}

The script runs a regular expression against the current URL to pull out the sys_id and set it to the value of the field. Keep in mind that if you are using a wizard you will need to update the record producer at the end to set the value on the new record. If you are using just a record producer you can name the field the same thing in both the producer and the dictionary and it will automatically copy over.