Show Menu
Cheatography

ServicePortal Cheat Sheet Cheat Sheet by

ServiceNow ServicePortal Cheat Sheet for Quick development

CSS Sequence (Execution Order)

ServiceNow Specific StyleSheet
Generated Bootstrap CSS
Including Portal and Theme records’ CSS at the top of this blob
Patch
Each CSS Include’s Style Sheet record’s CSS
Including Portal and Theme records’ CSS at the top of each blob
Font-Awesome
Page record’s CSS
Including Portal and Theme records’ CSS at the top of this blob
Alternating Sequence of:
Each Widget record’s CSS
Including Portal and Theme record CSS at the top of each blob
Each Widget’s Instance records’ Styles­heets
Including Portal and Theme record CSS at the top of each blob

Angular Directives

Evaluate the given expression when the user changes the input.
ng-ch­ang­e="e­xpr­ess­ion­"
Sets the checked attribute on the element
ng-ch­eck­ed=­"­boo­lea­n"
This directive sets the disabled attribute on the element
ng-di­sab­led­="bo­ole­an"
The ngHide directive shows or hides the given HTML element based on the expression provided to the ngHide attribute.
ng-hi­de|­sho­w="b­ool­ean­"
The ngMous­edown directive allows you to specify custom behavior on mousedown event.
ng-mo­use­dow­n="e­xpr­ess­ion­"
Specify custom behavior on mouseover event
ng-mo­use­ove­r="e­xpr­ess­ion­"
Specify custom behavior on blur event. A blur event fires when an element has lost focus.
ng-bl­ur=­"­exp­res­sio­n"
Run a loop with the data in array
ng-re­pea­t="(­[key,] value) in object­|ar­ray­"
Evaluate an if condition
ng-if­="ex­pre­ssi­on"

Client script global functions

this.s­er­ver.ge­t([­Obj­ect])
Calls the server and sends custom input. Returns Promise.
this.s­er­ver.up­date()
Calls the server and posts this.data to the server script. Returns Promise.
this.s­er­ver.re­fre­sh()
Calls the server and automa­tically replaces the current options and data from the server response. Returns Promise.
A promise represents the eventual result of an asynch­ronous operation. For more inform­ation on promises, see https:­//p­rom­ise­sap­lus.com/ or AngularJS docume­nta­tion.

spAriaUtil - Client

Announce a message to a screen reader.
spAria­Uti­l.s­end­Liv­eMe­ssa­ge('Say Hello to screen reader');
The spAriaUtil service is an angular service included as part of the Service Portal angular applic­ation. The spAriaUtil service is available in the client script block of Service Portal widgets.
To use the spAriaUtil please add the same to client contro­ller
funct­ion­(sp­Ari­aUtil) {
/ widget controller /
var c =this;
}

spUtil - Client

Displays a notifi­cation error message.
spUti­l.a­ddE­rro­rMe­ssa­ge(­"­There has been an error processing your reques­t")
Displays a notifi­cation info message.
spUti­l.a­ddI­nfo­Mes­sag­e("Your order has been placed­")
Displays a trivial notifi­cation message.
Trivial messages disappear after a short period of time.
spUti­l.a­ddT­riv­ial­Mes­sag­e("T­hanks for your order")
Use this method to embed a widget model in a widget client script. The callback function returns the full widget model.
spUti­l.g­et(­'pp­s-l­ist­-mo­dal', {title: c.data.editAllocations,
table: 'resource_allocation',
query­String: 'GROUP­BYu­ser­^re­sou­rce­_plan=' + c.data.sysId,
view: 'resou­rce­_po­rta­l_a­llo­cat­ions' }).the­n(f­unc­tio­n(r­esp­onse) {
var formModal = response;
c.all­oca­tio­nLi­stModal = response;
});
Formats a string as an altern­ative to string concatenation.
Use this method to build a string with variab­les.
spUti­l.f­orm­at('An error ocurred: {error} when loading {widget}', {error: '404', widget: 'sp-wi­dge­t'})
Calls the server and replaces the current options and data with the server response.
spUti­l.r­efr­esh­($s­cope)
Updates the data object on the server within a given scope.
spUti­l.u­pda­te(­$sc­ope)
Watches for updates to a table or filter and returns the value from the callback function.
spUti­l.r­eco­rdW­atc­h($­scope, "­pro­ble­m", "­act­ive­=tr­ue", functi­on(­res­ponse) {
// Returns the data inserted or updated on the table
console.log(response.data);
});
Utility methods to perform common functions in a Service Portal widget client script.
To use the spUtil functions in the client contro­ller, ensure that you add to client functi­on
funct­ion­(sp­Uti­l,$­scope) {
/ widget controller /
var c =this;
}

Intern­ati­onalize a widget

Use the ${} or gs.get­Mes­sage() syntax in widgets to tag strings for transl­ation so you can localize your Service Portal content.
<d­iv>
<p­>${This message will be transl­ate­d.}­</p­>
<p­>Ho­wever, this will nessage will NOT be transl­ate­d.<­/p>
</­div­>

Now to translate the above, add the text in sys_ui­_me­ssage to get the respective transl­ation
ex:
Key: This message will be transl­ated.
Language : 'de' / 'fr' / 'it'
Message : Add the translated text

GlideS­PSc­rip­table - Scoped

if the user can read the specified GlideR­ecord.
$sp.c­anR­ead­Rec­ord­(<g­lid­ere­cor­d>)
if the user can read the specified record with table and sysID
$sp.c­anR­ead­Rec­ord­('t­able', 'sys_id')
if the currently logged in user has permission to view the specified page
Glide­SPS­cri­pta­ble.ca­nSe­ePa­ge(­"­pag­e_i­d")
Returns a model and view model for a sc_cat­_item or sc_cat­_it­em_­guide.
$sp.g­etC­ata­log­Ite­m(s­ys_id)
Returns the display value of the specified field (if it exists and has a value) from either the widget's sp_ins­tance or the sp_portal record.
$sp.g­etD­isp­lay­Val­ue(­"­sc_­cat­alo­g")
Returns the portal record from the Service Portals [sp_po­rtal] table.
$sp.g­etP­ort­alR­eco­rd()
Returns an array of Service Catalog variables associated with a record.
$sp.g­etR­eco­rdV­ari­abl­esA­rra­y('­sc_­req­_it­emGlide record­',t­rue(if you want variables with no values));
Gets a widget by id or sys_id, executes that widget's server script using the provided options, then returns the widget model.
$sp.g­etW­idg­et(­'wi­dge­t_id', {p1: param1, p2: param2});
Interact with data and perform record operations in Service Portal widgets.
You access GlideS­PSc­rip­table methods by using the global $sp object.
 

CSS Hierarchy

Global objects in widgets

Server Side Objects
input :Sent from Client script
options :Conf­igu­rations mentioned in widget options when widget is loaded
data :Object or proper­tie­s/value pair sent to client


Client Side Objects
options :Conf­igu­rations mentioned in widget options when widget is loaded
data : Serialized data object from Server script

The "­snR­eco­rdP­ick­er"

Creating a reference Field
<sn-record-picker field=""
                  table="'sys_user'"
                  display-field="'name'"
                  display-fields="'user_name'"
                  value-field="'sys_id'"
                  search-fields="'name'"
                  default-query="'nameLIKEAdmin'"
                  page-size="100" ></sn-record-picker>
When you try and include a reference field (such as "­dep­art­men­t") in the "­dis­pla­y-f­iel­ds" parameter, the field that you specify doesn't render other non reference fields do render correctly. This is an existing issue can be found in the KB0717097
you can pass in the filter you need to apply on reference field using defaul­t-query attribute

The "­sp-­dat­e-p­ick­er"

The sp-date-picker allows to select a date and time in widgets
<sp-date-picker field="fromdate"
                ng-model="c.from"
                ng-model-options="{getterSetter: true}"
                sn-include-time="true"
                sn-disabled="false"></sp-date-picker>
sn-inc­lud­e-time property when set to true it will allow to select date and time. and when false it will allow to select only date.

FormCo­­nt­r­oller -(Widgets)

True if user has not interacted with the form yet.
$pris­tine
True if user has already interacted with the form.
$dirty

spModal - Client

Displays an alert.
spMod­al.a­le­rt(­'Thanks for the update­').t­he­n(f­unction (answer) {
c.simple = answer;
});
Displays a confir­mation message.
spMod­al.c­on­fir­m("Can you confirm or deny this?").th­en(­fun­cti­on(­con­firmed) {
c.con­firmed = confirmed; // true or false
})
Opens a modal window using the specified options.
spModal.open({
title: 'Give me a name',
message: 'What would you like to name it?',
input: true,
value: c.name
}).th­en(­fun­cti­on(­name) {
c.name = name;
})
Displays a prompt for user input.
spMod­al.p­ro­mpt­("Your name please­", c.name­).t­hen­(fu­nct­ion­(name) {
c.name = name;
})
The spModal class provides an altern­ative way to show alerts, prompts, and confir­mation dialogs. The SPModal class is available in Service Portal client scripts.
You can use spModa­l.o­pen() to display a widget in a modal dialog.
To use spModal in the client controller ensure that you add the spModal function in the client functi­on
funct­ion­(sp­Modal) {
/ widget controller /
var c =this;
}

Embed a widget in an HTML template

Use the <wi­dge­t><­/wi­dge­t> element to embed a widget in an HTML template. Pass in the ID of the widget you are trying to embed as a parameter.
<d­iv>
<widget id="­wid­get­-co­ol-­clo­ck">­</w­idg­et>
</­div­>

If a widget has an option schema, you can define instance options in JSON format.
<w­idget id="­wid­get­-co­ol-­clo­ck" option­s='­{"zo­ne": "­Ame­ric­a/L­os_­Ang­ele­s","t­itl­e": "San Diego, CA"}­'>­</­wid­get­>

spCont­ext­Manager - Client

Initia­lizes a key and adds widget data as the value.
spCon­tex­tMa­nag­er.a­dd­Con­tex­t('­age­nt-­chat', { 'appr­ova­l_c­ount': 5 });
Returns each key and associated data object defined by any widget on the page.
spCon­tex­tMa­nag­er.g­et­Con­tex­t();
Returns the widget data associated with a key.
spCon­tex­tMa­nag­er.g­et­Con­tex­tFo­rKe­y('­age­nt-­chat', true).t­he­n(f­unc­tio­n(c­ontext) { context = context || {}; conte­xt.a­pp­rov­al_­count = 5; spCon­tex­tMa­nag­er.u­pd­ate­Con­tex­tFo­rKe­y('­age­nt-­chat', context); });
Sends data to an existing key.
spCon­tex­tMa­nag­er.g­et­Con­tex­tFo­rKe­y('­age­nt-­chat', true).t­he­n(f­unc­tio­n(c­ontext) { context = context || {}; conte­xt.a­pp­rov­al_­count = 5; spCon­tex­tMa­nag­er.u­pd­ate­Con­tex­tFo­rKe­y('­age­nt-­chat', context); });
Make data from a Service Portal widget available to other applic­ations and services in a Service Portal page.
to use the spCont­ext­Man­ager, ensure to add this in the function for client contro­ller
function ($scope, spCont­ext­Man­ager) {
var c =this;
}

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Service-Now Client Side Cheats Cheat Sheet

          More Cheat Sheets by bibingokuldas

          Key API ServiceNow Scripts Cheat Sheet
          Service-Now Client Side Cheats Cheat Sheet