Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Consume multiple records through one rest Api Call

Kristina2
Tera Contributor

Hey guys,

I am exploring Batch API 

My requirement is , an internal team will post 10000 of Rest Calls per day. 

Each Rest Call has around 300 records that has to be inserted to target table . So i cannot use Import Set API , as Import Set API only inserts/updates one record per call . 

So I was exploring Scripted Rest API to parse the JSON and then insert to the Import Set table to transform. 

There are some reference fields i have to populate and also create a Parent Child Relationships , so i would have to have a transform to build logic . 

I also wanted to know about the Batch API . 

Lets assume , i provide the Batch API end point to a different team, if they are going to post Rest Calls with multiple records , how is the data being transformed and how can i add logic before i transform data to Target table . 

Is Batch API is an upgraded version of Table API with batches of Records ?? 

 

 

Thank You 

 

@Chuck Tomasi  @Ankur Bawiskar 

1 ACCEPTED SOLUTION

Hi,

you need to send array of json objects

the key would be the column name in import set

Example:

field1 and field2 are the column names of the import set staging table

[

{

"field1":"value1",

"field2":"value2"

},

{

"field1":"value3",

"field2":"value3"

}

]

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

20 REPLIES 20

@Kristina 

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

If not, please let us know if you need some more assistance.

Thanks!
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur , 

 

I was able to create the records in import set table. 

 

But the records are not automatically getting created in the Target table. Should i get an Inbound Web Service for this ? 

 

var ImportSetLoader = Class.create();
ImportSetLoader.prototype = {
    initialize: function(importSetTable, requestBody) {
        this.importSetTable = importSetTable;
        this.requestBody = requestBody;
        this.importSetRowCounter = 0;
        this.importSetSysID = this.createImportSet();
    },
    startImport: function() {
        for (var jsonRow in this.requestBody) {
            this.createImportSetRows(this.requestBody[jsonRow]);
        }
		this.closeImportSet();
    },
    createImportSet: function() {
        var importSertGr = new GlideRecord('sys_import_set');
        importSertGr.initialize();
        importSertGr.setValue('mode', 'synchronous'); //@ARG: to make sure transform map runs immediately after importset rows are inserted
        importSertGr.setValue('state', 'loading');
        importSertGr.setValue('table_name', this.importSetTable);
        importSertGr.setValue('short_description', 'Bypassing importsetAPI: Synchronous REST transformation');
        importSertGr.setValue('creation_source', 'ws_insert');
        importSertGr.max_row_id = 1; //@ARG: Not sure what this is
        var importSetSysID = importSertGr.insert();
        return importSetSysID;
    },
	closeImportSet: function(){
		var importSertGr = new GlideRecord('sys_import_set');
		importSertGr.get(this.importSetSysID);
		importSertGr.setValue('state', 'loaded');
		importSertGr.update();
	},
    createImportSetRows: function(jsonRow) {

        var importSetRowGr = new GlideRecord(this.importSetTable);
        importSetRowGr.initialize();
        importSetRowGr.setValue('sys_import_set', this.importSetSysID);
        importSetRowGr.setValue('sys_import_row', this.importSetRowCounter);
        importSetRowGr.setValue('sys_import_state', 'pending');
        /*@ARG: Map all the columns in the Request Body to the Import set Columns  */
        for (var jsonColumn in jsonRow) {
            try {
                importSetRowGr[jsonColumn] = jsonRow[jsonColumn];
            } catch (e) {
                gs.log('Unable to import the column ' + jsonColumn + ' Error:' + e.getMessage());
            }
        }

        importSetRowGr.insert();
        this.importSetRowCounter++;//@ARG: Incriment the import set row Counter, this is used by the transform map to sequentially process the import set rows
    },
    type: 'ImportSetLoader'
};

Hi,

the records created in staging table should auto-transform if you have transform map associated with that data source staging table

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hey Ankur , 

 

I did some digging and got the solution , i had some issues setting the mode Synchronous , that worked . 

 

Thanks for all your time , i am going to mark your answer correct . 

 

I appreciate all your time for the community . 

 

Keep Rocking !!!!

Glad to help.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader