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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

But why to move user/group records from production to non-prod? Is that a business requirement?

Usually we clone lower instances with production once go-live happens.

regards
Ankur

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

hi @Ankur Bawiskar 

User and Group records created in production and sometimes during functional test we need those newly created users or group records. We manually import them today whenever we face that situation. The ask here is to automate(with some interval or on demand) this so that the newly created or updated records will be imported in non-prod instance

Hi,

how do you know which users/groups to import?

Are they following some naming convention?

you can develop a schedule job in DEV and it will use Table API to create users, groups etc and then migrate this to TEST and PROD

This job will then automate the user/group migration in lower instances.

Regards
Ankur

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

Hi Ankur, 
Thank you for your inputs. 


Right now, new users and groups are only copied down to the lower environments when clones are done (which is about once a month) and this is not often enough. This pull needs to happen every day because so many new users and groups are added to our PROD instance and we receive a lot of requests to manually copy users/groups to the lower environments in between clones which is a tedious process. We would like to automate it.

Could you please share with me some docs or any inputs on this above?