For loop set to increment by one, increments by two

kristenankeny
Tera Guru

I have a script that is supposed to create more than one ticket called "Software install" when users select multiple "versions." However, we're seeing weird behavior and the for loop seems to be incrementing my variable by two instead of one. We have tried i++, i+=1, i = i + 1, but all result in i skipping from 0 to 2. These log statements show the issue. The first two lines at the bottom show that it finds two groups that it needs to create tickets for. Right after that, I log the length of the array and it finds 2. Right before the software ticket creation script, I log the length again and it's still two. Then, before I create the first ticket, it shows that i is 0. At the end of that same loop, i is suddenly showing 2 (before we exit the first loop, it never re-logs as if it's going to create a second ticket.:

DateType
MessageSource
8/29/2017 11:54InformationAppAccess i is 21504022091781 AppAccess
8/29/2017 11:54InformationAppAccess software ticket creation 3ce220e64f6d2640bd3328928110c7f9 and i is 01504022091473 AppAccess
8/29/2017 11:54InformationAppAccess - right before software ticket creation length is 2 1504022091472 AppAccess
8/29/2017 11:54InformationAppAccess groups list length is 21504022091121 AppAccess
8/29/2017 11:54InformationAppAccess Versions fulfillment group d467b01c4f06aa40bd3328928110c71d11504022091120 AppAccess
8/29/2017 11:54InformationAppAccess Versions fulfillment group 3ce220e64f6d2640bd3328928110c7f901504022091119 AppAccess

These are the relevant pieces of script:

var sg = []; //Software group

if(c.next()){

ag = c.u_access_fulfillment_group;

if(c.u_software == true){

s = 'yes';

//Is "fufillment" due to typo in 'cmdb_ci'

if(c.u_software_fufillment_group){ //If CI has a set software fulfillment group

sg.push(c.u_software_fufillment_group.toString());

}

else if(current.variables.version){ //If only one version, check for group

var ver = new GlideRecord('u_cmdb_ci_versions');

ver.addQuery('sys_id',current.variables.version);

ver.query();

ver.next();

sp = ver.u_software_push;

workflow.scratchpad.softwarePush = ver.u_software_push;

sg.push(ver.u_software_fulfillment_group.toString());

gs.log('AppAccess group is ' + sg.toString(),new GlideDateTime().getNumericValue() + ' ' + script_source);

if(sg == ''){

s = 'no';

}

secg = (current.variables.version.getValue('u_security_group'));

gs.log('AppAccess sp = ' + sp + ' and scratchpad push = ' + workflow.scratchpad.softwarePush, new GlideDateTime().getNumericValue() + ' ' + script_source);

}

else{ //Else, check all versions for groups

var versions = new GlideRecord('u_cmdb_ci_versions');

versions.addQuery('sys_id','IN',current.variables.list_version);

versions.query();

while(versions.next()){

gs.log('AppAccess Versions fulfillment group ' + versions.getValue('u_software_fulfillment_group') + sg.length,new GlideDateTime().getNumericValue() + ' ' + script_source);

//Is "fulfillment", no typo in 'u_cmdb_ci_versions'

sg.push(versions.getValue('u_software_fulfillment_group')+' ');

//I have also tried to push without the + ' '

}

sg = new ArrayUtil().unique(sg);

gs.log('AppAccess groups list length is ' + sg.length,new GlideDateTime().getNumericValue() + ' ' + script_source);

}

}

}

//Create software task

gs.log('AppAccess - right before software ticket creation length is ' + sg.length,new GlideDateTime().getNumericValue() + ' ' + script_source);

if(s == 'yes' && (current.variables.action != 'delete' && current.variables.action != 'password_unlock') && sp == false){

for(i = 0; i < sg.length; i+=1){

gs.log('AppAccess software ticket creation ' + sg[i] + ' and i is ' + i,new GlideDateTime().getNumericValue() + ' ' + script_source);

var srt = new GlideRecord('sc_task');

srt.newRecord();

srt.assignment_group = sg[i];

srt.short_description = aclab + ' Software Needed';

srt.description = current.comments_and_work_notes;

srt.request_item = current.sys_id;

srt.order = 2;

srt.priority = 3;

srt.u_requested_for = current.u_requested_for;

srt.parent = current.sys_id;

srt.location = current.location;

srt.u_office_cube = current.u_office_cube;

srt.u_phone = current.u_phone;

srt.due_date = current.due_date;

srt.state = -5;

srt.u_pending_reason = 'awaiting_previous_task';

srt.insert();

gs.log('AppAccess i is ' + i,new GlideDateTime().getNumericValue() + ' ' + script_source);

}

}

Unfortunately, HI Help Desk hasn't been able to provide assistance yet and as this is a high audit process, I need to get the fix in place as soon as possible. The odd thing is that the loop worked correctly and then suddenly didn't and no changes were made to cause it to suddenly behave differently.

1 ACCEPTED SOLUTION

Hi Sanjiv,



ServiceNow's HI team has my workflow checked out, but I grabbed the script, stuck it in a background script with changes so it didn't reference workflow and current, and reverted it to my original script to match your suggestions and I just commented out the log (the comma wasn't to join, I'm defining two separate parameters to pass to the log function - the message (before the comma) and the source (after the comma)). It did not resolve the issue.



However, changing i to a random string as a variable name somehow fixed it (despite not using i as a variable anywhere else in that script that I can see). Sometimes ServiceNow quirks are enough to drive me nuts; especially since the workflow worked as expect with i as the variable and then just suddenly didn't work without any changes to code from me. Is it possible ServiceNow got confused with maybe another script that might also use i?


View solution in original post

7 REPLIES 7

This might be to do with variable assignment vs declaration in your script (i.e. using i=0 rather than var i=0). Other scripts running in the platform using variable 'i' (the most common name for a loop index variable) may be changing your value as it is being declared in a global sense.

 

This would explain why changing the variable name fixed the issue.

 

The following community article explains this very well:https://www.servicenow.com/community/developer-forum/why-declaration-of-variables-is-essential-in-se...

Mark Endsley
Tera Guru

To anyone reading this.

...Please I beg you if you know the reason that this happens please tell me!!!

Until I know why, I will see the logic of the world as fractured...

Down is up, up is sideways. 

 

IS THE UNIVERSE EVEN REAL?

With help from Reddit I found the answer.

https://www.reddit.com/r/servicenow/comments/q8w72u/logic_breaking_thing_in_servicenow_background/