Scripted REST API for inbound integration

harishdasari
Tera Guru

Hi,

I have a requirement like, when user update state, Comments, Priority etc.. in third party application, then it should be updated in servicenow

I am using the scripted rest api PUT method to update the fields in servicenow.

Could you please help,

Script Include:

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Hi Harish,



As you said when ticket is updated in third party application then it should update the same in ServiceNow.


Are you saying that you have exposed the Scripted REST API to the third party.



I could see that you have used PUT method i.e. fine.



But the way you are fetching the response is incorrect . use following


var requestBody = request.body.dataString;



try to add logs to check requestBody is received correctly in your scripted rest api resource



also you are not sending the id as json key while consuming the REST endpoint so how you will get the value by parsing the json



the json parsing you have used is not correct. use following



var parser = new global.JSON();


var parsedData = parser.decode(requestBody);



var id = parsedData.work_notes;



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


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

19 REPLIES 19

Hi Harish,



As you said when ticket is updated in third party application then it should update the same in ServiceNow.


Are you saying that you have exposed the Scripted REST API to the third party.



I could see that you have used PUT method i.e. fine.



But the way you are fetching the response is incorrect . use following


var requestBody = request.body.dataString;



try to add logs to check requestBody is received correctly in your scripted rest api resource



also you are not sending the id as json key while consuming the REST endpoint so how you will get the value by parsing the json



the json parsing you have used is not correct. use following



var parser = new global.JSON();


var parsedData = parser.decode(requestBody);



var id = parsedData.work_notes;



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


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello, 

I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.

Could you please guide me how to implement this? Sample code would be great.

Input: Sys ID of the record 
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"sys_id": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"sys_id": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}

Hi Harish,



Any update on this?



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Thanks for the Response. I am still working on it. I will post you the update.



Thank you.


Hi Ankur,



As per your guidance, I have modified the code and I changed the code as below in SCRIPTED REST Api


please see the screenshot.


find_real_file.png




SCRIPT INCLUDE:



find_real_file.png




Please correct me in Script include.


ID   is the unique field. Based on this field record should get updated in servicenow.


In servicenow u_zendesk_ticket_id   is the field mapped against to the field ID.



Thank you.