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

Yeah..i am telling you again, you should not push glide record into an array. It will result in weird behavior.


Please write your logic inside while loop for each record.

Let me know why you are not writing your logic inside while loop.

 

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

Abhinay Erra
Giga Sage

Why are you pushing the changetaskrecords twice in to your array taskRecors

 

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

was two different methods of performing the push one is commented out at present. just left it there as reference.

Javier Arroyo
Kilo Guru
Abandon the current approach. Loop through GlideRecords and extract the fields needed into an Object. Then read from the object. The case of repeat values tends to be that the values being extracted hsven't been turned into a some form of JavaScript literal. In your case, Verify that you indeed can read the GlideRecords from the array without hiccups. When placing records in an array, the reference in the array runs risk of being either the first or last item in the array. This I find it to be in par with putting SNow objects inside arrays. Such as array.push(current.sys_id).

I think that part of my issue was the load of the array wasn't working as I would have expected.

 

While I may have been on the second iteration through my GlideRecord collection every time the code tried to write the full object into the array it would start over at 1st or Last record I was able to see it by fluke when I switched between using "for or while" variant of looping. in my case I was testing with a change that only had 2 related items attached to it so I was only seeing one the other.

 

Guess it boils sown to something to mess around with further as to why loading a array with a Gliderecord doesn't quite work. 

I ended up refactoring the way I was doing things and I did end up closer to what I believe, 

Bhagyashree was talking about doing.