We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Data source custom load by script with REstMessageV2

Jeff W NZAO B
Mega Guru

Hi,

Please I'm trying to create my data source load by script, with my restmessageV2, after to create my staging table and transform map using load data. But i have error with my script data source due to the table objet. Here is the code, if someone could help please. 
Thanks

 

(function loadData(import_set_table, data_source) {

    //Je recupère la methode dans le Rest Message et l'application scoped en paramètres
    var r = new sn_ws.RESTMessageV2('x_1205883_librar_0.MeteoFrance', 'getStations');

    var tableName = import_set_table.getTableName();
    //Je recupère les infos liées d'exécution de la requête dans response, récupération du corps de la réponse dans le responseBody, et le code HTTP du statut si ça marché ou pas dans httpStatus
    var response = r.execute();
    var httpStatus = response.getStatusCode();
    if (httpStatus == 200) {
        var responseBody = response.getBody();
        var data = JSON.parse(responseBody);
        var nomStation = data[0].nom;
        var idStation = data[0].id;
        var short_descStation = 'La Station météo est : ' + nomStation + '(' + idStation + ')';

        //Si le status est 200 (OK!) -> Je crée l'Incident en récupérant le nom et l'id. 

        for (var i = 0; i < parsed.length; i++) {

            var gr = new GlideRecord(tableName); // 

            gr.initialize();
            gr.station_name = parsed[i].nom;
            gr.station_id = parsed[i].id;
            gr.http_status = httpStatus;
            gr.raw_body = body;
            gr.insert();
			
			
        }
    } else {
        gs.addErrorMessage('Erreur de création : ' + httpStatus);
    }

})(import_set_table, data_source);
 

 

1 REPLY 1

yashkamde
Kilo Sage

Hello @Jeff W NZAO B ,

 

In your code you parsed the response into data but later iterate over parsed (which is undefined).

Also body is used but never defined (you stored the response in responseBody).

 

var r = new sn_ws.RESTMessageV2('x_1205883_librar_0.MeteoFrance', 'getStations');

    var tableName = import_set_table.getTableName();

    var response = r.execute();
    var httpStatus = response.getStatusCode();

    if (httpStatus == 200) {

        var responseBody = response.getBody();
        var parsed = JSON.parse(responseBody);

        for (var i = 0; i < parsed.length; i++) {

            var gr = new GlideRecord(tableName);
            gr.initialize();

            gr.station_name = parsed[i].nom;
            gr.station_id = parsed[i].id;
            gr.http_status = httpStatus;
            gr.raw_body = responseBody;

            gr.insert();
        }

    }

 

If my response helped mark as helpful and accept the solution.