The CreatorCon Call for Content is officially open! Get started here.

Need help in storing response body in one of the field in staging table

Tanya10
Tera Contributor

Hi,
I have scripted rest api and from there I'm calling script include.
I have to set the response (which I'm getting fro third party) in custom response (string field) on staging table.
Any help?

7 REPLIES 7

I'll personally put all the http logic in Scripted REST API and only have GlideRecord logic in Script Include so Script Include can be used else where.

Sample code. Inserting data into field1 and field2 in table "u_sample_table".

Scripted REST API

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

    try {
        var requestData = request.body.data;

		var jsonObj = requestData;
        if (!jsonObj.field1 || !jsonObj.field2) {
            //response.setError(new sn_ws_err.BadRequestError(e.message));
            var unspecifiedValue = new sn_ws_err.ServiceError();
            unspecifiedValue.setStatus(400);
            unspecifiedValue.setMessage('field1 or field2 not specified');
            unspecifiedValue.setDetail('values of field1 and field2 need to be specified in json data.');
            return unspecifiedValue;
        }
		var util = new SampleTableUtil();
		var sysId = util.insertData(jsonObj);

        if (!sysId) {
            var insertError = new sn_ws_err.ServiceError();
            unspecifiedField.setStatus(500);
            insertError.setMessage('error during insert');
            insertError.setDetail('data could not be inserted into a table.');
            return insertError;
        }
        return {
            "message": 'OK'
        };
    } catch (e) {
        var internalError = new sn_ws_err.ServiceError();
        internalError.setStatus(500);
        internalError.setMessage(e.message);
        return internalError;
    }

})(request, response);

Script Include

var SampleTableUtil = Class.create();
SampleTableUtil.prototype = {
    initialize: function() {},
    insertData: function(jsonObj) {
        var grSample = new GlideRecord('u_sample_table');
        grSample.initialize();
        grSample.setValue('u_field1', jsonObj.field1);
        grSample.setValue('u_field1', jsonObj.field2);
        return grSample.insert();
    },
    type: 'SampleTableUtil'
};

hey @Hitoshi Ozawa 

Thanks . That helped.
May I know how did you handle update scenerio if you are glide recordig to staging table?

Update is done by a PUT. Just create a PUT method in the same Scripted REST API and add a updateData() method in the script include to update the table. A single Scripted REST API can include multiple methods such as GET, POST, PUT, and DELETE. There's no need to create a new Script REST API for each.