Update method not working in Scheduled Job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 12:37 AM
I have a scheduled job written as -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 01:19 AM
Can I know what is the Field Type you have created on App server?
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 01:22 AM
It is of list type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 01:38 AM - edited 02-13-2024 01:42 AM
@surbhi_123 , on which table your writing this BR? and also to which table your referring on the List collector ?
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 01:50 AM
This is Scheduled job, I am GlideRecording on cmdb_ci_app_server table and setting the value of u_notice_domain which is of list type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2024 01:08 AM
The issue you're facing is likely due to the fact that you're trying to update the appSer record inside the while loop of another GlideRecord query. This is causing the appSer record to lose context and hence the update is not happening. Here's how you can fix it:
1. Create a new GlideRecord instance for updating the appSer record.
2. Move the update operation outside the inner while loop.
Here's the corrected code:
javascript
(function(){
var appSer=new GlideRecord('cmdb_ci_app_server');
appSer.addEncodedQuery('sys_idISNOTEMPTY');
appSer.query();
while(appSer.next()){
gs.log("sj run");
var sysId = 'b25bb2c7db38abc076b9ee71ca9619a5';
var gr = new GlideRecord('cmdb_rel_ci');
gr.addEncodedQuery('parent.sys_class_name=cmdb_ci_appl^child.sys_id='+sysId);
gr.query();
var arr = [];
while (gr.next()) {
var appSysID = gr.parent;
gs.log("app sys id" + appSysID);
var gp = new GlideRecord('cmdb_ci_appl');
gp.addQuery('sys_id', appSysID);
gp.query();
while (gp.next()) {
var x = gp.u_notice_domain.toString();
gs.log("x val:"+x);
x = x.split(',');
for (var i = 0; i < x.length; i++) {
arr.push(x[i]);
}
}
}
gs.log("arr length:"+arr.length);
var unique = [];
var seen = {};
for (i = 0; i < arr.length; i++) {
var val = arr[i];
if (!seen[val]) {
seen[val] = true;
unique.push(val);
}
}
gs.log("set unique" + unique);
var updateRecord = new GlideRecord('cmdb_ci_app_server');
if (updateRecord.get(appSer.sys_id)) {
updateRecord.u_notice_domain = unique.toString();
updateRecord.update();
}
}
})();
In this code:
- A new GlideRecord instance updateRecord is created to update the appSer record.
- The update operation is moved outside the inner while loop.
- The get method is used to fetch the current appSer record before updating it.
nowKB.com
For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/
For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER