How to parse the rest message response and populate it on custom table?

rajkrish
Tera Contributor

Hi,

        I'm able to get back Json inputs from REST message

[ {"SiteName":"SC","LabName":"SVC","UnitNumber":"M5835JC800490","MaterialLotNumber":"L542C251","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"},

{"SiteName":"SC","LabName":"SVC","UnitNumber":"M5424BB700234","MaterialLotNumber":"L541C677","ProductName":"BROADWELL EP 24C","PartTypeName":"MF 8PTSFV A B","FullLocationName":"SHIP: CR3-1-C35 Mega Lab Inventory Control","QDFNumber":"","LastVPONumber":"","LastStepName":"","LastBinName":"","MirNumber":63362,"MirRequesterWWID":"10519277","MirRecipientWWID":"10552840"}]

I need to insert   these records into a custom table(lets say u_demo table). Kindly help me with a proper coding.

Thanks,

Rajkumar.

1 ACCEPTED SOLUTION

Hi Rajkumar,



      There is small changes in code which i had missed earlier.



      Try this latest modified code. Hope it will work!



      var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method


      var response = r.execute();


      var responseBody = response.getBody(); //fetches JSON response


      var response= JSON.parse(responseBody);



      for(i=0;i<response.length;i++)


        {


                            var gr = new GlideRecord('u_labname');


                          gr.initialize();


                        gr.u_fieldname = response[i].SiteName;


                          ....................


                        ...................


                      gr.insert();


        }



Thanks,


Kaka Nayeem




PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

10 REPLIES 10

Hi Kaka,



This is what the code I have written in script include,



var vortexTest = Class.create();


vortexTest.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  helloWorld: function() {



  var r = new sn_ws.RESTMessageV2('XXX', 'get');


  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();



  var r2 = JSON.parse(responseBody);


for(i=0;i<r2.length;i++)


{



      var gr = new GlideRecord('u_vortexsnow');


      gr.initialize();


  gr.u_sitename = r2.SiteName;


  gr.u_labname = r2.LabName;


  gr.u_unitnumber = r2.UnitNumber;


  gr.u_materiallotnumber = r2.MaterialLotNumber;


  gr.u_productname = r2.ProductName;


  gr.u_parttypename = r2.PartTypeName;


  gr.u_fulllocationname = r2.FullLocationName;


  gr.u_qdfnumber = r2.QDFNumber;


  gr.u_lastvponumber = r2.LastVPONumber;


  gr.u_laststepname = r2.LastStepName;


  gr.u_lastbinname = r2.LastBinName;


  gr.u_mirnumber = r2.MirNumber;


  gr.u_mirrequesterwwid = r2.MirRequesterWWID;


  gr.u_mirrecipientwwid = r2.MirRecipientWWID;



      gr.insert();


}


  },


      type: 'vortexTest'


});



dummy.PNG


The output is like this, the value is not getting inserted. Kindly help me what should I change to make values to get inserted.



Thanks,


Rajkumar.


Hi Rajkumar,



      There is small changes in code which i had missed earlier.



      Try this latest modified code. Hope it will work!



      var r = new sn_ws.RESTMessageV2('REST_name_xxxx', 'get'); // in my case, we are using Get method


      var response = r.execute();


      var responseBody = response.getBody(); //fetches JSON response


      var response= JSON.parse(responseBody);



      for(i=0;i<response.length;i++)


        {


                            var gr = new GlideRecord('u_labname');


                          gr.initialize();


                        gr.u_fieldname = response[i].SiteName;


                          ....................


                        ...................


                      gr.insert();


        }



Thanks,


Kaka Nayeem




PS: Hit like, Helpful or Correct depending on the impact of the response


oops... thanks.


Hi Mrudula,


//


current.xx_fieldname= abc.SiteName; // SiteName being the variable from JSON


gs.addInfoMessage("Sitename is   "+abc.SiteName);//



Can you elaborate what you mean by this.



Thanks,


Rajkumar.


Mrudula6
Mega Guru

Hi Rajkumar,



Sure. The first line, I am parsing the JSON response in ABC variable. Further, I am updating my current field value of demo table(assuming, it is a BR or UI action) with "SiteName" from parsed JSON response. In case you need to insert a new record, instead of current use a new GlideRecord and insert the value in demo table.



Just try logging below code value.



var abc= JSON.parse(responseBody);


gs.addInfoMessage(abc);


current.xx_fieldname= abc.SiteName; // SiteName being the variable from JSON


gs.addInfoMessage(abc.SiteName);


current.update();