- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2022 09:34 AM
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,
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2022 12:00 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2022 12:00 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2022 10:13 PM
Hi Marcin,
Thanks a lot for your quick response. I have tried above code and it is perfectly working well. Once again thank you very much.
Best Regards,
Sukanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2022 07:05 AM
Hi Marcin,
Hope you are doin Good!
Sorry I have one query on the same. since users doesn't know sysid's of incident if we want to incorporate numbers as a query parameters while retrieving work notes how should I incorporate that. Could you please help me on this.
Thanks and Regards,
Sukanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2022 09:30 AM
Hi Marcin,
Thank you. I have incorporated the logic for numbers instead of sysid's.
Best Regards,
Sukanya