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

Dear Charanraj,



Can you post the error message again.. I dont see it in the earlier comment.



Thanks


Anil


Hi Anil,



attached the error message files


Thanks,


charan


Dear Charanraj,



Please use this updated script



var tableName = 'sys_user';


var uniqueValue = 'sys_id'; // Provide Unique Value


var query = "sys_updated_on%3E%3Djavascript%3Ags.beginningOfLast3Months()";


var instanceUrl = 'https://dev38250.service-now.com/';


var restApiName = 'KingstonInstance';


var restApiMethodName = 'getUsers';


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()) {



}


}



Thanks


Anil


Hi Anil,



thanks for new updated script i will check and update on this.


small change condition whenever new user or group is created production automatically need to created in the development instance.




Thanks,


Charan


in order to achieve that you need to create a business rule in production and trigger a script that creates a record in dev instance. Pls let me know if you need help with the script.