Welcome!

Cloud Sherpas: Your expert guide to Google's cloud

Cloud Sherpas

Subscribe to Cloud Sherpas: eMailAlertsEmail Alerts
Get Cloud Sherpas via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Cloud Computing, Macintosh Magazine, Dell News on Ulitzer, Java in the Cloud

Blog Feed Post

ServiceNow Tech Tuesdays: Form Printing With Multiple Views

Recently, I have received several questions about the ability to print a form with a different view than what is displayed on the browser when the form is printed. For example, one customer wanted the printed view of purchase order forms to differ depending on which vendor is selected. It turns out that printing multiple views of forms is quite easy to do in ServiceNow. In fact, there are two possible options available, both of which I will outline below.

1. Set a printURL parameter

ServiceNow looks for an element with id = ‘printURL’ when the print icon is clicked. If found, the value on the element is assumed to be a URL that is then used to produce the form that will be printed.

2. Observe the “print” event

When the print icon is clicked, the first thing ServiceNow does is check to see if someone is observing the “print” event. If someone is and the function called as a result of the observation returns false, then only that function will be used to handle the print. If the function returns true, then the normal print processing is resumed.

This means that the function called can do one of two things:

1. Handle opening the print form all on its own and then return false.

2. Add the “printURL” element to the form and return true. This will force the normal print process to then use the URL value on the “printURL” element when opening the print window.

Steps to implement

1. Decide which option will work.

Option 1 is the simplest, but Option 2 provides more flexibility, as it allows for additional custom processing before the form is printed. For example, Option 2 makes it possible to have the browser print dialog box appear immediately rather than having the ‘printer friendly’ page open in a new browser window where the user must then click the ‘Print Page’ button.

2. Create an onLoad client script which will set up the custom print functionality.

The contents of the client script will be different depending on which option you decide to use.

Option 1 – Set a printURL parameter

Have the client script add a new hidden element with id set to “printURL” to the current form. The value of the element should be set to the URL that specifies the correct view to use when the form is printed.

The example client script below, which was applied to a purchase order form, checks to see if the vendor field is Apple or Dell and sets a different view for each. If the vendor is neither Apple nor Dell, a standard po print view is used.

function onLoad() {

/*
* ServiceNow looks for an element with id = 'printURL' when the print icon is clicked.
* If found, this url will be used to produce the form that will be printed.
*/

// The view name that we want to use on a print
var viewName = "po_print";
if (g_form.getDisplayBox('vendor').value == 'Apple Computer') {
viewName = "po_print_apple";
} else if (g_form.getDisplayBox('vendor').value == 'Dell Inc.') {
viewName = "po_print_dell";
}

// The url for printing the form
var printURL = g_form.getTableName() + ".do?sys_id=" +
g_form.getUniqueValue() + "&sysparm_view=" + viewName;

// Uncomment this line if you want it printed in landscape.
// Only seems to be effective in Chrome
//printURL += "&landscape=true";

// If you want an immediate PDF download then uncomment this line
// ** Loses the view ???
// href += "&PDF";

// The attributes we need to place on the printURL element
var props = {value: printURL, name: 'printURL', type: 'HIDDEN', id: "printURL"};

// Create the printURL element
var printURLElement = new Element("input", props);

// Add our new printURL element after the form's sys_id element
var anchor = $("sys_uniqueValue");
anchor.insert({ after : printURLElement});
}

Option 2 – Observe the “print” event

This example client script, also used on the purchase order form, is a bit long-winded but provides greater functionality than the script in the first example. In addition to checking the vendor to set the correct view, this script also includes the option to automatically bring up the browser print dialog. It does this by embedding a hidden frame in the current form, loading it with the printer-friendly form using the appropriate view and then triggering the browser print on the hidden frame.

function onLoad() {

/*
* ServiceNow will check to see if anyone is observing the "print" event. If
* they are and the function called as a result of the observation returns
* false then only that function will be used to handle the print.
*
* This means that the "print" function can do one of two things:
* 1) Handle opening the print form all on its own and then return false
* 2) Simply add the "printURL" element to the form and return true. This will force
* the normal print process to then use the url value on the "printURL"
* element when opening the print window
*/

// Set the form to observe the "print" event
var mainWin = getMainWindow();
if (mainWin) {
mainWin.CustomEvent.observe("print", printReport);
}
}

function printReport() {

// Determine which view we want to use - either Dell, Apple or standard
var viewName = "po_print";
if (g_form.getDisplayBox('vendor').value == 'Apple Computer') {
viewName = "po_print_apple";
} else if (g_form.getDisplayBox('vendor').value == 'Dell Inc.') {
viewName = "po_print_dell";
}

// The url for printing the form
var frameSrc = g_form.getTableName() + ".do?sys_id=" + g_form.getUniqueValue() + "&sysparm_view=" + viewName;
frameSrc = printListURLDecode(frameSrc);

if (frameSrc.indexOf("?") < 0) {
frameSrc += "?";
} else {
frameSrc += "&";
}

// we're doing a print; we want the whole report
frameSrc = frameSrc.replace("partial_page=", "syshint_unimportant=");
frameSrc = frameSrc.replace("sysparm_media=", "syshint_unimportant=");
frameSrc += "sysparm_stack=no&sysparm_media=print";

// Uncomment this line if you want it printed in landscape.
// Only seems to be effective in Chrome
frameSrc += "&landscape=true";

// If you want an immediate PDF download then uncomment this line
// ** Loses the view
// frameSrc += "&PDF";

var autoPrint = false;

// If you want to automatically bring up the print dialog uncomment this
// next line. Only tested on a mac using Chrome and FireFox.
autoPrint = true;

if (frameSrc != null && frameSrc != "") {
if (autoPrint) {
// create a hidden frame.
var props = {
src : frameSrc,
name : 'print_frame',
id : "print_frame"
};

// Create the iFrame element and hide it
var printFrame = new Element("iframe", props);
printFrame.hide();
// add the hidden to this form
var anchor = $("sys_uniqueValue");
anchor.insert({
after : printFrame
});

// Reset the frame source
printFrame.setAttribute("src", frameSrc);
// Wait for the frame to finish loading
var readyStateCheckInterval = setInterval(function() {
var frameDoc = printFrame.contentWindow.document;
if (frameDoc.readyState === "complete" || frameDoc.readyState === "loaded") {
printFrame.contentWindow.print();
clearInterval(readyStateCheckInterval);
}
}, 10);
} else {
var features = "resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=yes,location=no";
win = window.open(frameSrc, "Printer_friendly_format", features);
win.focus();
}
} else {
alert("Nothing to print");
}
// Return false to ensure that this is the end of the print processing
return false;
}

Results

The screenshots below show the results of clicking the print icon for two different purchase orders. The first purchase has Apple selected as the vendor and the second has Dell selected. The view used on the printer-friendly form is based on the vendor.

Warning

The functionality in both of the options described above makes assumptions about the way ServiceNow prints forms. As such, it may break should ServiceNow change how forms are printed in future releases.

The post ServiceNow Tech Tuesdays: Form Printing With Multiple Views appeared first on Cloud Sherpas.

Read the original blog entry...

More Stories By Cloud Sherpas

Cloud Sherpas [www.cloudsherpas.com] is a leading Google Apps Reseller, systems integrator and application developer. Our Google Apps Certified Deployment Specialists have migrated tens of thousands of users from legacy, on-premise messaging systems to Google Apps and Google App Engine. We help organizations adopt cloud computing to innovate and dramatically reduce their IT expenses. SherpaTools for Google Apps [www.sherpatools.com] is a free app from Cloud Sherpas that enhances the functionality and ease-of-use of Google Apps for both administrators and end-users.