UI Extension Javascript API
UI Extension Javascript API
Section titled “UI Extension Javascript API”To interact with the standard form fields from the Javascript of a UI extension, R-Service offers the UI Extension Javascript API.
All API calls start with
ITRP
. For example, the API call
ITRP.field('note').val();
can be used to get the current value of the Note field of the Request form.
- UI Extension Javascript API
- Get initial values
- Distinguish new and existing records
- Distinguish Self Service and Specialist view
- Interact with form fields in Self Service
- Interact with form fields in the Specialist view
- Functions
- Access attributes of selected items in suggest fields
- Access attributes of selected items via metadata fields of custom views
- Populate suggest fields
- [Other properties of the ITRP object](#Other properties of the ITRP object)
Get initial values
Section titled “Get initial values”It is possible to get the initial values of a record when it is opened in edit mode. For example, to make the Attachment field required when a request is edited that has the status Assigned, use the following:
JavaScriptif (ITRP.record.initialValues.get('status') === 'assigned') { ITRP.field('attachment').required();}The general form of this API call is
ITRP.record.initialValues.get('field')
. The fields that are available depend on the type of the record that the UI extension is linked to, and are the same as those listed in the
Collection Fields
section of the respective API:
- Requests
- Problems
- Workflows
- Tasks
- Projects
- Project Tasks
- Configuration Items
- Contracts
- Organizations
- Sites
- People
- Risks
Note: some of the fields are
references
, such as the team to which a Request is assigned. You can access the reference attributes (usually
id
and
name
/
subject
) by passing both the name of the reference and the name of the attribute to the API, for example:
ITRP.record.initialValues.get('team', 'name')
.
Distinguish new and existing records
Section titled “Distinguish new and existing records”To find out whether the user is adding a new record, use the following:
JavaScript// Possible values: true and falsevar new_record = ITRP.record.new;if (new_record) { /* ... */ }Distinguish Self Service and Specialist view
Section titled “Distinguish Self Service and Specialist view”To find out whether the user is using Self Service or the Specialist view, use the following:
JavaScript// Possible values: 'self_service' and 'full_ui'if (ITRP.context === 'self_service') { /* ... */ }A practical example of this can be found in Advanced UI Extension Examples .
Interact with form fields in Self Service
Section titled “Interact with form fields in Self Service”The Javascript API offers various functions to interact with the built-in form fields of R-Service records. For each type of record and each field, the available functions are listed below. Refer to Functions for descriptions and brief examples of these functions.
Requests
Section titled “Requests”- Subject:
ITRP.field('subject')-requiredshowhidetoggleval - Note:
ITRP.field('note')-requiredshowhidetogglevalplaceholder - Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Asset:
ITRP.field('asset')-requiredshowhidetoggleval - Service instance:
ITRP.field('service_instance')-val(read only)onoffdata
Note that the fields
Subject
and
Asset
can only be interacted with when the user is registering a
new
request.
The field
Asset
can only be interacted with when the user is registering a request based on a template
with the option
Asset selection in Self Service
enabled.
- Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Status:
ITRP.field('status')-valonofftrigger
Project Tasks
Section titled “Project Tasks”- Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Status:
ITRP.field('status')-valonofftrigger
Interact with form fields in the Specialist view
Section titled “Interact with form fields in the Specialist view”The Javascript API offers various functions to interact with the form fields of records that are available in the Specialist view. For each type of record and each field, the available functions are listed below. Refer to Functions for descriptions and brief examples of these functions.
Requests
Section titled “Requests”- Subject:
ITRP.field('subject')-val - Note:
ITRP.field('note')-valplaceholder - Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Internal note:
ITRP.field('internal_note')-val - Internal attachments:
ITRP.field('internal_attachment')-requiredshowhidetogglesize - Configuration items:
ITRP.field('asset')-required - Impact:
ITRP.field('impact')-valonofftrigger - Status:
ITRP.field('status')-valonofftrigger - Service instance:
ITRP.field('service_instance')-val(read only)onoffdata
- Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Impact:
ITRP.field('impact')-valonofftrigger - Status:
ITRP.field('status')-valonofftrigger - Configuration items:
ITRP.field('asset')-required
Project Tasks
Section titled “Project Tasks”- Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Status:
ITRP.field('status')-valonofftrigger
Problems
Section titled “Problems”- Attachments:
ITRP.field('attachment')-requiredshowhidetogglesize - Impact:
ITRP.field('impact')-valonofftrigger - Status:
ITRP.field('status')-valonofftrigger - Configuration items:
ITRP.field('asset')-required
Configuration Items
Section titled “Configuration Items”- Status:
ITRP.field('status')-valonofftrigger - System ID:
ITRP.field('systemID')-valrequiredonofftrigger - Service:
ITRP.field('service')-required - Site:
ITRP.field('site')-valrequiredonofftrigger - Location:
ITRP.field('location')-valrequiredonofftrigger - Remarks:
ITRP.field('remarks')-valrequiredonofftrigger - Attachments:
ITRP.field('attachments')-required - Nr. of processors:
ITRP.field('nr_of_processors')-valrequiredonofftrigger - Nr. of cores:
ITRP.field('nr_of_cores')-valrequiredonofftrigger - Nr. of licenses:
ITRP.field('nr_of_licenses')-valrequiredonofftrigger - Service instances:
ITRP.field('service_instances')-required - Users:
ITRP.field('users')-required - Product:
ITRP.field('product')-valonofftrigger - Rule set:
ITRP.field('rule_set')-val(read only) - Configuration items:
ITRP.field('ci_relations')-required - Contracts:
ITRP.field('contracts')-required - Supplier:
ITRP.field('supplier')-valrequiredonofftrigger - Serial number:
ITRP.field('serial_nr')-valrequiredonofftrigger - Financial owner:
ITRP.field('financial_owner')-valrequiredonofftrigger - PO number:
ITRP.field('po_nr')-valrequiredonofftrigger - Asset ID:
ITRP.field('assetID')-valrequiredonofftrigger - Depreciation method:
ITRP.field('depreciation_method')-valrequiredonofftrigger - In use since:
ITRP.field('in_use_since')-valrequiredonofftrigger - Warranty expiry date:
ITRP.field('warranty_expiry_date')-valrequiredonofftrigger
- Status:
ITRP.field('status')-valonofftrigger - Mitigation target:
ITRP.field('mitigation_target_at')-valrequiredreadonlyshowhidetoggleonofftrigger - Severity:
ITRP.field('severity')-valrequiredreadonlyshowhidetoggleonofftrigger - Closure reason:
ITRP.field('closure_reason')-valonofftrigger
Functions
Section titled “Functions”required
Section titled “required”Use this function to make a field optional or required.
JavaScriptITRP.field('note').required(); // Make the Note field requiredITRP.field('note').required(true); // Make the Note field requiredITRP.field('note').required(false); // Make the Note field optionalreadonly
Section titled “readonly”Use this function to make a field readonly or read/write.
JavaScriptITRP.field('severity').readonly(); // Make the Severity field readonlyITRP.field('severity').readonly(true); // Make the Severity field readonlyITRP.field('severity').readonly(false); // Make the Severity field read/writeUse this function to get or set the value of a field.
JavaScript// Get the value of the Subject field:var value = ITRP.field('subject').val();
// Set the value of the Subject field to 'New subject':ITRP.field('subject').val('New subject');
// Clear the value of the Subject field:ITRP.field('subject').val('');
// Fill a rich text field with HTML:ITRP.field('note').val({ html: 'This is <b>bold</b> text.' });Use this function to get additional attributes of a field.
JavaScriptvar si_details = ITRP.field('service_instance').data('item');var si_id = si_details.id; // ID of service instance, same as: ITRP.field('service_instance').val()var si_name = si_details.name; // Name of service instancevar service_id = si_details.service.id; // ID of service related to service instancevar service_name = si_details.service.name; // Name of service related to service instanceUse this function to make a hidden field visible.
JavaScriptITRP.field('note').show(); // Show the note fieldUse this function to hide a field.
JavaScriptITRP.field('note').hide(); // Hide the note fieldtoggle
Section titled “toggle”Use this function to toggle the visibility of a field.
JavaScriptITRP.field('note').toggle(true); // Show the note fieldITRP.field('note').toggle(false); // Hide the note fieldUse this function to get the number of attached files.
JavaScriptvar size = ITRP.field('attachment').size();if (size < 2) { /* ... */ }placeholder
Section titled “placeholder”Use this function to set the placeholder of the note field.
JavaScriptITRP.field('note').placeholder('Describe your request here…');Use this function to attach an event handler to a field. It is based on the jQuery on() function and takes two required parameters:
event type: the type of event that should be handled, such aschange,click, orfocus.handler function: the function that should be called when events of the given event type occur.
JavaScript// Make the Remarks field required// when the Status is changed to 'In Production'ITRP.field('status').on('change', function() { var status = ITRP.field('status').val(); ITRP.field('remarks').required(status === 'in_production');});Use this function to remove an event handler from a field. It is based on the jQuery off() function and takes two required parameters:
event type: the type of event that should no longer be handled, such aschange.handler function: the handler function that was previously attached via theonfunction. Note that this should be the same function.
JavaScriptvar set_remarks_value = function() { if (ITRP.field('status').val() === 'being_repaired') { ITRP.field('remarks').val('Describe what needs to be repaired:'); // Make sure that the value of the remarks field is only filled in // the first time the status is set to 'Being Repaired': ITRP.field('status').off('change', set_remarks_value); }};
ITRP.field('status').on('change', set_remarks_value);trigger
Section titled “trigger”Use this function to manually trigger an event. It is based on the jQuery trigger() function and takes one parameter:
event type: the type of event that should be triggered, such aschange.
JavaScript// Do something when the user changes the status field to another value:ITRP.field('status').on('change', function() { /* do something ... */ });
// Make sure that the action defined above is also performed// when the form is initially displayed:ITRP.field('status').trigger('change');
// The two statements above can also be combined, as follows.// This is a common pattern when attaching an event handler.ITRP.field('status').on('change', function() { /* do something ... */}).trigger('change');Access attributes of selected items in suggest fields
Section titled “Access attributes of selected items in suggest fields”When a custom field is a suggest field (for example a
organization-suggest
or a
custom-suggest
), calling
.val()
on the field returns the ID of the selected item. You may need something else to happen depending on the selected item. For example: another field should be shown only when a certain item was selected, as described in the
Advanced UI Extension Examples
. However, using the ID of the selected item in Javascript may not be desirable, because it makes it more difficult to synchronize UI Extensions between the R-Service QA and Production environment.
To help with this use case, it is possible to access some of the attributes of the selected item.
When the suggest field allows the selection of a single value, use
.data('item')
to access these attributes.
When the suggest field allows the selection of multiple values, use
.data('items')
to get an array of selected items.
The following attributes of an item are available by default:
iddisplay_name: this is the name or subject of the item as it is displayed on the screen. In some cases thedisplay_namecan be a combination of multiple attributes. For example, thedisplay_nameof a CI is a combination of the Label and Name attribute.reference: only available for record types that have a Reference field, such as custom collection elements.
JavaScriptif ($color.data('item').reference === 'orange') { /* do something ... */ }
var is_orange = function(item) { return item.reference === 'orange'; };if ($colors.data('items').filter(is_orange).length > 0) { /* do something ... */ }Access attributes of selected items via metadata fields of custom views
Section titled “Access attributes of selected items via metadata fields of custom views”In addition to the standard attributes listed in the previous section,
custom-suggest
fields can expose additional attributes of items
by adding these attributes to the ‘Metadata fields’ field of the
Custom View
,
that the
custom-suggest
is based on.
For example, suppose you have created a custom view on
Configuration Items
and added the field
assetID
and two custom fields called
Battery Level
and
Cost Center
to its Metadata fields.
The custom field
Cost Center
refers to a custom collection element.
These metadata fields can then be accessed in the same way as the standard attributes described in the previous section:
JavaScriptvar assetID = $my_ci.data('item').assetID;var assetIDs = $my_cis.data('items').map(function(item) { return item.assetID; });
var batteryLevel = $my_ci.data('item').custom_fields['Battery Level'];var batteryLevels = $my_cis.data('items').map(function(item) { return item.custom_fields['Battery Level']; });
var costCenter = $my_ci.data('item').custom_fields['Cost Center'];var costCenterId = costCenter.id;var costCenterName = costCenter.display_name;var costCenterReference = costCenter.reference;A practical example is included in the Advanced UI Extension Examples .
Warning
: Be careful when exposing metadata fields containing sensitive information.
Suppose a
custom-suggest
is included as a public custom field in a request template offered to end users.
These end users will then be able to obtain the metadata fields of all possible values of the
custom-suggest
.
Data types
Section titled “Data types”The representation of the exposed data depends on the data type of the metadata field.
- Dates and timestamps:
ISO8601
representation of the date(and/or time)
in the UTC time zone, e.g.
2022-03-22T12:22:53Z. Examples: thecreated_atandin_use_sincefields of Configuration Items . - Enumerations
: the
idof the enumerated value, e.g.undergoing_maintenance. Examples: thestatusandrule_setfields of Configuration Items . - Booleans: as-is (i.e.
trueorfalse). Examples: thesite_licenseandtemporary_licensefields of Configuration Items . - Numerical values (integer / decimal): as-is.
Examples: the
rateandsalvage_valuefields of Products . - Strings: as-is.
Examples: the
name,labelandassetIDfields of Configuration Items . - References to a record: a hash containing
idanddisplay_name, e.g.{ id: 16, display_name: 'End-User Support, Houston' }. Also includesreferencefor record types that have a Reference field, such as custom collection elements. Examples: theproductandsupport_teamfields of Configuration Items .
Populate suggest fields
Section titled “Populate suggest fields”As explained in the previous paragraph, when a custom field is a suggest field
(for example a
organization-suggest
or a
custom-suggest
),
calling
.val()
on the field returns the ID of the selected item.
To
populate
a suggest field with a value, you can also use the
.val()
function.
The examples below illustrate how this works.
JavaScript// Set the value of a suggest field based on the ID of a record:// ('123456' is the ID of a person record in R-Service)$level_1_approver.val(123456);
// Set the value of a suggest field that allows multiple values:$level_2_approvers.val([123456, 987654]);
// When the suggest field is a custom-suggest// based on custom collection elements,// you can also use the reference of the custom collection element:$cost_center.val({ reference: "cost-center-a" });
// And if the custom-suggest allows multiple values:$cost_centers.val({ reference: ["cost-center-a", "cost-center-b"] });Other properties of the ITRP object
Section titled “Other properties of the ITRP object”ITRP.$
Section titled “ITRP.$”You can use jQuery in the Javascript of UI Extensions. Access the jQuery API via ITRP.$.
Note that the Javascript of every new UI extension starts with the lines:
var $ = ITRP.$;var $extension = $(this);The $extension variable is then used to select fields of the UI extension, add event handlers, and so on:
$extension.find('#my_field').on('change', function() { ...});ITRP.hasFormData() and ITRP.clearFormData()
Section titled “ITRP.hasFormData() and ITRP.clearFormData()”The functions ITRP.hasFormData and ITRP.clearFormData are used to interact with all form elements in a given scope:
// hasData will be true if any form element in the $extension scope has a valuevar hasData = ITRP.hasFormData($extension);if (hasData) { // Do something}var $my_button = $extension.find('#my_button');$my_button.on('click', function() { // Clears all form elements in the $extension scope. ITRP.clearFormData($extension);});A practical example using both of these functions is given on the Advanced UI Extension Examples page.
The after-prefill hook
Section titled “The after-prefill hook”The after-prefill hook can be used to execute a JavaScript function that should run after all form elements have been prefilled when editing a record.
When editing an existing record with custom fields, R-Service performs a few technical steps:
- Load the form HTML, including the HTML of the UI extension. At this point, all the UI extension form elements are present, but still empty.
- Load and execute the R-Service JavaScript.
- Load and execute the JavaScript of the UI extension.
- Fill the form elements of the UI extension with the custom field values.
- Trigger the
after-prefillhook.
The following JavaScript snippet illustrates this:
Section titled “The following JavaScript snippet illustrates this:”var $my_field = $extension.find('#my_field');
// This statement is executed in step 3console.log("Value in step 3: " + $my_field.val());
ITRP.hooks.register('after-prefill', function() { // This statement is executed in step 5 console.log("Value in step 5: " + $my_field.val());});Suppose that you are editing a request in which the custom field my_field has the value Test. The above snippet will then output the following lines to the developer console of the browser:
Value in step 3:Value in step 5: Test