Update method not working in Scheduled Job

surbhi_123
Tera Expert

I have a scheduled job written as -

(function(){
    var appSer=new GlideRecord('cmdb_ci_app_server');
    appSer.addEncodedQuery('sys_idISNOTEMPTY');
    appSer.query();
    while(appSer.next()){
    gs.log("sj run");
    //var sysId =appSer.sys_id;
    var gr = new GlideRecord('cmdb_rel_ci');
 
    var sysId = 'b25bb2c7db38abc076b9ee71ca9619a5';
   
    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);
   
    appSer.u_notice_domain = unique.toString();
 
    appSer.update();
    }
   
})();
 
Here in logs value I am getting the unique value but it is not being set to the u_notice_field name.
 
10 REPLIES 10

Can I know what is the Field Type you have created on App server?

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

It is of list type

@surbhi_123 , on which table your writing this BR? and also to which table your referring on the List collector ?

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

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 

Rajdeep Ganguly
Mega Guru


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