DynamicGlideAjax: A universal reusable GlideAjax script include to return data dynamically as JSON

Russ T
Kilo Guru

DynamicGlideAjax is a script include to provide GlideAjax functionality without needing to create a new script include each time you need to find data from a table. You can just send a query and a list of fields to the script include and the response is returned as JSON for processing client side.

 

  • Dotwalking is available but will only return on the display key/value pair. The normal key:value pair will return null
  • When dotwalking, the returned value will replace . with _ so if you query manager.email, the returned value will be in the key display_manager_email. For a sys_id just query manager.sys_id.
  • This will return both the value and display value of each field in the JSON

Depending on your query you will either have one record returned or multiple, so this can be useful for, say, populating specific fields on a form (one record) or populating a choice list (multiple records).

 

Usage

You can call the script include with multiple parameters:

 

tableThe name of the table to query
encodedQueryA standard encoded query to look up the record(s)
orderByOptional - the field to order the lookup results by
fieldListA comma separated list of the fields you want to look up and have returned
ignoreDomainOptional. Boolean true/false. If true, the query will be run with queryNoDomain() to ignore domain separation.

 

Example:

 

    var ga = new GlideAjax('DynamicGlideAjax');
    ga.addParam('sysparm_name', 'GetData');
    ga.addParam('encodedQuery', 'active=true^u_business_unit=true');
    ga.addParam('table', 'core_company');
	ga.addParam('orderBy', 'u_display_name');
    ga.addParam('fieldList', 'sys_id,u_display_name,u_primary_approver');

    ga.getXML(populateData);

 

 

The data is returned as JSON key/value pairs, so in the example above you'd get the data as u_display_name:value and display_u_display_name:value with the latter being the "DisplayValue" of the field. Note that all data is returned as a string so Boolean value would be "1" as the value and "true" as the display value.

 

You can then parse the data as needed to populate fields or any other script methods that require the data.

If you want to dotwalk, just put the dotwalked field into the fieldList parameter, for example you can add manager.email and the key/value pair will return display_manager_email with the populated data (again note returned key replaces . with _ for dotwalked fields).

 

Example returned data: 

[{"display_location":"123 London Street","location":"80caff9b47dff194f0a2c60cd36d43be"}]

 

Examples:

 

Multiple records:

 

function populateData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer); //Transform the JSON string to an object
        for (var i = 0; i < answer.length; i++) { //Loop into the array

g_form.addOption('pick_one', answer[i].sys_id, answer[i].u_display_name + " - " answer[i].display_u_primary_approver)
}

 

 

Single record expected:

 

function populateData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer); //Transform the JSON string to an object


g_form.setValue('approver', answer[0].u_primary_approver)
}

 

 

Dotwalking example:

 

    var ga = new GlideAjax('DynamicGlideAjax');
    ga.addParam('sysparm_name', 'GetData');
    ga.addParam('encodedQuery', 'u_active=true^sys_id=' + g_form.getValue('requested_for'));
    ga.addParam('table', 'sys_user');
    ga.addParam('fieldList', 'sys_id,user_name,manager.email');

    ga.getXML(populateData);

function populateData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer); //Transform the JSON string to an object


g_form.setValue('manager_email', answer[0].display_manager_email)
}

 

 

Download here: ServiceNow Share 

0 REPLIES 0