Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Scripted Rest API to retrieve work_notes of an incident for multiple sysid's which is passed as paramters

ss52
Tera Contributor

Hi Everyone,

I have a requirement where we are developing scripted rest api to retrieve work_notes of an incidents by passing multiple sys id's in one request and should display Incident number (INCXXXX)1 :  whole work_notes of that incident,

            incident number 2: work_notes  in json format in response body

This is the script which I have incorporated in scripted rest api.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var workarr = [];
    // implement resource here
    var sysID = request.pathParams.sysid;
    var SIR = new GlideRecord('sys_journal_field');
    SIR.addEncodedQuery('element_idLIKE' + sysID + '^element=work_notes');
    SIR.query();
    while (SIR.next()) {
        var body = {};
        workarr.push(SIR.value.toString());
    }
    body.worknotes = workarr;
    response.setBody(body);


})(request, response);

The above script is working fine for one sys id and able to retrieve worknotes from journal table.

How should i incorporate mutilple sys id's which is passing by users in the api call and tarverse through journal field table and getting incident number of those sys-id's and displaying in teh format below.

{

IncXX1: worknotes,

INCXX2 : worknotes,

}

 

Thanks In Advance,

1 ACCEPTED SOLUTION

Marcin20
Giga Guru

Hi,

 

I propose the following approach:

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

        var body = {};
        var sysID = request.pathParams.sysid;

        //Assume that sysid contains incident sysids as a comma separated string
        var sysIDarr = sysID.split(',');

        for (var i = 0; i < sysIDarr.length; i++) {
            var workarr = [];
            var incNum = '';

            var IR = new GlideRecord('incident');
            if (IR.get(sysIDarr[i])) {
                incNum = IR.getng apValue('number');
            } else {
                continue;   // skipping not valid sysids
            }

            var SIR = new GlideRecord('sys_journal_field');
            SIR.addEncodedQuery('element_idLIKE' + sysIDarr[i] + '^element=work_notes');
            SIR.query();
            while (SIR.next()) {

                workarr.push(SIR.value.toString());
            }

            body[incNum] = workarr;
        }

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

 

Best Regards,

Marcin

 

If my answer helped you in any way, please mark this answer as helpful and correct.

View solution in original post

5 REPLIES 5

Hi Sukanya,

That is great. Thank you for the update.

 

Best Regards,

Marcin