Insert data in multiple tables

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?

3 REPLIES 3

larstange
Mega Sage

Hi



I would do it the same way as you have done here.


JimmieOdelius
Tera Expert

Like Lars I don't see any way this could be made more efficient.


Kalaiarasan Pus
Giga Sage

One minor tweak that I will suggest is to try and make the column name also dynamic if you trust the source sending the data. The catch would be different the fields that are reference, choice etc vs normal string field.



gr.setValue('column name from json', 'column value from json');