Reading all GlideRecord results from a query.

michaelswartz
Giga Contributor

Hello I am trying to loop thorough and write out the GlideRecord query results. Why for what ever reason will the GlideRecord will not increment but continues to repeat the  values of the first result?

I am working this as a Advanced Condition for a Notification. I have this set up to run as a fixed script so I can see my out put quickly.

Issue I think is here: The while pulls the first result and will write it twice, the for variant writes the second result twice. Wish "for each" was an option. 

		while(changeTaskRecord.next())
		{		
			taskRecords.push(changeTaskRecord);
		}		
// 		for(var recordCount = 0; recordCount < changeTaskRecord.getRowCount(); recordCount++)
// 		{
// 			taskRecords.push(changeTaskRecord);
// 			changeTaskRecord.next();
// 		}

Here is the Fixed Script/AdvanceCondition script. 

var changeRecordId="A CHANGE NUMBER";
	
	var eventLog = "";

	var changeRecord = new GlideRecord("change_request");
	changeRecord.addQuery('number', '=', changeRecordId);
	changeRecord.query();
	
// 		eventLog += "Change Record:\n";
// 		var fields = changeRecord.getFields();
// 		for (var i = 0; i < fields.size(); i++) { 
// 			var field = fields.get(i);
// 			var name = field.getName(); 
// 			var value = field.getDisplayValue(); 
// 			eventLog += (i + ". " + name + "=" + value + "\n"); 
// 		}
// 		eventLog += "\n";		
//		eventLog += "Change Record Id: " + changeRecord.number.getDisplayValue() + "\n";	
	
	if (changeRecord.next())
	{	
		var changeTaskRecord = new GlideRecord("change_task");
		changeTaskRecord.addQuery('parent.number', '=', changeRecord.number.getDisplayValue());	
		changeTaskRecord.query();
		
		eventLog += "Task Query Record Count: " + changeTaskRecord.getRowCount() + "\n";	
		var taskRecords = [];
		while(changeTaskRecord.next())
		{		
			taskRecords.push(changeTaskRecord);
		}		
// 		for(var recordCount = 0; recordCount < changeTaskRecord.getRowCount(); recordCount++)
// 		{
// 			taskRecords.push(changeTaskRecord);
// 			changeTaskRecord.next();
// 		}
		
		eventLog += "Task Array Record Count: " + taskRecords.length + "\n";		
		if(taskRecords.length == 0)
		{
			eventLog += "Unable to retrieve Task Records.\n";
			answer = false;			
		}
		else
		{
			var hasSanitizeTasks = 0;
			for(var recordCount = 0; recordCount < taskRecords.length; recordCount++)
			{
				eventLog += "Task Record: " + recordCount + "\n";
				eventLog += "Task Name: " + taskRecords[recordCount].getElement('u_name') + "\n";			
				eventLog += "Index of Sanitize: " + taskRecords[recordCount].getElement('u_name').indexOf("Sanitize") + "\n";
				
				var fields1 = taskRecords[recordCount].getFields();
				for (var i1 = 0; i1 < fields1.size(); i1++) 
				{ 
					var field1 = fields1.get(i1);
					var name1 = field1.getName(); 
					var value1 = field1.getDisplayValue(); 
					eventLog += (i1 + ". " + name1 + "=" + value1 + "\n"); 
				}
				eventLog += "\n";					
				
				if(taskRecords[recordCount].getElement('u_name').indexOf("Sanitize") > -1)
				{				
					hasSanitizeTasks++;	
				}
			}
			eventLog += "Has Sanitize Tasks:" + hasSanitizeTasks + "\n";
		
			var changeRecordEndDate = changeRecord.end_date.getGlideObject();
			var SanitizeGracePeriod = new GlideDateTime(changeRecordEndDate);
			SanitizeGracePeriod.addDays(30);
			
			eventLog += "Change Record End Date:" + changeRecordEndDate + "\n";
			eventLog += "Sanitize Grace Period:" + SanitizeGracePeriod + "\n";
			eventLog += "Current Time:" + gs.nowDateTime().toString() + "\n";	
			
			if (hasSanitizeTasks > 0 && SanitizeGracePeriod < gs.nowDateTime())
			{
				eventLog += "Option1: Change Task contains 'Sanitize': True\n";			
				answer = true;
			}
			else if(hasSanitizeTasks == 0)
			{			
				eventLog += "Option2: Change Task contains 'Sanitize': False\n";
				answer = true;
			}		
			else
			{
				eventLog += "Option3: Change Task contains 'Sanitize': True & GracePeriod is > Today \n";
				answer = false;				
			}		
		}
		eventLog += "Has Sanitize Tasks:" + hasSanitizeTasks + "\n";
		
		var changeRecordEndDate = changeRecord.end_date.getGlideObject();
		var SanitizeGracePeriod = new GlideDateTime(changeRecordEndDate);
		SanitizeGracePeriod.addDays(30);
		
 		eventLog += "Change Record End Date:" + changeRecordEndDate + "\n";
 		eventLog += "Sanitize Grace Period:" + SanitizeGracePeriod + "\n";
 		eventLog += "Current Time:" + gs.nowDateTime().toString() + "\n";	
	}
	gs.print(eventLog);

 

1 ACCEPTED SOLUTION

michaelswartz
Giga Contributor

So here is what I arrived at. This is code as it appears on the Notification Advanced Condition.

Commented out all the logging line I used for troubleshooting. For whatever reason when I left them in the email that this condition fires will not send. When they are commented the email sends fine.

I can just take this code and with a small edit to the change Id I can run it as a fixed script if I encounter any errors in the future.
(fixed script variant is in the code snippet on original post.)

Thanks to those who responded.

//var eventLog = "Change Implementation Past End Date:\n";

var changeRecordId;
var taskRecord = new GlideRecord("task_sla");
taskRecord.addQuery('sys_id', event.parm1.toString());
taskRecord.query();

if (taskRecord.next())
{
	changeRecordId = taskRecord.task.sys_id.getDisplayValue();

	var changeRecord = new GlideRecord("change_request");
	changeRecord.addQuery('sys_id', 'CONTAINS', changeRecordId);
	changeRecord.query();
	
// 		eventLog += "Change Record:\n";
// 		var fields = changeRecord.getFields();
// 		for (var i = 0; i < fields.size(); i++) { 
// 			var field = fields.get(i);
// 			var name = field.getName(); 
// 			var value = field.getDisplayValue(); 
// 			eventLog += (i + ". " + name + "=" + value + "\n"); 
// 		}
// 		eventLog += "\n";		
//		eventLog += "Change Record Id: " + changeRecord.number.getDisplayValue() + "\n";	
	
	if (changeRecord.next())
	{	
		var changeTaskRecord = new GlideRecord("change_task");
		changeTaskRecord.addQuery('parent.number', '=', changeRecord.number.getDisplayValue());	
		changeTaskRecord.query();
		
//		eventLog += "Task Query Record Count: " + changeTaskRecord.getRowCount() + "\n";	
		
		var hasSanitizeTasks = 0;
		while(changeTaskRecord.next())
		{			
			if(changeTaskRecord.getRowCount() == 0)
			{
//				eventLog += "Unable to retrieve Task Records.\n";
				answer = false;			
			}
			else
			{
// 				eventLog += "Task Name: " + changeTaskRecord.getElement('u_name') + "\n";			
// 				eventLog += "Index of Sanitize: " + changeTaskRecord.getElement('u_name').indexOf("Sanitize") + "\n";
				
// 				var fields1 = changeTaskRecord.getFields();
// 				for (var i1 = 0; i1 < fields1.size(); i1++) 
// 				{ 
// 					var field1 = fields1.get(i1);
// 					var name1 = field1.getName(); 
// 					var value1 = field1.getDisplayValue(); 
// 					eventLog += (i1 + ". " + name1 + "=" + value1 + "\n"); 
// 				}
// 				eventLog += "\n";					
				
				if(changeTaskRecord.getElement('u_name').indexOf("Sanitize") > -1)
				{				
					hasSanitizeTasks++;	
				}		
			}			
		}

//		eventLog += "Has Sanitize Tasks:" + hasSanitizeTasks + "\n";
	
		var changeRecordEndDate = changeRecord.end_date.getGlideObject();
		var SanitizeGracePeriod = new GlideDateTime(changeRecordEndDate);
		SanitizeGracePeriod.addDays(30);
		
//		eventLog += "Change Record End Date:" + changeRecordEndDate + "\n";
//		eventLog += "Sanitize Grace Period:" + SanitizeGracePeriod + "\n";
//		eventLog += "Current Time:" + gs.nowDateTime().toString() + "\n";	
		
		if (hasSanitizeTasks > 0 && SanitizeGracePeriod < gs.nowDateTime())
		{
//			eventLog += "Option1: Change Task contains 'Sanitize': True\n";			
			answer = true;
		}
		else if(hasSanitizeTasks == 0)
		{			
//			eventLog += "Option2: Change Task contains 'Sanitize': False\n";
			answer = true;
		}		
		else
		{
//			eventLog += "Option3: Change Task contains 'Sanitize': True & GracePeriod is > Today \n";
			answer = false;				
		}			
	}
	else
	{
//		eventLog += "Cannot Retrieve Change Record." + changeRecord.number.getDisplayValue() + "\n";
		answer = false;	
	}	
}
else
{
// 	eventLog += "Cannot Retrieve Task SLA Record." + event.parm1.toString() + "\n";
	answer = false;	
}	

//gs.log(eventLog);

 

View solution in original post

10 REPLIES 10

Bhagyashree8
Kilo Guru

Hi,

 

Below lines will not work. You can not push glide record inside array and use it later. it is only a pointer.

 

while(changeTaskRecord.next()) {

taskRecords.push(changeTaskRecord);

}

 

Instead write your logic inside that while loop.

 

Mark my ANSWER as CORRECT / HELPFUL if it served your purpose.

Actually, it does save the record into that array.

Later in the code I use the taskRecords array to write out all the fields and values of the Record?

So not sure how saying it won't work is a valid response? It will, the Gliderecord results just won't increment.

so in your script can please highlight which part you think is not working?

its either the push of the gliderecords into the arrary

	        while(changeTaskRecord.next())
		{		
			taskRecords.push(changeTaskRecord);
		}		
// 		for(var recordCount = 0; recordCount < changeTaskRecord.getRowCount(); recordCount++)
// 		{
// 			taskRecords.push(changeTaskRecord);
// 			changeTaskRecord.next();
// 		}


or the read out. In either case the array or the gliderecord does not increment as expected

for(var recordCount = 0; recordCount < taskRecords.length; recordCount++)
{
	eventLog += "Task Record: " + recordCount + "\n";
	eventLog += "Task Name: " + taskRecords[recordCount].getElement('u_name') + "\n";			
	eventLog += "Index of Sanitize: " + taskRecords[recordCount].getElement('u_name').indexOf("Sanitize") + "\n";
			
	var fields1 = taskRecords[recordCount].getFields();
	for (var i1 = 0; i1 < fields1.size(); i1++) 
	{ 
		var field1 = fields1.get(i1);
		var name1 = field1.getName(); 
		var value1 = field1.getDisplayValue(); 
		eventLog += (i1 + ". " + name1 + "=" + value1 + "\n"); 
	}
	eventLog += "\n";					
			
				
        if(taskRecords[recordCount].getElement('u_name').indexOf("Sanitize") > -1)
        {				
	        hasSanitizeTasks++;	
        }
}