issue with nested loop

johnrecz
Giga Contributor

Hi All,

I've created a Morning Checklist application that we have a staff member fill out that automatically creates incidents based on the information that is input. It was all good until they wanted me to make it create a new incident per new line in the input data and also have it check previous incidents and not create the incident if there is already an active ticket with that description.

So basically it should work like this for any of the given fields.
Day 1:

Inc 1 - new incident created
Inc 2 - new incident created

Day 2:
Inc 1 - no new incident created

Inc 3 - new incident created

This is what I've got so far and it appears to work some of the time but I do not have the skill set required to figure out why:

inc = new GlideRecord('incident');

newinc = new GlideRecord('incident');

checklist = new GlideRecord('morning_checklist_table');

checklist.orderByDesc('sys_created_on');

checklist.chooseWindow(0,1);

checklist.query();

caller_id = checklist.getValue('sys_created_by');

inccount = 0;

if (checklist.next()) {

  a = checklist.getValue('alertmb_info');

  if (checklist.getValue("alertmb") != 'OK'){

            s = a.toString();

            sArray = s.split("\n");

            inc.addEncodedQuery('category=Morning Checklist^subcategory=Alerts Mailbox^state!=7^ORstate!=6');

            for (count=0; count < sArray.length; count++) {

                      inc.query();

                      while(inc.next()){

                                b = inc.getValue('short_description');

                                if (sArray[count].toString() == b.toString()) {

                                          inccount = inccount +1;

                                }

                      }

                      if (inccount < 1){

                                newinc.newRecord();

                                newinc.initialize();

                                newinc.short_description = sArray[count];

                                newinc.assignment_group.setDisplayValue('Sys Admin');

                                newinc.category = "Morning Checklist";

                                newinc.subcategory = "Alerts Mailbox";

                                newinc.urgency = 3;

                                newinc.priority = 3;

                                newinc.callerid = caller_id;

                                newinc.update();

                      }

            inccount = 0;

            }

  }

  <The other categories below before closing the loop>

1 ACCEPTED SOLUTION

Bryan Tay3
Mega Guru

hi john,



i think u can try the following code instead:



checklist = new GlideRecord('morning_checklist_table');  


checklist.orderByDesc('sys_created_on');  


checklist.chooseWindow(0,1);  


checklist.query();  


caller_id = checklist.getValue('sys_created_by');    


 


if (checklist.next()) {  


  a = checklist.getValue('alertmb_info');  


  if (checklist.getValue("alertmb") != 'OK'){  


            s = a.toString();  


            sArray = s.split("\n");  


            for (count=0; count < sArray.length; count++)


            {  


  var inc = new GlideRecord('incident');  


                inc.addEncodedQuery('category=Morning Checklist^subcategory=Alerts Mailbox^state!=7^ORstate!=6');  


                inc.addquery('short_description','=',sArray[count].toString());


  inc.query();



  if(inc.getRowCount()==0)


  {


  var newinc = new GlideRecord('incident');


  newinc.newRecord();  


  newinc.initialize();  


  newinc.short_description = sArray[count];  


  newinc.assignment_group.setDisplayValue('Sys Admin');  


  newinc.category = "Morning Checklist";  


  newinc.subcategory = "Alerts Mailbox";  


  newinc.urgency = 3;  


  newinc.priority = 3;  


  newinc.callerid = caller_id;  


  newinc.update();  


  }  


            }  


  }  


View solution in original post

4 REPLIES 4

Bryan Tay3
Mega Guru

hi john,



i think u can try the following code instead:



checklist = new GlideRecord('morning_checklist_table');  


checklist.orderByDesc('sys_created_on');  


checklist.chooseWindow(0,1);  


checklist.query();  


caller_id = checklist.getValue('sys_created_by');    


 


if (checklist.next()) {  


  a = checklist.getValue('alertmb_info');  


  if (checklist.getValue("alertmb") != 'OK'){  


            s = a.toString();  


            sArray = s.split("\n");  


            for (count=0; count < sArray.length; count++)


            {  


  var inc = new GlideRecord('incident');  


                inc.addEncodedQuery('category=Morning Checklist^subcategory=Alerts Mailbox^state!=7^ORstate!=6');  


                inc.addquery('short_description','=',sArray[count].toString());


  inc.query();



  if(inc.getRowCount()==0)


  {


  var newinc = new GlideRecord('incident');


  newinc.newRecord();  


  newinc.initialize();  


  newinc.short_description = sArray[count];  


  newinc.assignment_group.setDisplayValue('Sys Admin');  


  newinc.category = "Morning Checklist";  


  newinc.subcategory = "Alerts Mailbox";  


  newinc.urgency = 3;  


  newinc.priority = 3;  


  newinc.callerid = caller_id;  


  newinc.update();  


  }  


            }  


  }  


I never even thought of trying to do the matching inside the query!



It is however throwing up errors for me whilst testing the code snippet as a background script:
Evaluator: org.mozilla.javascript.EcmaError: Cannot find function addquery in object [object GlideRecord].


Caused by error in script at line 31


==> 31: inc.addquery('short_description','=',sArray[count].toString());



I also tried to switch the '=' to 'IN' as documentation seemed to indicate that '=' was more for numerical data: http://wiki.servicenow.com/index.php?title=Using_GlideRecord_to_Query_Tables#Available_JavaScript_Op...
And also dropping the .toString() as I think the data should already be in string format from earlier but still same error message.



I'm doing a search into what this error message is but any ideas on how to solve would be great!


hi john,



it should be function 'addQuery' with a capital Q.


let me know if it works.


Oh wow it's definitely Friday afternoon!



That looks like it's working!



Thank you very much good sir!