GlideSPScriptable - Scoped

The GlideSPScriptable API provides a set of methods for use in Service Portal Widgets.

You access GlideSPScriptable methods by using the global $sp object.

Scoped GlideSPScriptable - canReadRecord(GlideRecord gr)

Returns true if the user can read the specified GlideRecord.

If the record type is kb_knowledge, sc_cat_item, or sc_category, the method checks if the user can view the item.

Table 1. Parameters
Name Type Description
gr GlideRecord The GlideRecord to check.
Table 2. Returns
Type Description
Boolean True if the record is valid and readable.

Server script

//Server script
data.items = [];
data.userName = gs.getUserDisplayName();
var gr = new GlideRecord("sc_cat_item");
gr.query();
while(gr.next() && data.items.length < 10) {
    if ($sp.canReadRecord(gr)) {
        data.items.push(gr.getDisplayValue("name"));
    }
}

//HTML template
<div class="panel panel-default">
    <div class="panel-heading">Hi, {{c.data.userName}}!</div>
    <div class="panel-body">
        Here are some things you can order:
        <ul><li ng-repeat="item in c.data.items">{{item}}</li></ul>
    </div>
</div>

Scoped GlideSPScriptable - canReadRecord(String table, String sysId)

Returns true if the user can read the specified GlideRecord.

If the record type is kb_knowledge, sc_cat_item, or sc_category, the method checks if the user can view the item.

Table 3. Parameters
Name Type Description
table String Name of the table to query.
sysId String Sys_id of the record to query.
Table 4. Returns
Type Description
Boolean True if the record is valid and readable.

Scoped GlideSPScriptable - getCatalogItem(String sysId)

Returns a model and view model for a sc_cat_item or sc_cat_item_guide.

This method is deprecated. Use the getCatalogItem(String sysId, Boolean isOrdering) method instead. This method calls the getCatalogItem(String sysId, Boolean isOrdering) method with the isOrdering parameter set to false, which means that write roles security checking is done.

This method is a quick way to get the data necessary to render and order a catalog item using <sp-model />. If you just need to get a catalog item to show its picture or name, use GlideRecord to query the sp_cat_item table.

Table 5. Parameters
Name Type Description
sysId String The sys_id of the catalog item (sc_cat_item) or order guide (sc_cat_item_guide).
Table 6. Returns
Type Description
Object An object containing the catalog item variable model, view, sections, pricing, and client scripts.
// Server script
(function() {
    var sys_id = $sp.getParameter("sys_id")
    data.catItem = $sp.getCatalogItem(sys_id);
})();

// Client script
function($http, spUtil) {
    var c = this;
    var submitting = false;
    c.getIt = function() {
        if (submitting) return;
        $http.post(spUtil.getURL('sc_cat_item'), c.data.catItem).success(function(response) {
            if (response.answer) {
                c.req = response.answer;
                c.req.page = c.req.table == 'sc_request' ? 'sc_request' : 'ticket';
            }
        });
    }
}

//SCSS
.img-bg {
    padding: 5px;
    background-color: $brand-primary;
}

.img-responsive {
    margin: 0 auto;
}

.cat-icon {
    display: block;
    margin: -40px auto 0;
}

// HTML template
<div class="col-sm-4">
  <div class="panel panel-default">
    <div class="img-bg">
      <img ng-src="{{::data.catItem.picture}}" class="img-responsive" />
    </div>
    <span class="cat-icon fa fa-stack fa-lg fa-3x hidden-xs">
      <i class="fa fa-circle fa-stack-2x text-success"></i>
      <i class="fa fa-desktop fa-stack-1x fa-inverse"></i>
    </span>
    <div class="panel-body">
      <p class="lead text-center">{{::data.catItem.name}}</p>
      <ul class="list-unstyled">
        <li class="text-center" ng-if="::data.catItem.price">${Price}: {{::data.catItem.price}}</li>
      </ul>
      <sp-model form-model="::data.catItem" mandatory="mandatory"></sp-model>
      <p ng-if="c.req" class="text-center text-success">
        ${Request created!} <a href="?id={{c.req.page}}&table={{c.req.table}}&sys_id={{c.req.sys_id}}">{{c.req.number}}</a>
      </p>
      <button ng-if="!c.req" class="btn btn-default btn-block" ng-click="c.getIt()">${Get it}</button>
    </div>
  </div>
</div>

Scoped GlideSPScriptable - getCatalogItem(String sysId, Boolean isOrdering)

Returns a model and view model for a sc_cat_item or sc_cat_item_guide.

This method is available in IP3a, HP4 and subsequent releases.

See Apply permissions to a service catalog variable for information on security roles for Service Catalog variables.

Table 7. Parameters
Name Type Description
sysId String The sys_id of the catalog item (sc_cat_item) or order guide (sc_cat_item_guide).
isOrdering Boolean When true, uses create roles security check. When false, uses write roles security check.

When users are ordering an item or have it in their cart, check using the create roles.

If users are not ordering, for example, somebody is looking at a requested item to see the variables associated with that item, then check using the write roles.

Table 8. Returns
Type Description
Object An object containing the catalog item variable model, view, sections, pricing, and client scripts.

Scoped GlideSPScriptable - getDisplayValue(String fieldName)

Returns the display value of the specified field (if it exists and has a value) from either the widget's sp_instance or the sp_portal record.

Table 9. Parameters
Name Type Description
fieldName String Name of the field
Table 10. Returns
Type Description
String The display value from either the sp_instance or sp_portal record.
//Server script
(function() {
    data.title = $sp.getDisplayValue("title");
    data.catalog = $sp.getDisplayValue("sc_catalog");
})();

//HTML template
<div>
    <h1>sp_instance.title: {{::data.title}}</h1>
    <h1>sp_portal.sc_catalog: {{::data.catalog}}</h1>
</div>

Scoped GlideSPScriptable - getField(GlideRecord gr, String fieldName)

Returns information about the specified field in the specified GlideRecord.

Table 11. Parameters
Name Type Description
gr GlideRecord The GlideRecord to check
fieldName String The field to find information for
Table 12. Returns
Type Description
Object An object containing the field's label, value, displayValue, and type. Returns null if the GlideRecord of field name are not valid, or if the field is not readable.

Scoped GlideSPScriptable - getFields(GlideRecord gr, String fieldNames)

Checks the specified list of field names, and returns an array of valid field names.

Table 13. Parameters
Name Type Description
gr GlideRecord The GlideRecord to check
field Names String A comma separated list of field names.
Table 14. Returns
Type Description
Array An array of valid fields.

Scoped GlideSPScriptable - getFieldsObject(GlideRecord gr, String fieldNames)

Checks the specified list of field names and returns an object of valid field names.

Table 15. Parameters
Name Type Description
gr GlideRecord The GlideRecord to check
field Names String A comma separated list of field names.
Table 16. Returns
Type Description
Object An object containing valid field names.

Scoped GlideSPScriptable - getForm(String tableName, String sysId)

Return the form.

Table 17. Parameters
Name Type Description
tableName String The name of the table
sysId String The form's sys_id
Table 18. Returns
Type Description
Object The form

Scoped GlideSPScriptable - getListColumns(String tableName, String view)

Returns a list of the specified table's columns in the specified view.

Table 19. Parameters
Name Type Description
tableName String Name of the table
view String The view by which to filter the columns
Table 20. Returns
Type Description
Object An object containing the column names.

Scoped GlideSPScriptable - getMenuHREF(GlideRecord page)

Returns the (?id=) portion of the URL based on the sp_menu type.

Table 21. Parameters
Name Type Description
page GlideRecord The page
Table 22. Returns
Type Description
String The href portion of the URL.

Scoped GlideSPScriptable - getMenuItems(String sysId)

Returns an array of menu items for the specified instance.

Table 23. Parameters
Name Type Description
sysId String sysId of the instance
Table 24. Returns
Type Description
Array Menu items for the specified instance

Scoped GlideSPScriptable - getParameter(String name)

Returns the value of the specified parameter.

Table 25. Parameters
Name Type Description
name String The name of the key from the query string or post body.
Table 26. Returns
Type Description
Object Returns the specified parameter as an object. Returns null if there is no request, JSON request, or widget.

Scoped GlideSPScriptable - getPortalRecord()

Returns the portal's GlideRecord.

Table 27. Parameters
Name Type Description
None
Table 28. Returns
Type Description
GlideRecord The portal record
//Server script
(function() {
    var portalGr = $sp.getPortalRecord();
    data.logo = portalGr.getDisplayValue("logo");
    data.homepage = portalGr.getDisplayValue("homepage.id");
})();

//HTML template
<div>
    <img ng-src="{{::c.data.logo}}" />
    <a href="?id={{::c.data.homepage}}">Click here to go home</a>
</div>

Scoped GlideSPScriptable - getRecord()

Returns the current portal context.

Table 29. Parameters
Name Type Description
None
Table 30. Returns
Type Description
GlideRecord The sp_portal record of the current portal context or null. Returns null if the widget is embedded by another widget.
//Server script
(function() {
    var portalGr = $sp.getPortalRecord();
    data.logo = portalGr.getDisplayValue("logo");
    data.homepage = portalGr.getDisplayValue("homepage.id");
})();

//HTML template
<div>
    <img ng-src="{{::c.data.logo}}" />
    <a href="?id={{::c.data.homepage}}">Click here to go home</a>
</div>

Scoped GlideSPScriptable - getRecordDisplayValues(Object data, GlideRecord from, String names)

Copies display values for the specified fields into the data parameter.

Table 31. Parameters
Name Type Description
data Object The display values for the specified fields are copied to this object.
from GlideRecord The GlideRecord to process.
names String A comma-separated list of field names.
Table 32. Returns
Type Description
void

Scoped GlideSPScriptable - getRecordElements(Object data, GlideRecord from, String names)

For the specified fields, copies the element's name, display value, and value into the data parameter.

Table 33. Parameters
Name Type Description
data Object The element's name, display value, and value for the specified fields are copied to this object.
from GlideRecord The GlideRecord to process.
names String A comma-separated list of field names.
Table 34. Returns
Type Description
void

Scoped GlideSPScriptable - getRecordValues(Object data, GlideRecord from, String names)

Copies values for the specified field names from the GlideRecord into the data parameter.

Table 35. Parameters
Name Type Description
data Object The value for the specified fields are copied to this object.
from GlideRecord The GlideRecord to process.
names String A comma-separated list of field names.
Table 36. Returns
Type Description
void

Scoped GlideSPScriptable - getStream(String table, String sysId)

Gets the activity stream for the specified record. This method works on tables that extend the task table.

Table 37. Parameters
Name Type Description
table String The table name
sysID String The sys_id of the record
Table 38. Returns
Type Description
Object If a table extending the task table is specified, contains the display_value, sys_id, short_description,number, entries, user_sys_id, user_full_name, user_login, label, table, and journal_fields properties; otherwise contains the table and sys_id properties.

Scoped GlideSPScriptable - getUserInitials()

Returns the user's initials.

Table 39. Parameters
Name Type Description
None
Table 40. Returns
Type Description
String The user's initials

Scoped GlideSPScriptable - getValue(String name)

Returns the value of the specified parameter.

Table 41. Parameters
Name Type Description
name String Name of the parameter
Table 42. Returns
Type Description
Object Value of the specified parameter. Null if the request does not exist or has no such parameter, the rectangle does not exist or has no such parameter, or the portal is null or has no such parameter.
//Server script
(function() {
    data.title = $sp.getValue("title");
    data.catalog = $sp.getValue("sc_catalog");
})();

//HTML templage
<div>
    <h1>sp_instance.title: {{::data.title}}</h1>
    <h1>sp_portal.sc_catalog: {{::data.catalog}}</h1>
</div>

Scoped GlideSPScriptable - getValues(Object data, String names)

Copies values from the request or instance to the data parameter.

Table 43. Parameters
Name Type Description
data Object Receives the parameter values.
names String Comma-separated string of field names.
Table 44. Returns
Type Description
void

Scoped GlideSPScriptable - getWidget(String sysID, Object options)

Gets a widget by id or sys_id, executes that widget's server script using the provided options, then returns the widget model.

Table 45. Parameters
Name Type Description
sysID String The widget sys_id or widget_id
options Object An object to pass to the widget's server script. Refer to this object as options in your server script.
Table 46. Returns
Type Description
Object A widget model to be used with sp-widget.
//Server script
data.myWidget = $sp.getWidget('widget_id', {p1: param1, p2: param2});

//HTML
<sp-widget widget="c.data.myWidget"></sp-widget>