Need Help to capture/fetch element from JSON response via ATF

Som4
Tera Contributor

I am trying to fetch Incident number from JSON response and passing it to the next step step using ATF. I wrote below server side script, but it keeps giving error. 

 

(function(outputs, steps, params, stepResult, assertEqual) {

    var res= new GlideRecord("sys_atf_test_result_item");
    res.addQuery('step','10a2d28283baee50bfb311b6feaad3ad');
    res.orderByDesc("sys_updated_on");
    res.setLimit(1);
    res.query();

    while(res.next()) {

        var jsonresult=res.output;
    var parsedJson = JSON.parse(jsonresult);

    // Check if the parsed JSON has the incident number and extract it
    if (parsedJson.hasOwnProperty('result') && parsedJson.result.length > 0) {
        var incidentNumber = parsedJson.result[0].number; //'number' is a field in JSON response
        outputs.record_id = incidentNumber;
        gs.info("Incident number: " + outputs.record_id);
    } else {
        gs.info("Incident number not found in the response.");
    }
}
})(outputs, steps, params, stepResult, assertEqual);

Can someone please help here?

Error :
Som4_0-1752920341638.png

 


1 REPLY 1

Maik Skoddow
Tera Patron
Tera Patron

Hi @Som4 

such an error message appears whenever JSON.parse fails due to wrong JSON format. And the output field of the sys_atf_test_result_item table can contain everything and mostly it is plain text. So check first the data in that field before you continue.

Maik