Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

SANDEEP28
Mega Sage

@surbhi_123 Are you using correct field in below line of code. You said value is not getting set in "u_notice_field" field  but you have used "u_notice_domain" field in below line of code.

 

 appSer.u_notice_domain = unique.toString();

 

I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

The field name is  - u_notice_domain

shyamkumar VK
Kilo Patron

 

@surbhi_123  , Can you try with this Update Script?

(function(){
    var appSer=new GlideRecord('cmdb_ci_app_server');
    appSer.addEncodedQuery('sys_idISNOTEMPTY');
    appSer.query();
    while(appSer.next()){
        gs.log("sj run");
        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 (var 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.join(','); 
        appSer.update(); 
    }
})();

 

 

Regards,

Shyamkumar

 

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

Shyamkumar

In the logs of set unique I am getting value as - "set unique bbbc1cce0f1fd200b11cf68ce1050e2e,700d148e0f1fd200b11cf68ce1050e10,603d1cce0f1fd200b11cf68ce1050e1e,511d1cce0f1fd200b11cf68ce1050e1d"

And using unique.join(,) it is not updating