
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2022 09:33 PM
Hi Team,
Currently we are copying production over non-prod instance once in a month and we manually export/import update or created user/group records from production to non-prod instances. We have a requirement now that we need pull these newly created or updated records automatically from production instance.
Please share your inputs or solution if you have implemented similar set-up. Any input on this would be much appreciated.
Thanks,
Fedrick
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 11:16 PM
Hello Feddy,
You can use the something like below script to get the Record from your production instance and create the recods with same sys_id in your dev instance:
You need to replace the your PROD instance, tableName and sysparm_query (used name in this example) in the below script and can run this in background script for testing on your Dev instance and later on run in scheduled job.
You may need to create separate Scheduled Job for User, Group, Group Member, User Role etc to sync the data from required table and can use query created at or after yesterday something like this.
Also, you need to provide the username and password of the user from PROD instance.
This is a generic solution and you may need to make changes as per your requirement.
(function () {
var tableName = "cmdb_ci_server";
var RESOURCE_PATH = "/api/now/table/" + tableName;
var request = new sn_ws.RESTMessageV2();
request.setEndpoint("https://<YOUR_PROD_INSTNCE>.service-now.com/"+RESOURCE_PATH );
request.setHttpMethod('GET');
request.setQueryParameter("sysparm_query", "name=ApplicationServerHelpdesk");
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);
//gs.print(JSON.stringify(requestBody));
//request.setRequestBody(JSON.stringify(requestBody));
request.setRequestHeader("Accept","application/json");
var response = request.execute();
var responseBody = response.getBody();
gs.print(responseBody);
var jsonResponse = JSON.parse(responseBody);
var responseResult = jsonResponse["result"];
for (var record in responseResult) {
var cmdbServer = new GlideRecord(tableName);
cmdbServer.newRecord();
var insertRecord = false;
var recordData = responseResult[record];
for (var data in recordData) {
var fieldName = "";
var fieldValue = "";
if (fieldName == "sys_id") {
cmdbServer.setNewGuidValue(fieldValue);
} else if (typeof recordData[data] == "object") {
fieldName = data;
fieldValue = recordData[data]["value"];
gs.print(fieldName + " : " + fieldValue);
} else {
fieldName = data;
fieldValue = recordData[data];
gs.print(fieldName + " : " + fieldValue);
}
if (fieldName && fieldValue) {
insertRecord = true;
cmdbServer.setValue(fieldName, fieldValue);
}
}
if (insertRecord) {
cmdbServer.insert()
}
}
})();
Please mark my respsone as helpful/correct, if it answer your question.
You can start with this and if you have any other question specific you your integration you can asked raise other question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2022 11:16 PM
Hello Feddy,
You can use the something like below script to get the Record from your production instance and create the recods with same sys_id in your dev instance:
You need to replace the your PROD instance, tableName and sysparm_query (used name in this example) in the below script and can run this in background script for testing on your Dev instance and later on run in scheduled job.
You may need to create separate Scheduled Job for User, Group, Group Member, User Role etc to sync the data from required table and can use query created at or after yesterday something like this.
Also, you need to provide the username and password of the user from PROD instance.
This is a generic solution and you may need to make changes as per your requirement.
(function () {
var tableName = "cmdb_ci_server";
var RESOURCE_PATH = "/api/now/table/" + tableName;
var request = new sn_ws.RESTMessageV2();
request.setEndpoint("https://<YOUR_PROD_INSTNCE>.service-now.com/"+RESOURCE_PATH );
request.setHttpMethod('GET');
request.setQueryParameter("sysparm_query", "name=ApplicationServerHelpdesk");
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);
//gs.print(JSON.stringify(requestBody));
//request.setRequestBody(JSON.stringify(requestBody));
request.setRequestHeader("Accept","application/json");
var response = request.execute();
var responseBody = response.getBody();
gs.print(responseBody);
var jsonResponse = JSON.parse(responseBody);
var responseResult = jsonResponse["result"];
for (var record in responseResult) {
var cmdbServer = new GlideRecord(tableName);
cmdbServer.newRecord();
var insertRecord = false;
var recordData = responseResult[record];
for (var data in recordData) {
var fieldName = "";
var fieldValue = "";
if (fieldName == "sys_id") {
cmdbServer.setNewGuidValue(fieldValue);
} else if (typeof recordData[data] == "object") {
fieldName = data;
fieldValue = recordData[data]["value"];
gs.print(fieldName + " : " + fieldValue);
} else {
fieldName = data;
fieldValue = recordData[data];
gs.print(fieldName + " : " + fieldValue);
}
if (fieldName && fieldValue) {
insertRecord = true;
cmdbServer.setValue(fieldName, fieldValue);
}
}
if (insertRecord) {
cmdbServer.insert()
}
}
})();
Please mark my respsone as helpful/correct, if it answer your question.
You can start with this and if you have any other question specific you your integration you can asked raise other question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 11:18 PM
Hello Feddy,
Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.
If you still need any further help or guidance on this then please update those on this question.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2022 01:24 AM
Hi Mahendra,
Thank you so much for your input ! I tried this in my personal instance and it worked fine .
is this script documented somewhere or you built this for your requirement?
I would like to know few things,
1. user record has many reference field on the form like company ,location.
What will happen if those records are missing in the lower instance? will it be empty?
2. Can we know if the record is successfully inserted or skipped with logs?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2022 06:41 AM
Hello Feddy,
Please find the below answers to your questions:
is this script documented somewhere or you built this for your requirement? -->
RESTMessageV2 - Scoped, Global
RESTResponseV2 - Scoped, Global
1. user record has many reference field on the form like company ,location.
What will happen if those records are missing in the lower instance? will it be empty? --> Those fields will not be empty but will store the sys_id of the record. If you see those in list view you will see them as empty. But if you right click and say show matching then it will show some sys_ids. So in this case if you export and import for example missing location record then you don't have to user the location on user record because it was storing the location record's sys_id once the location record is imported the relationship will be established.
2. Can we know if the record is successfully inserted or skipped with logs? --> you will get the response in return so you can read the response and you can find which record is inserted and which is not inserted.
I believe I have answered your question as it was related to an approach that can be used. So if you feel your question is answered, could you please do close this thread/question by marking the appropriate response as correct and helpful.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 02:05 AM
Hello
Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.
If you still need any further help or guidance on this then please update those on this question.
Thanks