How to create multiple record at a time through Scripted REST API in ServiceNow?

Not applicable

Issue: I need to create multiple record in Snow system at a time through Scripted REST API. Any idea how to achieve this solution?

Scenario: There is a record A which has table name X.

Again there is record B and table name Y, but record B is present in the related list of record A, for example Approval field is present is related list of RITM.

Expected sol: I need to create 100 records for B,

That 100 record's Name should be 100, 102,103.... and up to 200.

1 ACCEPTED SOLUTION

johnfeist
Mega Sage
Mega Sage

Maybe I'm missing something here.  If you are developing the Scripted Rest API on your instance, the code can include pretty much anything you would like.  All you need is to have a way for your code to know what needs to be part of the table B rows.  Something like this:

var tableARecord = new GlideRecord("tableA");
//Set the values
var newTableA = tableARecord.insert();
for (var i = 0; i < 100; i++) {
    var tableBRecord = new GlideRecord("tableB");
    tableBRecord.setValue("name", i);
    tableBRecord.setValue("<reference field>", newTableA.sys_id);
    //set any other values
    tableBRecord.insert();
}

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

5 REPLIES 5

Hi VJ,

Lets start at the beginning.  If you need to create a script include navigate to System Definition | Script Includes.  Click on the New button.  If you will need to call parts of this script include from client scripts, check the client callable box for the new script.  Otherwise you can leave it alone.

You will add the code to create your records into the script include like this:

var MyNewScriptInclude = Class.create();
MyNewScriptInclude.prototype = {
    initialize: function() {},
    populateRecords: function() {
        var tableARecord = new GlideRecord("tableA");
        //Set the values
        var newTableA = tableARecord.insert();
        for (var i = 0; i < 100; i++) {
            var tableBRecord = new GlideRecord("tableB");
            tableBRecord.setValue("name", i);
            tableBRecord.setValue("<reference field>", newTableA.sys_id);
            //set any other values
            tableBRecord.insert();
        }
    },
    type: 'MyNewScriptInclude'
};

populateRecords() is the function within the myNewScriptInclude class that will actually create the records for you.

Within your scripted REST API you will have the following:

var theScript = new myNewScriptInclude();
theScript.populateRecords();

You can easily have populateRecords() send back a result with a simple return statement.  In that case, just change the second line in the above to:

var theResult = theScript.populateRecords();

You can pass arguments into populateRecords() like you would with any function call.

I used generic names for this example.  Naming is open to you/your organizational standards.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster