Populate table from REST message & scheduled job

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 07:01 PM
I created a RESTmessage to obtain data from an external API. I am now attempting to create a scheduled job with a script in order to put that REST data into a table i built. Here is my code (scheduled job). I feel like I am close because it is creating a record, but everything field is (empty). Any help would be greatly appreciated.
var r = new sn_ws.RESTMessageV2('test to vmware', 'Default GET');
r.setMIDServer('Stage1');
var response = r.execute();
//var responseBody = response.getBody();
var jsonString = response.getBody();
gs.log('renies jsonString '+ jsonString,'renie');
var parser = new JSONParser();
var parsed = parser.parse(jsonString);
if(response) {
var gr = new GlideRecord('u_vcenter'); gr.initialize(); gr.setValue("name", response.u_name); gr.insert();
}
//gs.log('renies parsed '+ parsed, 'renie');
for(var i=0; j < parsed.response.length; i+=1) {
var u_name= parsed.response[i].u_name;
var u_vm = parsed.response[i].vm;
//gs.log('u_name '+ u_name, 'renie');
//var gr = new GlideRecord('u_vcenter');
//gr.initialize();
//gr.u_name = u_name;
//gr.insert();
}
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 07:36 PM
Hi,
What are the results of your debugging?
If you are close, then in every loop of 'if(response)' you should be able to log the all values that you are trying to insert for each record.
Also, unless I misunderstand you appear to be initializing and inserting before you loop through your dataset.
var gr = new GlideRecord('u_vcenter'); gr.initialize(); gr.setValue("name", response.u_name); gr.insert();
and I'm not sure if you should be using setValue, perhaps the format for the data should be gr.myFieldName = myPayload.fieldName
- The syntax of the glidequery you have commented out looks to be more appropriate.
How is your rest message formatted, what does this string gs.log('renies jsonString '+ jsonString,'renie') look like?
Often multiple rest messages are encapsulated in a parent container, eg "records"
If your data is formatted in this manner you may need to loop through response.records and not the parent contain – response.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 07:40 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 08:33 PM
Hi, perhaps you can post the log entry generated by
gs.log('renies jsonString '+ jsonString,'renie');
You can sanitize the data - it is the overall format that is of interest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 08:35 PM
So this is my code, doing the same thing (i.e. doing a REST Get call and from the response extracting data to create a new record in a table)
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<Instance Address to make the call>/api/now/table/<tableName>?sysparm_query=sys_id%3D87c401aedbc2b300e0eebc5a3a96195f&sysparm_limit=1');
request.setHttpMethod('GET');
request.setAuthenticationProfile('basic','<sys_id of the Basic Auth Profile>' );
request.setRequestHeader("Accept","application/json");
var response = request.execute();
var resBody = JSON.parse(response.getBody());
var statusCode = response.getStatusCode();
var responseResult = resBody.result.length > 0 ? resBody.result : [];
responseResult.forEach(function(el){
var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "This is for REST Call "+el.short_description;
gr.description = el.number+" - "+el.description;
gr.insert();
});
Let me know if you have any question or if this works or not.
Cheers,
Sam