How can replicate a record from one instance to another via rest integration but sys_id should be same between instance.

Chandra18
Mega Sage

Hello expert,

I am not expected like this from community, I am trying to find resolution from last week but not satisficed.

How can replicate a record from one instance to another via rest integration but sys_id should be same between instance. 

Thank you in advanced! 

6 REPLIES 6

Kieran Anson
Kilo Patron

Hi Chandra,

In order to get relevant engagement from the community, you need to respond to your questions. Looking at your previous post How can I integrate one record from a table to another instance in the same table with same sys_Id. ... there are valid answers there to which you haven't responded. Starting a new question is counterproductive.

Mahendra RC
Mega Sage

Hello Chandra,

This is a similar question raised earlier How can I integrate one record from a table to another instance in the same table with same sys_Id. I am provided sample script that can be used as shown below.

************************************************************************************************************

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. 

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

Hello Chandra,

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

Hello Mahendra,

I have checked it is not working, maybe I have missed something.
Could you please change the code for Incident table(Incident should be created from prod to DEV with same sys_id with dynamic data/Value for variables).


Thanks