Import User and group record from prod to non-prod instance

Feddy
Kilo Sage

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

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

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 instancetableName 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

View solution in original post

16 REPLIES 16

@Mahendra

Hi Mahendra, 

Currently the job is pulling newly created record(created on today) , and we would like to know if the same record is updated in production, how can I update the same in non-prod instance?

Thanks, 
Fedrick

 

 

Cent
Tera Contributor

Hi @Feddy ,

 

does the solution answer or satisfy your requirements? how did you achieve this what configuration have you done to sync users/groups/roles from PROD to dev instance? thank you