- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-17-2017 11:12 PM
I have a third party endpoint, when i save it am getting only 'Default Get' method(no other methods are generated), when i test, am able to get all user details in 'Response' field (json format) , now i want to add these records to my sys_user table, how to do that?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 01:08 AM
Hi Swathi,
you need to parse the response using JSONParser() and iterate over the key value pairs
Can you share the response so that the script can be designed?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 02:00 AM
Hi Swathi,
First of all you have to remove the slash from the response so that you can parse the JSON
var jsonString = "{\"UserID\":\"10001460\",\"Name\":\"B. Swathi\",\"FIrst Name\":\"B.\",\"Last Name\":\"Swathi\",\"Email\":\"\",\"Company\":\"ABC Corp Limited\",\"Company Code\":\"ABC0002\",\"Building\":\"Nagar\",\"City\":\"Cp Dist\",\"Country\":\"INDIA\",\"Department\":\"Support\",\"Vertival\":\"Admin\",\"Sub Vertical\":\"Admin\",\"Gender\":\"Male\",\"Business Phone\":\"\",\"Location\":\"India\",\"Manager\":\"Ashok\",\"State\":\"Kar\",\"Street\":\"kur\",\"Time Zone\":\"\",\"VIP\":\"\",\"Pincode\":\"500\"}";
jsonString = jsonString.replaceAll("\\","");
var parser = new JSONParser();
var parsedData = parser.parse(jsonString);
var userId = parsedData.UserID;
var Name = parsedData.Name;
gs.print(userId);
gs.print(Name);
this prints the userId and name in background script. execute this in background script
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 02:02 AM
Hi Swathi,
Once you get all the values such as name, userId, location etc then you can populate it in sys_user table
var gr = new GlideRecord('sys_user');
gr.initialize();
gr.user_name = userId;
gr.location = location
.
.
.
gr.insert();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 02:41 AM
try {
var r = new sn_ws.RESTMessageV2('Test', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new global.JSON();
var parsed = parser.decode(responseBody);
var gr = new GlideRecord('sys_user');
gr.initialize();
gr.name = parsed.Name;
gr.u_user_id = parsed.UserID;
gr.insert();
}
catch(ex) {
var message = ex.getMessage();
}
===============================================
Hi Ankur,
am using above script in my scheduled jobs, but record is getting created in sys_user table, but name and user_id fields are not populating, every thing is blank....
What am doing wrong?
-------
Am getting the responsons properly....
This the response when i test get message:
{\"UserID\":\"10001111\",\"Name\":\"Swathi\"}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 02:47 AM
Hi Swathi,
Did you print the jsonString?
Try following line and print the values
var parser = new JSON().decode(responseBody);
gs.print(parser.UserID);
gs.print(parser.Name);
First try to print the values and then insert into user table
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 10:41 PM
Hi Ankur, am getting 100+ records, just i copied one record.. How to add all other record?