how to sync user,group and role Production To Dev Instance

cha08
Kilo Explorer

Hi All,

I'm looking sync user , group and roles from Production and QA.

Because daily creating new user adding groups and roles in the production again need to re-create in the development instance.

whether it can be automated task using the script by scheduling job/business script running the script weekly or monthly and also

user will be created specific domain or company.

Thanks in advance.

Regards,

Charan

27 REPLIES 27

Hi Anil,



please can you help me with script and business rule also need to automatically create user in the development along with role,group,group roles and user role need mapped users in the development.to make sure that domain and company information properly mapped.



thanks in advance



thanks,


charan


Anil,

Set all this up to sync the sys_user_grpmember table.  Set it up initially to pull from Dev to Test.  I commented out the insert statement and un-commented out the log statement:

gs.print(j + "=" + jsonPayload.result[0][j]);

 

When I look at the output I see this:

sys_id=0012d9e1db086bc493c09cd5ca961966
*** Script: sys_updated_by=OktaProvisioning
*** Script: sys_created_on=2018-08-20 16:25:34
*** Script: sys_mod_count=0
*** Script: sys_updated_on=2018-08-20 16:25:34
*** Script: sys_tags=
*** Script: user=[object Object]
*** Script: sys_created_by=OktaProvisioning
*** Script: group=[object Object]

My group membership is not getting update and I'm wondering if it's not getting the actual group.

It has been a while since I built this, please let me know if you are stuck, we can talk and I can help.

I am stuck.  It identifies that I am pulling back 1 new record in the sys_user_grmember table, but it doesn't perform the last step to insert.  I've tried both versions of your script:

var tableName         = 'sys_user_grmember';
var uniqueValue       = 'sys_id'; // Provide Unique Value 
var query             = "sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";
var instanceUrl       = ''https://<my_instance>.service-now.com/';
var restApiName       = 'DevInstance';
var restApiMethodName = 'getGroupMembers';

copyRecords(tableName, query,instanceUrl, restApiName, restApiMethodName, uniqueValue);

function copyRecords(tableName, query, instanceUrl, restApiName, restApiMethodName, uniqueValue) {
	// if(uniqueValue == undefined) {
	// //uniqueValue = getUniqueValue(tableName);
	// }
	var sDebug      = '';
	var endPointUrl = instanceUrl+ 'api/now/table/' + tableName + '?sysparm_query=' + query;
	sDebug += 'endPointUrl = ' + endPointUrl;
	sDebug +='\n' + 'restApiName = ' + restApiName;
	sDebug += '\n' + 'restApiMethodName = ' + restApiMethodName;
	var r   = new sn_ws.RESTMessageV2(restApiName, restApiMethodName);
	r.setEndpoint(endPointUrl);
	var response     = r.execute();
	var responseBody = response.getBody();
	var jsonPayload  = JSON.parse(responseBody);
	var headers      = response.getHeaders();
	var rowCount     = headers['X-Total-Count'];
	sDebug += '\n' + 'Row Count = ' + rowCount;
	for (var i=0; i< rowCount; i++) {
		var grTable = new GlideRecord(tableName);
		grTable.get(jsonPayload.result[i][uniqueValue]);
		if(!grTable.isValid()) {
			grTable.newRecord();
			for (var j in jsonPayload.result[i]) {
				gs.print(j +"=" + jsonPayload.result[0][j]);
				grTable[j] = jsonPayload.result[i][j] + '';
			}
			
			try {
				grTable.insert();
			}
			catch(e) {
				gs.log("invalid insert");
			}
		}
	}
	gs.print(sDebug);
}
function getUniqueValue(tableName) {
	var query ='unique=true^name=' + tableName;
	var grDict = new GlideRecord('sys_dictionary');
	grDict.addEncodedQuery(query);
	grDict.query();
	if(grDict.next()) {
	}
}

Hi Anil,

Hope you are doing good. Your work is really appreciated.

I think its a very older thread, but I got the similar requirement now. I tried the fix script provided by you for the Groups and it is working absolutely fine. 

Likewise the ask is whatever the user profiles are being created in the Production instance, the users will have Groups added and roles will be inherited/added to the user profiles from the Groups. 

I seen you mentioned we can achieve this functionality through Business rule. So whatever the user profiles are created in production instance, automatically all the user profiles should be created in Dev instance as well along with Groups and Roles parallelly.

I really appreciate your help. Can this be achieved through only 1 fix script or 1 Rest API or 1 business rule. Please build the logic and help us so that it will be helpful for everyone whoever is referring this thread.

 

Regards,

Afzal basha