Scripted REST API can only get one record

SystemsGo
Tera Contributor

Hi Experts,

 

I have the following Scripted REST API. 

 

As the log showed, it could get 3 different Case Records.

SystemsGo_1-1726723356274.png

 

But when I do test from API explorer and post man, it displayed one record.

SystemsGo_2-1726723414106.png

Is there anything wrong with coding? how to fix that?

 

 

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

    var jsonData = [];
    var rec = new GlideRecord("sn_customerservice_case");
    rec.addEncodedQuery("account=f9028d7d1b343510feb9628b234bcb16^ORaccount=1a71df4c1bcb4410f1b487f7cc4bcbe6^stateIN3,6^opened_at>=javascript:gs.beginningOfLast12Months()");
    rec.query();
    var count = 3;
    while (rec.next()) {
        count--;
        var item = {
            id: count,
            name: 'item' + count,
            number: rec.number
        };
        gs.info(item.number);
        jsonData.push(item);
        if (count == 0) {
            break;
        }

    }
    response.setBody(jsonData);
    response.setContentType('application/json');
    response.setStatus(200);

})(request, response);
2 ACCEPTED SOLUTIONS

Long Duong
Kilo Sage

Hi there,

I think there is bug in your JS coding. To fix it, please replace "number: rec.number" with "number: rec.getValue('number')"

Please mark my comment helpful if the issue is resolved.

Thank you and have a nice day!

View solution in original post

Hi, 

Thank you for your confirmation. If it worked for you, can you please mark my comments is solution or helpful?
About the document, I think it is still good. Because you can use [[number: rec.number + '']] also to convert it to string type. Hope it is clear for you.

 

Thanks

View solution in original post

3 REPLIES 3

Long Duong
Kilo Sage

Hi there,

I think there is bug in your JS coding. To fix it, please replace "number: rec.number" with "number: rec.getValue('number')"

Please mark my comment helpful if the issue is resolved.

Thank you and have a nice day!

Hi Long,

 

Thank you for your reply. It worked for me.

Thank you.

I think the following doc should be updated.

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/integrate/custom-web-services/ref...

Hi, 

Thank you for your confirmation. If it worked for you, can you please mark my comments is solution or helpful?
About the document, I think it is still good. Because you can use [[number: rec.number + '']] also to convert it to string type. Hope it is clear for you.

 

Thanks