How can i insert data coming from Rest call to a servicenow table?

Avinash Shawdar
Giga Contributor

How can i insert data coming from an Outbound Rest Api that gets data as Json, into a staging table 

I have an outbound Rest Api that gets data as Json from an external Api. I manage to call to this Rest Api and get its data from a Ui Action, but i can't manage to insert this data into its corresponding table using GlideRecord.

I would be glad if someone has a suggestion or a comment to add on my code.

Here is my script:

try {
	var r = new sn_ws.RESTMessageV2('Get users from Json placeholder', 'get');
	var response = r.execute();
	var responseBody = response.getBody();
	var httpStatus = response.getStatusCode();
	
	var result = responseBody.split(','); //This depends on what's in your response
	
	var restGR = new GlideRecord('u_users_stagging_table');
	restGR.initialize();
        // In this part i read the data coming from the Rest Api
                restGR.name = result[1]; 
		restGR.user_name = result[2]; 
		restGR.email = result[3]; 
		restGR.street = result[4]; 
		restGR.city = result[6]; 
		restGR.zip = result[7];
		restGR.company = result[12]; 
	        
                gs.log('Row: ' +restGR.name+restGR.user_name+restGR.email+restGR.street+restGR.city+restGR.zip+restGR.company);

		restGR.sys_import_set = 'bcec9d70db0f13005c965d50cf96193c'; 
		restGR.insert();
			}
		}
		catch(ex) {
			var message = ex.getMessage();
			gs.log('Here is the error in the execution of the REST API call : '+ex,'Me');
		}
1 ACCEPTED SOLUTION

Hi,

PFB screenshot.

 

find_real_file.png

and in the Log i can able to see a lot of records not only one record  please correct me am i am wrong .

Please use the insert funtion in a loop as appropriate and execute the query by commenting the address related fields.

find_real_file.png

 

and please make sure that you if you are getting multiple records  from response you have to use the insert query in a loop.

 

Note:

https://community.servicenow.com/community?id=community_question&sys_id=3d215be9dbdcdbc01dcaf3231f9619d3

follow this link it might helps you to insert multiple records.

 

Thanks,Naveen

 

 

View solution in original post

14 REPLIES 14

Cool,Glad my observations helped you.

 

Thank you so much for you reply.

 

Thanks,Naveen

Your welcome Naveen šŸ™‚ šŸ™‚ šŸ™‚

I would also like to know what the : GlideRecord_Object.sys_import_set=sys_id_of_the_import_set method does.

Hi,

Are  you asking about the below thing??

restGR.sys_import_set = 'bcec9d70db0f13005c965d50cf96193c';

it's simple like

gr.short_description = 'My import set';

so sys_import_set has to be part (field)of the table(u_users_stagging_table) and you are setting the value with bcec9d70db0f13005c965d50cf96193c.

 

 

Thanks,Naveen

Ok Naveen thank you.

Avinash Shawdar
Giga Contributor

OK