ServiceNow Code Snippets: Catalog Items and the Portal Widget Data Object
ServiceNow Code Snippets
If you’re a ServiceNow portal developer, chances are you’re fully aware of the portal widget ‘data’ object and all it has to offer. You may be surprised that portal widgets aren’t just for portal pages. In fact, portal widgets fit nicely into service catalog items.
Here’s a look at two new methods (along with the ServiceNow code for both) you can use to access server data in your portal-based service catalogs.
The first screenshot shows a catalog item macro variable type. The macro variable can hold a few different objects that can be represented on a catalog item, one of them being a Service Portal widget.
The widget is displayed in the second screenshot. Notice there’s a UI consisting of HTML (and Angular). Leveraging portal widgets is a great way to develop robust offerings that go beyond basic catalog design patterns. In this example, we have an input that allows the user to type anything they wish and display it on the very first form variable (Populated onClick from form buttons).
The user also has the ability to select from the ‘Populate w/User Data’ drop-down and populate this variable with the current user’s email, title, or username. Finally, the user can mouse over and select any role to populate the form variable with that role’s title.
- Input field and data population buttons that will populate item 3 (drop-down is ‘data’ object driven).
- Mouse-over menu click to populate item 3 (‘data’ object list).
- Populated through user text or ‘data’ object button items.
- Reference field populated onLoad through client script call to custom widget API through ‘data’ object.
- Reference field populated onLoad through traditional GlideAjax.
- Reference field populated onLoad directly from ‘data’ object, no client script. Direct from script included.
After the listed roles, we have three more catalog reference variables (sys_user table) that are pre-populated in three different ways with the current user. One variable is being populated by the same source that generated all the data above, while the other is being populated by a standard GlideAjax call.
Now, let’s look at other options for client scripting and server scripting.
Client Scripting (3 ways, same result)
Example #1: Populated from portal widget: using onLoad custom API call
Using the custom API generated from our portal widget, we can instantiate and call methods that are linked directly to the widget’s client script, which can be connected directly to the widget’s server-side scripting/’data’ object.
- onLoad we need to set the timeout to allow all the page assets to load.
- Functional instantiation. This function exists in the HTML of the widget and is available on page load.
- Associating our desired method and setting its value to our variable.
While this method is used to perform something mundane in this example, it can be used to initiate a complex data push or pull. Additionally, you can perform conditional DOM manipulations with this method. This example has been understated to illustrate the concept.
Example #2: Populated from GlideAjax (this is provided for comparison purposes only)
This is your standard GlideAjax call being shown for comparison. Please see ServiceNow’s documentation if you would like to learn more about GlideAjax. You can leverage this method to return one or multiple values through an asynchronous callback function. Please note: You must have an independent client-callable script include to accompany this.
Example #3: Populated from portal widget using: $scope.page.g_form
This code is used in the widget’s Client Controller only. No other code is necessary anywhere
- Set a familiar named object to the scope’s actual ‘g_form’ object.
- Consume the ‘data’ object directly from the script include called from the widget’s server scripting
- Alternatively, you could skip the script include and place all scripting here
- Using the script include offers obvious centralized global access
Example #1 and Example #3: Both are using a variation that calls the same data from the same script include. What you’re missing in this formula is the widget code. Here’s the script include:
Notice the function does not include a ‘return.’ The ‘data’ object is exposed when the widget loads.
Example #2: GlideAjax prototype (this is provided for comparison purposes only)
So as not to downplay GlideAjax, you can absolutely create and return JSON objects and reach out to non-callable functions.
Portal Widget (Examples #1 and #3 will use this)
Keep in mind that the ‘data’ object is exposed to the widget on page load through the server script of the widget (scope). The widget server script is the first portion of the widget to load. Essentially, an API can be developed through a widget that can be used for multiple catalog items by simply including it in the Macro.
Select widget code snippets
The widget HTML allows you to create the widget UI and it also allows you to create functions and methods to call Angular functions that are linked to the server scripting (data object).
In Example #1, we made a call to a function from a catalog onLoad client script
This script was calling a function from the widget’s HTML scripting
Notice that this function’s ‘userSysID’ method is returning an Angular object that is calling one of the widget’s client controller functions (c.userSysID()). This can be a more difficult way of performing the particular action since the ‘data’ object is already exposed. This is included to demonstrate a method to actuate widget functions from a catalog client script.
The widget’s client controller function is returning our ‘data’ object for Example #1
The widget’s client controller function is returning our ‘data’ object for Example #3
For Example #3, there is no catalog client script, this is all you need from the widget client controller
The ‘data’ object is being populated from the script include through the widget’s server script. This last step exposes all the data within any script include function. This applies to both Example #1 and #3.
Full widget code: HTML
Full widget code: CSS
Create the code below
Full widget code: Client Controller
Full widget code: Server Script
→Pro Tip: If you’ve ever found yourself needing to perform a particular g_form action 20 or more times in a single script, you could easily create a function call and pass a comma-delimited string parameter of variable names to the widget client script. At the client script, you can iterate through the list and automatically generate virtual g_form calls for each catalog variable you listed. Here’s an example of a script that is performing 3 g_form functions on as many catalog variable names placed in the ‘list’ parameter. In the catalog, this would be a three line call for about five variables instead of 15 lines:
Bottom line: The widget technique doesn’t replace GlideAjax. Outside of the portal, this method has no merit. If you have a requirement to develop catalog offerings that will be viewed from the native UI, GlideAjax is what you will need to use. If you have a requirement to develop catalog offerings that will only be viewed from the Service Portal, this method is an option.
Interested in learning more about how to simplify access to your Service Catalog data in ServiceNow? Please contact us at [email protected].
*Author’s note: Code provided in this article was written in a manner to provide configuration options and do not always reflect my personal choice of techniques. For example, in the ‘_getRoles’ function, I purposely provided an extended code version to demonstrate how extensive you can be when populating the data object.
5 More ServiceNow Tricks and Hidden Features for Developers and Advanced System Administrators (Part 2)
You can use client scripts and catalog client scripts in the Service Portal if the UI Type is set to Mobile / Service Portal or All. Client scripts and catalog client scripts are used with the Form widget and SC Catalog Item widget, as opposed to a widget client controller.
Before flagging a script as Mobile/Service Portal or All, make sure that you are only using the mobile APIs. Setting a client script to Mobile does not ensure that it will work, it simply flags that the script should be attempted by the mobile app or the Service Portal. Many of your existing client scripts can be set to All as long as the API calls are supported by the mobile client scripting environment.
The topics in this section require advanced coding knowledge and an understanding of Service Portal APIs.
Checking desktop vs mobile runtime
You might want to mark a client script compatible with both desktop and mobile, but include behavior that depends on the runtime. You can use this script:
Unsupported client scripting globals
Widget client controllers are full Angular controllers and are not subject to the unsupported client script globals listed here. Use jQuery and Angular as needed.
The following globals and APIs are unavailable in client scripts and catalog client scripts used in the Service Portal:
Embedded widgets & g_form
When using the Service Catalog variable type Macro and Macro with Label, you can pick a widget to embed in a catalog item form. Within the client controller for the embedded widget you can access the field object and catalog item g_form instance using:
Example: g_list and g_service_catalog in client scripts used with Service Portal
The g_list global helps you set the filter of a Glide list element or a list collector variable. Use this API in place of the g_filter API on desktop client scripts.
g_service_catalog is only available in Service Portal service catalog item scripts. Use this API to know if your catalog item script is run as part of an order guide or on its own.
Service Portal & Client Scripts
Service Portal runs client scripts & catalog client scripts as long as the UI Type is set to "Mobile" or "Both". Many of your existing client scripts can be set to "Both" as long as the api calls are supported by the mobile client scripting environment.
Please note: This document applies to the Form widget and SC Catalog Item widget. Client Scripts are different than a widget's client controller & link script blocks.
Unsupported client scripting globals
The following globals and APIs are unavailable in service portal client script:
Client scripting in widgets
Widget client controllers are full angular controllers and are not subject to the restrictions listed above. Feel free to use jQuery and Angular as needed.
Embedded widgets & g_form
When using the Service Catalog variable type "Macro" and "Macro with Label" you can pick a widget to embed in a catalog item form. Within that embedded widget's client controller you can access the field object and catalog item g_form instance using:
Checking desktop vs mobile runtime
You might want to mark a client script compatible with both Desktop and Mobile but still do something different depending on the runtime use this:
Supported client scripting APIs
These are the officially supported client scripting APIs you can use in onLoad, onChange, and onSubmit client scripts.
- addDecoration(fieldName, icon, title)
- addOption(fieldName, value, label, index)
- getReference(fieldName, callback)
- hideAllFieldMsgs(type: "info | error")
- hideFieldMsg(fieldName, clearAll)
- removeDecoration(fieldName, icon, title)
- removeOption(fieldName, value)
- setFieldPlaceholder(fieldName, placeholder)
- setLabel(fieldName, label)
- setMandatory(fieldName, isMandatory)
- setReadOnly(fieldName, isReadOnly)
- setSectionDisplay(sectionName, isVisible)
- setValue(fieldName, value, displayValue)
- setVisible(fieldName, isVisible)
- showErrorBox(fieldName, message, scrollForm)
- showFieldMsg(fieldName, message, type: "info | error", scrollForm)
- addItem(value, displayValue)
- new GlideAjax(scriptIncludeName)
- addParam (name, value)
- getParam (name)
- successCalback(data, status, xhr)
- new GlideRecord(tableName)
- addQuery(fieldName, operator, value)
- setValue(fieldName, value)
g_list helps you set the filter of a glide list element or a list collector variable. Use this api in place of the "g_filter" api on desktop client scripts.
g_service_catalog is only available in Service Portal service catalog item scripts. Use this API to know if your catalog item script is being run as part of an order guide or on its own.
The HTML input ID of a table field is set to the logical name of the attribute. This makes selecting a field, setting values, or other client-side manipulation easy by with jQuery.
Sometimes you might need to customize the validation of fields on the form. The following example demonstrates adding a custom validator. This example forces the user to specify an email only if the other field for preferred method of contact is set to Email.
The client-side field validation is not supported in a subgrid.
On click of the Next/Submit button, a function named webFormClientValidate is executed. You can extend this method to add custom validation logic.
Just fantastic. - Maybe, will you stay with us. Tomorrow is Sunday.Demo: Getting started with Power Apps Portals
Oh, you shameless puss. You wrote to me in the ro. she did not let him finish, she grabbed her curls again and pressed her mouth to her pussy.
- Rebecca zamolo show
- Blackcurrant amazon
- Minecraft lagging
- Strong in urdu
- Hobo purses amazon
- Maria mora facebook
- Cartoon owls
- Heartless tattoo
- Cross svg
- Monoma mha
She sometimes asked her parents for permission to try her cooking efforts. What she cooked also made her children happy. The other guys called her the Mother of Millions.