Script is not working, Not entering For Loop

Saravanakumar1
Tera Contributor

Hi All,

I have written the below script in workflow to get the variable value from Service Request form and update in application table but my script is not working especially the for loop part, The log I have given inside forloop is not getting logged

var ci = current.variables.u_appl_ci;

var i = 0;

var g = current.variables;

var sr = [g.u_new_version,g.u_new_critical,g.u_new_category,g.u_new_subcategory,g.u_new_managed,g.u_new_group,g.u_new_status,g.u_new_vendor,g.u_new_date,g.u_new_owner,g.u_new_licensed,g.u_new_portfolio,g.u_new_description];

gs.log('r003411 1 '+sr[0]+sr[2]);

var gR = new GlideRecord('cmdb_ci_appl');

gR.addQuery('sys_id',ci);

gR.query();

if(gR.next()){

      gs.log('r003411 IN 2 '+sr[0]+sr[2]);

      var app = [gR.version,gR.u_business_criticality,gR.u_application_category,gR.u_application_subcategory,gR.managed_by,gR.u_tech_support_group,gR.u_application_status,gR.vendor,gR.u_support_end_date,gR.u_business_owner,gR.u_licensed,gR.u_service_portfolio,gR.short_description];

      gs.log('r003411 app 3 '+app[0]+app[12]);

      for (i = 0; i < app.length; i++)

              {

              gs.log('roo3411 for 4 '+app[i]);      

              if(sr[i] != '')

                      {

                      gs.log('roo3411 forsr 5 '+sr[i]);

                      app[i] = sr[i];

              }

      }

      gR.update();

}

5 REPLIES 5

tritaku
Kilo Explorer

If this isn't entering your for loop, it looks like your gR.query() is returning 0 results which is causing it to skip your if statement which contains the for loop.


I'd double check if your variable u_appl_ci actually matches the sys ids on your cmdb_ci_appl table


Hi Andrew,



Thank you for your response!



I believe gR.query() is working because I am getting correct result for the log 'gs.log('r003411 app 3 '+app[0]+app[12]);' which is inside if(gR.next()) loop.



Regards,


Saravanakumar S


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Saravanakumar,



I think the array you defined is not proper i.e. the app array.


you need to define the array elements as following:



var app = ['gr.version','gr.short_description'];



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Thank you for your response!



When I ran the script in the background to check the value in the array, I got all the values printed correctly.



Regards,


Saravanakumar S