Service Portal - Requested For
Have you ever been frustrated by the two-step order process having a field called requested_for that you can't set from your catalog item? Sometimes you want the person an item is requested for on your form simply so you can set other options like the appropriate approver, here's how you can do this WITHOUT modifying any portal widgets.
ServiceNow documents an option for the service portal widget that allow you to set a custom Request for user, but somewhat surprisingly, it only allows for a hard coded value (which doesn't make sense to me).
Here's a screenshot from the documentation.
But this gives us the opportunity to dynamically set the options for the widget whenever a particular field changes. As an example...
The code is a little complex as it involves a bit of angular scope magic, but I've commented it so you should be able to follow the logic. If in doubt, write the various scope objects out to the console log so you can see what they contain.
- Your catalog item, record producer or order guide should have a reference field requested_for that looks at the sys_user table
- You'll need to add this as an onchange catalog client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//ServiceNow allows for the requester to be set in the options of the SC Catalog Item widget, but it's a static value
//We'll take advantage of this and transform it into a dynamic value using the angular scope object
//https://docs.servicenow.com/bundle/orlando-it-service-management/page/product/service-catalog-management/task/request-cat-item-portal.html
try{
//First, get the angular scope for the catalog item widget as that has both options and variable values stored in it
//If sc_cat_item isn't there, we're probably within an order guide so we'll grab that scope instead
var catItemScope = this.angular.element('#sc_cat_item').scope();
var orderGuideScope = this.angular.element('div[sn-atf-blacklist="IS_SERVICE_CATALOG"]').scope();
//Find the parameters for this depending on whether it's an order guide or a catalog item
var widgetParameters = catItemScope === undefined ? orderGuideScope : catItemScope.$parent;
//newValue is the sys_id for the requestor selected by our user
widgetParameters.options.requested_for_id = newValue;
//ServiceNow doesn't have an easy way of getting display values beyond using Glide Ajax or a g_form.getReference
//but rather than doing an ajax call, we'll simply grab the name of this user from the scope as it's already there
var displayValue = widgetParameters.data.sc_cat_item._fields.requested_for.displayValue;
//Now we set options display value and we're done
widgetParameters.options.requested_for_display = displayValue;
}catch(e){
//If we run into any problems, we'll print the scope to the console
console.log('%cOh no, an error occured in the client script Update Requested For','color:red');
console.log(e);
console.log(catItemScope);
console.log(orderGuideScope);
}
}
I've adapted this script so it works with catalog items and order guides (but the reference field needs to be on the order guide not buried away in one of the various items in the guide)
Also, I've attached the catalog script to this article.
Have fun
https://www.servicenow.com/community/now-platform-articles/service-portal-requested-for/ta-p/2320820