Can we store data in multiple tables simultaneously through scripted rest API?

ishaanvohra
Kilo Contributor

HI,

I want to store data in multiple tables simultaneously by running a webservice script which will call my JSON code to store data in it.Is it possible to store data in servicenow simulatneously.And if it is possible then help me out   that how can I do that? Please help me out?

3 REPLIES 3

JimmieOdelius
Tera Expert

Hi Ishaan.



What do you mean simultaneously?



As scripted REST (and SOAP) allow you to create your own scripts there's nothing stopping you from creating two (or more) records in the same script.


Server side it will be seen as two separate database actions however.


harishdasari
Tera Guru

Hi Ishaan,



Yes you can insert records into multiple tables using REST API, But just have a doubt that what is the use of inserting same data into multiple tables ?



That doesn't make any change inserting same data into 2 different tables.



But if you need some data to be inserted into one table and some other data in different table then yes you can do it.



Thanks


ishaanvohra
Kilo Contributor

Hi,


This is the rest API script which I wrote to insert data into multiple tables from my JSON.This script will insert the data in three tables and I used GlideRecord to record the data into tables.It is inserting data into multiple tables correctly.Can this script be more efficient?



Is it the best way to store data in multiple tables through scripted rest API by using GlideRecord or is there any other better and efficient way to do that?




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


var accountId ;


var personalId;



var requestBody = request.body;


var requestData = requestBody.data;


accountId=requestData.accountId;


personalId=requestData.personalId;



var gr = new GlideRecord('Table 1'); //To insert data in first table


gr.initialize();


gr.account_id.setDisplayValue(accountId);


gr.personal_id.setValue(personalId);



var changes=requestData.changes;


var gr1 =new GlideRecord('Table 2'); //to insert data in second table


            gr1.initialize();


            gr1.additional_information.setDisplayValue(changes.additionalInformation);


            gr1.change_operation.setDisplayValue(changes.changeoperation);


gr1.insert();



var Change=requestData.Change;


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


var gr2=new GlideRecord('Table 3');   //to insert data in third table


          gr2.initialize();


        var Observer=Change[i].Observer;


var Changeoperation = Change[i].changeoperation;


          gr2.change_operation.setValue(Changeoperation);


          gr2.observer.setValue(Observer);


gr2.u_attestation_task_reference.setDisplayValue(taskID);


          gr2.insert();


}






This is the sample JSON which my web service will call :-


{


  "accountId": "DROSE",


  "personalId": "PER-ISHU" ,


  "changes": {


     


          "changeoperation": "Add",


          "additionalInformation": "Changed record is added"


      },


      "Change": [


          {


              "changeoperation": "delete",


              "Observer": "Rejected Change"


          },


          {


              "changeoperation": "add",


              "Observer": "Add the change"


          }


      ]



}


Is there any other way to store data in multiple tables which is better than this and which is more efficient?