Returning Certain Fields from GlideRecord Query using sys_property

Jamesk
Tera Expert

I'm working on a scripted REST API and I am looking to query incidents closed yesterday and return specific fields.

 

I can achieve this by listing all fields out in the code however I am looking to make use of a sys_property to store the fields and use these to return the fields from the query.

 

My code is below, can this be amended to make use of a sys property?

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var results = [];

    var grIncident = new GlideRecordSecure('incident');
    grIncident.addEncodedQuery('
closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()
');
    grIncident.query();

    while (grIncident.next()) {
        results.push({
            "number": grIncident.number.getValue(),
            "opened_at": grIncident.opened_at.getValue(),
            "resolved_at": grIncident.resolved_at.getValue(),
            "parent": grIncident.parent.getValue(),
            "parent_incident": grIncident.parent_incident.getValue(),
            "short_description": grIncident.short_description.getValue(),
            "description": grIncident.description.getValue(),
            "correlation_display": grIncident.correlation_display.getValue(),
            "caller_id": grIncident.caller_id.getDisplayValue(),
            "contact_type": grIncident.contact_type.getDisplayValue(),
            "priority": grIncident.priority.getValue(),
            "state": grIncident.state.getDisplayValue(),
            "category": grIncident.category.getDisplayValue(),
            "assignment_group": grIncident.assignment_group.getDisplayValue(),
            "assigned_to": grIncident.assigned_to.getDisplayValue(),
            "sys_updated_on": grIncident.sys_updated_on.getValue(),
            "sys_updated_by": grIncident.sys_updated_by.getDisplayValue(),
        });

        return results;
    }
})(request, response);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Jamesk 

Steps

1) create system property of type string and add below fields as comma separated value

number,opened_at,resolved_at,parent,parent_incident,short_description,description,correlation_display,caller_id,contact_type,priority,state,category,assignment_group,assigned_to,sys_updated_on,sys_updated_by

AnkurBawiskar_0-1756226365666.png

 

2) update your scripted REST API script as this

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var results = [];

    var fieldsProp = gs.getProperty('api.fields');
    if (!fieldsProp) {
        response.setStatus(500);
        response.setBody({
            error: 'Property x_yournamespace.incident_fields not found'
        });
        return;
    }

    var fields = [];
    var fieldsSplit = fieldsProp.split(',');
    for (var i = 0; i < fieldsSplit.length; i++) {
        fields.push(fieldsSplit[i].trim());
    }

    var grIncident = new GlideRecordSecure('incident');
    grIncident.addEncodedQuery('closed_atONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()');
    grIncident.query();

    while (grIncident.next()) {
        var recordObj = {};
        for (var j = 0; j < fields.length; j++) {
            var field = fields[j];
            var displayVal = grIncident.getDisplayValue(field);
            var valueVal = grIncident.getValue(field);
            recordObj[field] = (displayVal !== valueVal) ? displayVal : valueVal;
        }
        results.push(recordObj);
    }

    response.setBody(results);
})(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

View solution in original post

6 REPLIES 6

Brad Bowman
Mega Patron

Here's one way you can do that.  I believe you would need two system properties to differentiate between your intended getValue and getDisplayValue.  In this simplified example I created 2 system properties, retrieving 2 fields each:

BradBowman_1-1756224851606.png

 

BradBowman_0-1756224815962.png

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var results = [];
    var resultsObj = {};
    var gv = gs.getProperty('bkb.inc.gv').split(',');
    var gdv = gs.getProperty('bkb.inc.gdv').split(',');
    
    var grIncident = new GlideRecordSecure('incident');
    grIncident.addEncodedQuery('
closed_atONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()
');
    grIncident.query();

    while (grIncident.next()) {
        for (var i=0; i<gv.length; i++) {
	    resultsObj[gv[i]] = grIncident.getValue(gv[i]); 
	}
        for (var j=0; j<gdv.length; j++) {
            resultsObj[gdv[j]] = grIncident.getDisplayValue(gdv[j]); 
	}
        results.push(JSON.stringify(resultsObj));
    }
    return results;
})(request, response);

 

 

Ankur Bawiskar
Tera Patron

@Jamesk 

Steps

1) create system property of type string and add below fields as comma separated value

number,opened_at,resolved_at,parent,parent_incident,short_description,description,correlation_display,caller_id,contact_type,priority,state,category,assignment_group,assigned_to,sys_updated_on,sys_updated_by

AnkurBawiskar_0-1756226365666.png

 

2) update your scripted REST API script as this

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var results = [];

    var fieldsProp = gs.getProperty('api.fields');
    if (!fieldsProp) {
        response.setStatus(500);
        response.setBody({
            error: 'Property x_yournamespace.incident_fields not found'
        });
        return;
    }

    var fields = [];
    var fieldsSplit = fieldsProp.split(',');
    for (var i = 0; i < fieldsSplit.length; i++) {
        fields.push(fieldsSplit[i].trim());
    }

    var grIncident = new GlideRecordSecure('incident');
    grIncident.addEncodedQuery('closed_atONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()');
    grIncident.query();

    while (grIncident.next()) {
        var recordObj = {};
        for (var j = 0; j < fields.length; j++) {
            var field = fields[j];
            var displayVal = grIncident.getDisplayValue(field);
            var valueVal = grIncident.getValue(field);
            recordObj[field] = (displayVal !== valueVal) ? displayVal : valueVal;
        }
        results.push(recordObj);
    }

    response.setBody(results);
})(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Jamesk 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

@Jamesk 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.