- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 09:31 PM
Hi all
I have the following requirement:
Update multiple records existing in the instance B from instance A.
I thought this requirement could be achieved by using Table API.
So I wrote the following code based on the Code Sample obtained by calling the Table API in the [REST API Explorer] module.
// data to be used for updating
var updateData = [
{
'sys_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'[column name]': '[example001]'
},
{
'sys_id': 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
'[column name]': '[example002]'
}
];
// set as many endpoints as the number of data and execute the update process
for(var i = 0; i < updateData.length; i++){
var request = new sn_ws.RESTMessageV2();
var endP = 'https://[InstanceB].service-now.com/api/now/table/[table name]/' + updateData[i].sys_id;
request.setEndpoint(endP);
request.setHttpMethod('PUT');
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
// configure updates to endpoints
var reqBody = '{';
var objKeys = Object.keys(updateData[i]);
for(var j = 1; j < objKeys.length; j++){
var objKey = objKeys[j];
reqBody += '\"' + objKeys[j] + '\":\"' + updateData[i][objKey] + '\",';
}
var slicedReqBody = reqBody.slice(0, -1);
slicedReqBody += '}';
request.setRequestBody(slicedReqBody);
var response = request.execute();
gs.log(response.getBody());
}
Furthermore, I ran this code in [Scripts - Background] of instance A to check its operation.
As a result, this code was able to update multiple user records in instance B.
I have two questions here.
1. Is there a better way to update records for other instances?
2. Is the procedure for calling the table API from a ServiceNow instance correct as follows?
2-1. use the [REST API Explorer] module to obtain a Code Sample.
2-2. Describe the Code Sample obtained in 2-1 in [Script Includes], etc., and create a function.
2-3. call the function created in 2-2.
Regards,
Shuto
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 09:39 PM
usually table API is not used since it exposes all the fields of that table and many customers prefer scripted rest api
but if both the instances are owned and managed by same customer then it's fine.
yes that's the usual approach but we can also use schedule job etc to trigger the integration
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 09:39 PM
usually table API is not used since it exposes all the fields of that table and many customers prefer scripted rest api
but if both the instances are owned and managed by same customer then it's fine.
yes that's the usual approach but we can also use schedule job etc to trigger the integration
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader