How do I iterate through a loop and also query the same table

gnewuser
Tera Contributor

Hi, I have a table  A and I query all the reocrds and I have 2 fields in the table that I need to get for example "numberofdays1" and "numberofdays2".

 

Once I get that I add the "numberofdays1" and "numberofdays2". But once I get this "totalnumberofdays", I need to query the same table A and find a field called  "Enddate"(which is a date field) and add the following condition:

 

get all records where the "Enddate" is within the  "totalnumberofdays" .

 

Now I am confused as to how do I code this.

 

 

var grA = new GlideRecord("A");
grA.Query(); 

var totalnumberofdays;

while (grA .next()){
 
totalnumberofdays= grA .numberofdays1+ grA .numberofdays1;
}

Now inside this loop, how do I query the same table "A", and find all the records where the "Enddate" is within a "totalnumberfdays"

1 ACCEPTED SOLUTION

Gaurav Shirsat
Mega Sage

@gnewuser 

from your question I did not get how you are doing this I am not sure, if you are doing this using Business Rule, I will request you to create 2 separate functions in Script Include and call one by one here. also you can provide input query parameter to script include too.

Call the First Function which will give you the counting of your total number of days. same thing doing in business rule is also Ok. just pass this result as input query to script include.

var grQuery = new GlideRecord("A");
grQuery .query();
var days = 0;
while (grQuery .next()) {{

days += parseInt(grA.numberofdays1) + parseInt(grA.numberofdays2);
}

break;

}

Now you just have to pass this result to your script include, in which you can write below code

reassignWork: function(myDay) {
var aTable = new GlideRecord('atable');
atable.EncodedQuery("");
aTable.query();
if (aTable.next()) {
//your work

return variable_name

}
},

refer below screenshots and links how you can call script include to Business Rule.

BR.png

SI.png

https://www.basicoservicenowlearning.in/2019/12/script-include-servicenow.html#:~:text=To%20use%20sc... 

https://www.servicenow.com/community/csm-forum/how-to-call-script-include-from-business-rule/m-p/398... 

Mark my Response as Correct or Helpful, if you find it Appropriate.
Gaurav Shirsat : ServiceNow Community MVP 2022
https://www.linkedin.com/in/gauravshirsat/

 

 

 

View solution in original post

5 REPLIES 5

gnewuser
Tera Contributor

Another requirement in the job above. I have a table which has 2 integerfields which is Numberofdates1 and NumberofDates2. I also have the enddate(date field). I need to send the following notifications based on the conditions

1 . Notification1 -> gets triggered when end date(Dec 31,2023) is within Numberofdates1 + Numberofdates2. So if Numberofdates1 is 20 and Numberofdates2 is 30 , I need to find if the total is within the totalnumberofdates (50 days)

2. Notification2 -> gets triggered once every month till end date(until Nov 30,2023)

3. Notification 3-> gets triggered once every week from Nov 30 to Dec 30

If the receiver of the email responds and a request is created then no need to send the subsequent notifications.

I think I may need some help with scripting the condition for Notification2 and Notification 3.

 

Any suggestions?

 

Thanks in advance

3.