- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 09:27 AM
Hi All,
When ever in production instance newly record created or updated extisting record in ( user table or group table ) it should be copied in lower instance
Which mean replica of data when new record created or existing record got updated those change move automativally prod to our lower instance.
Below script in working for newly created records but not updating the existing recoed. please suggest below script should update the existing records as well
Below url is different i have provided for example .
(function () { var tableName = "sys_user"; 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", "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_updated_onONLast 2 hours@javascript:gs.beginningOfLast2Hours()@javascript:gs.endOfLast2Hours()"); //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(sys_user); 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 suggest.
Thanks,
Varma
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 09:09 AM
Hi @AnveshKumar M ,
Thanks you so much your support. I run the job No logs are captured.
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"];
var recordData = responseResult[record];
var usrGr = new GlideRecord('sys_user');
if(usrGr.get('sys_id', recordData.sys_id)){
try {
new GlideQuery('sys_user')
.where('sys_id', recordData.sys_id)
.update(recordData);
} catch (ex) {
gs.info('VARMA GQ UPDATE ERROR: ' + ex);
}
}else{
try {
new GlideQuery('sys_user')
.insert(recordData);
} catch (ex) {
gs.info('VARMA GQ INSERT ERROR: ' + ex);
}
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 12:28 AM
Hi @varma2 .
The line number 7 should look like the one in the green box shown below. See the difference (Under lined with red line) between the two lines in the image.
It should work if you place the line 7 as shown in the green box. If it didn't check whether the API is returning any records or not by placing gs.info() messages like,
gs.info('User records returned from higher instance : ' + responseResult.length);
And, observe the system log. If you see any valid number of records in the log, then we have to look at the for loop block of code.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 01:17 AM
Hi @AnveshKumar M ,
I have implimented and run the job its captured in logsbelow is the screenshot.
Please suggest.
Thanks,
Varma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 02:00 AM
@varma2 ,
So the query is working fine, can you share the JSON format for any one record? You can mask the sensitive information and just share the structure. If you want to capture the JSON I can suggest using the following line just before the second for loop and monitor the log.
gs.info('User record JSON : ' + JSON.stringify(recordData) );
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 02:21 AM
Hi @AnveshKumar M ,
Thank you so much on taking time on this. Below is the JSON format from logs.
Thanks,
Varma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 02:43 AM
Hi @varma2 ,
I found an issue in my script, I forgot to enclose table name in single quotes, change the line next to the first for loop and run the script.
var cmdbServer = new GlideRecord('sys_user');
Anvesh