Trying to list Incidents in standard report via script.

Aditya Dev
Kilo Contributor

Hi All, I am trying to fetch data on standard report (list view) using script include.

Below is scenario : 

There are two custom fields on Incident form (value - integer)

1. actual efforts (u_actual_efforts)

2. planned efforts (u_planned_efforts)

I want to fetch data comparing these two fields ; if actual efforts is more than planned efforts return incidents.

Can you please help me how to write script for the same? I want to use script Include.

Also, if there is any other way, i would also prefer using it.

 

1 ACCEPTED SOLUTION

Hi Aditya,

 

Use the following script include;

var CommunityAnswerEffortComparison = Class.create();
CommunityAnswerEffortComparison.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	compareEfforts : function(){
		var array = [];
		
		var gr = new GlideRecord('incident');
		//gr.addActiveQuery();
		gr.query();
		
		while(gr.next()){
			
			var plannedEffort = gr.getValue('u_planned_efforts');
			var actualEffort = gr.getValue('u_actual_efforts');

			var difference = gs.dateDiff(actualEffort, plannedEffort, true);

			if(difference < 0)
				array.push(gr.getValue('sys_id')+'');	
		}
		
		return array.toString();
		
	},

    type: 'CommunityAnswerEffortComparisonForReport'
});

 

Hopefully this will fulfill your requirement.

 

View solution in original post

5 REPLIES 5

Muhammad Khan
Mega Sage
Mega Sage

Hi Aditya,

 

Create a script include as follows;

var CommunityAnswerEffortComparison = Class.create();
CommunityAnswerEffortComparison.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	compareEfforts : function(){
		var array = [];
		
		var gr = new GlideRecord('incident');
		//gr.addActiveQuery();
		gr.addEncodedQuery('u_actual_effortsGT_FIELDu_planned_efforts');
		gr.query();
		
		while(gr.next()){
			array.push(gr.getValue('sys_id')+'');
		}
		
		return array.toString();
		
	},

    type: 'CommunityAnswerEffortComparison'
});

Following is the image of Script Include for reference.

find_real_file.png

 

Call it in filter condition of report as follows;

javascript:new CommunityAnswerEffortComparison().compareEfforts()

Following is the image of filter condition in report for reference.

find_real_file.png

 

For reference, do check following knowledge article;

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0832058

 

Note: You can also use condition builder (filter) in reports to fetch these records without using script.

 

Hopefully this will help you in creating your report.

Hi Muhammad Khan,

 

Can you help me if Field value is Duration, not Integer ? do we have to convert duration to integer and then compare values? if yes, can you help how to modify existing script ?

Hi Aditya,

 

Use the following script include;

var CommunityAnswerEffortComparison = Class.create();
CommunityAnswerEffortComparison.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	compareEfforts : function(){
		var array = [];
		
		var gr = new GlideRecord('incident');
		//gr.addActiveQuery();
		gr.query();
		
		while(gr.next()){
			
			var plannedEffort = gr.getValue('u_planned_efforts');
			var actualEffort = gr.getValue('u_actual_efforts');

			var difference = gs.dateDiff(actualEffort, plannedEffort, true);

			if(difference < 0)
				array.push(gr.getValue('sys_id')+'');	
		}
		
		return array.toString();
		
	},

    type: 'CommunityAnswerEffortComparisonForReport'
});

 

Hopefully this will fulfill your requirement.

 

Awesome, This works Absolutely fine, I need to learn coding from you..