Portal onload

Portal onload DEFAULT

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.

Screenshot #1

Screenshot 1

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.

Screenshot #2

Screenshot #2

  1. Input field and data population buttons that will populate item 3 (drop-down is ‘data’ object driven).
  2. Mouse-over menu click to populate item 3 (‘data’ object list).
  3. Populated through user text or ‘data’ object button items.
  4. Reference field populated onLoad through client script call to custom widget API through ‘data’ object.
  5. Reference field populated onLoad through traditional GlideAjax.
  6. 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.

Screenshot 3

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.

Scripting Example A

  1. onLoad we need to set the timeout to allow all the page assets to load.
  2. Functional instantiation. This function exists in the HTML of the widget and is available on page load.
  3. Associating our desired method and setting its value to our variable.
  4. Use the JavaScript variable as the form variables value.

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)

Scripting Example B

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

  1. Set a familiar named object to the scope’s actual ‘g_form’ object.
  2. Consume the ‘data’ object directly from the script include called from the widget’s server scripting
    1. Alternatively, you could skip the script include and place all scripting here
    2. Using the script include offers obvious centralized global access

Server Scripting

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:

Conclusion

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.

It is important to understand that this method is simply using standard JavaScript, Angular, and Glide scripting to maximize ServiceNow’s platform capabilities.

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)

Sours: https://www.covestic.com/blog/servicenow-code-snippets-catalog-items-and-the-portal-widget-data-object/

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:

if (window === null) // Write your mobile compatible code here else // Write your desktop compatible code here

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:

  • $
  • $$
  • $j
  • angular
  • control
  • document
  • jQuery
  • window

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.

    function onLoad() { var myListCollector = g_list.get("my_list_collector"); myListCollector.reset(); myListCollector.setQuery("active=true^category=8c7b22230b402200b0b02c6317673a62"); myListCollector.addItem('3a700d39af5f4fc0aab978df90f4c692', 'Power Supply'); myListCollector.addItem('1cb93419a3a248318da8f814140b42f6', 'Backpack'); }

    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.

    function onLoad() { if (window) // if CMS, don't run this return; // g_service_catalog api for Service Portal and Mobile var isOrderGuide = g_service_catalog.isOrderGuide(); g_form.setValue("is_order_guide", isOrderGuide ? "Yes!" : "Nope :("); }
    Sours: https://docs.qualityclouds.com/qcd/service-portal-and-client-scripts-31721296.html
    1. The bronx collab
    2. Shapes in tagalog
    3. Orajel toothache

    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:

    • window
    • document
    • $
    • jQuery
    • $$
    • $j
    • angular

    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:

    • $scope.page.field
    • $scope.page.g_form()

    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.

    g_form

    • addDecoration(fieldName, icon, title)
    • addErrorMessage(message)
    • addInfoMessage(message)
    • addOption(fieldName, value, label, index)
    • clearMessages()
    • clearOptions(fieldName)
    • clearValue(fieldName)
    • getActionName()
    • getBooleanValue(fieldName)
    • getDecimalValue(fieldName)
    • getDisplayValue(fieldName)
    • getEncodedRecord()
    • getFieldNames()
    • getIntValue(fieldName)
    • getLabel(fieldName)
    • getReference(fieldName, callback)
    • getRelatedListNames()
    • getSectionNames()
    • getSysId()
    • getTableName()
    • getValue(fieldName)
    • hasField(fieldName)
    • hideAllFieldMsgs(type: "info | error")
    • hideErrorBox(fieldName)
    • hideFieldMsg(fieldName, clearAll)
    • hideRelatedList(listTableName)
    • hideRelatedLists()
    • isMandatory(fieldName)
    • isNewRecord()
    • isReadOnly(fieldName)
    • isVisible(fieldName)
    • removeDecoration(fieldName, icon, title)
    • removeOption(fieldName, value)
    • save()
    • serialize(onlyDirtyFields)
    • 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)
    • showRelatedList(relatedTableName)
    • showRelatedLists()
    • submit(submitActionName)

    g_list

    • get(fieldName)
      • addItem(value, displayValue)
      • removeItem(value)
      • reset()
      • setQuery(queryString)
      • setDefaultOperator(operator)
      • getDefaultOperator()

    g_service_catalog

    GlideAjax

    • new GlideAjax(scriptIncludeName)
      • addParam (name, value)
      • getParam (name)
      • getXML(callback)
      • getXMLAnswer(callback)
      • getJSON(callback)
      • setErrorCallback(errorCallback)
      • getURL()
      • getParams()
      • execute()
      • successCalback(data, status, xhr)
      • errorCallback(xhr)
      • setScope(scope)

    GlideRecord

    • new GlideRecord(tableName)
      • addQuery(encodedQuery)
      • addQuery(fieldName, operator, value)
      • getEncodedQuery()
      • deleteRecord(callback)
      • get(id)
      • getTableName()
      • hasNext()
      • insert(callback)
      • gotoTop()
      • next()
      • loadRow(rowObj)
      • getValue(fieldName)
      • setValue(fieldName, value)
      • isDotWalkField(fieldName)
      • addOrderBy(fieldName)
      • orderBy(fieldName)
      • orderByDesc(fieldName)
      • setDisplayFields(fieldNames)
      • query(callback)
      • setRows(rowsArray)
      • setTableName(tableName)
      • update(callback)
      • setLimit(maxInt)
      • getLimit()

    getMessage(messageKey, callback)

    g_list

    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

    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.

    Sours: https://serviceportal.io/docs/documentation/client_scripting.md
    Web File in Portal - How to use JavaScript Web Files \u0026 Odata Query - Dynamics 365 - Power App Portal

    Add custom JavaScript

    The Advanced Form Step record contains a field named Custom JavaScript that can be used to store JavaScript code to allow you to extend or modify the form's visual display or function.

    The custom block of JavaScript will be added to the bottom of the page just before the closing form tag element.

    Form fields

    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.

    Note

    The client-side field validation is not supported in a subgrid.

    General validation

    On click of the Next/Submit button, a function named webFormClientValidate is executed. You can extend this method to add custom validation logic.

    See also

    Sours: https://docs.microsoft.com/en-us/powerapps/maker/portals/configure/add-custom-javascript

    Onload portal

    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.

    Now discussing:

    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.



    1349 1350 1351 1352 1353