email notification script to return multiple values

Hari1
Mega Sage

Hi All,

We have created an email notification script to create a report and i am not able to see all the records.

notification script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

        baseUrl = gs.getProperty("glide.servlet.uri");
	email.setSubject("User- User ticket details : " + gs.now());
	
	var arr = [1,2,3,6,7,8]; //New,In Progress, On Hold, Resolved,CLosed,Canceled
	var storeCount = [];
	var listCount = {};
	var x = 0;
	var getGroup,getCountInProg, getCountOnHold, getCountNew,getState,cc, getStateVal,getIncCount;
	while(x < arr.length)
	{
		gs.log("User-Inside while");
		var gr = new GlideRecord('incident');
		gr.addQuery('active',true);
		gr.addQuery('assigned_to','46d44a23a9fe19810012d100cca80666' );
		gr.addQuery('state',arr[x]);
		gr.orderByDesc('state'); 
		gr.chooseWindow(0, 1);
		gr.query();
		while(gr.next())
		{
			gs.log("User-inside if");
		
			getState = gr.getValue('state');
			
			if(getState == arr[x])
				{
					getGroup = gr.getDisplayValue('assignment_group');
					getStateVal = gr.getDisplayValue('state');
					cc = parseInt(gr.getRowCount());
					listCount = {Groups: getGroup,State: getStateVal, DataVal : cc};
					storeCount.push(listCount);
					gs.info('Count Values is: ' + JSON.stringify(listCount));
				}
		}
			
			
		x++;
		
	}
	gs.info('StoreVal is: ' + JSON.stringify(storeVal));

	
	template.print("<table border=2>");
	template.print("<tr><td colspan=5 align =center>List Of Tickets</td></tr>");
	template.print("<tr><th>Assignment group</th><th>State</th><th>Count</th></tr>");
	
	storeCount.forEach(mySortingFunction);
	function mySortingFunction(item, index)
	{
		gs.log("User- Inside function");
		template.print('<tr><td>' + item.Groups + '</td><td>'+ item.State + '</td><td>'+ item.DataVal + '</td></tr>');
	}

})(current, template, email, email_action, event);

 

I am seeing the output as below:

find_real_file.png

Expected output needs to be based on state and assignment group in the above output i am not able to see the assignment groups "Hardware" and "Network" Instead i am able to see only 2 group and the count based on state :

find_real_file.png

I need to get individual state count based on individual groups.

Can anyone please help me out on this?

 

1 ACCEPTED SOLUTION

I rechecked and the code works absolutely fine.

May be on your instance someone is createing tickets or reassigning tickets.

How are you triggering this email? If its via ticket creation then that might be the reason why you see a difference.

 

-Tanaji

Please mark reply correct/helpful if applicable

View solution in original post

8 REPLIES 8

I assume Generic Request is a custom field and name of the field is u_generic_request. If not then you need to replace u_gereric_request in the following code.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var finalJSONArray = [];
    baseUrl = gs.getProperty("glide.servlet.uri");
    email.setSubject("User- User ticket details : " + gs.now());

    var count = new GlideAggregate('incident');
    count.addQuery('active', 'true');
    count.addQuery('assigned_to', '46d44a23a9fe19810012d100cca80666');
    count.addAggregate('COUNT');
    count.groupBy('assignment_group');
    count.groupBy('state');
    count.groupBy('u_generic_request');
    count.query();
    if (count.hasNext()) {
        template.print("<table border=2>");
        template.print("<tr><td colspan=5 align =center>List Of Tickets</td></tr>");
        template.print("<tr><th>Assignment group</th><th>State</th><th>Count</th><th>INC Count</th><th>GREQ Count</th></tr>");

        var tempJSON = {};
        while (count.next()) {
            var assignmentGroup = count.assignment_group.getDisplayValue();
            var stateValue = count.state.getDisplayValue();
            var genericRequestValue = count.u_generic_request.getDisplayValue();
            var groupCount = count.getAggregate('COUNT');
            //template.print("<tr><td>" + assignmentGroup + "</td><td>" + stateValue + "</td><td>" + groupCount + "</td></tr>");

            if (JSON.stringify(tempJSON) == "{}") {
                tempJSON.assignment_group = assignmentGroup;
                tempJSON.state = stateValue;
                tempJSON.u_generic_request = {};
                tempJSON.u_generic_request[genericRequestValue] = groupCount;
                tempJSON.count = groupCount;
            } else if (tempJSON.assignment_group == assignmentGroup && tempJSON.state == stateValue) {
                tempJSON.u_generic_request[genericRequestValue] = groupCount;
                tempJSON.count = parseInt(tempJSON.count) + parseInt(groupCount);
            } else {
                finalJSONArray.push(tempJSON);
                tempJSON = {};
                tempJSON.assignment_group = assignmentGroup;
                tempJSON.state = stateValue;
                tempJSON.u_generic_request = {};
                tempJSON.u_generic_request[genericRequestValue] = groupCount;
                tempJSON.count = groupCount;
            }

        }
        finalJSONArray.push(tempJSON);

        for (var i in finalJSONArray) {
            template.print("<tr><td>" + finalJSONArray[i].assignment_group + "</td><td>" + finalJSONArray[i].state + "</td><td>" + finalJSONArray[i].count + "</td><td>" + (finalJSONArray[i].u_generic_request["false"] ? finalJSONArray[i].u_generic_request["false"] : 0) + "</td><td>" + (finalJSONArray[i].u_generic_request["true"] ? finalJSONArray[i].u_generic_request["true"] : 0) + "</td></tr>");
        }

        template.print("</table>");
    }

})(current, template, email, email_action, event);

 

-Tanaji

Please mark reply correct/helpful if applicable

Hi Tanaji,

Thank you once again for your response. Yes, u_generic_request is a custom field.

I don't see the count as expected.

REF:: I see the below as the output.

find_real_file.png

I see 2 GREQ and rest to be as INC but as per the above screenshot the output is wrong.

find_real_file.png 

I rechecked and the code works absolutely fine.

May be on your instance someone is createing tickets or reassigning tickets.

How are you triggering this email? If its via ticket creation then that might be the reason why you see a difference.

 

-Tanaji

Please mark reply correct/helpful if applicable

Hi Tanaji,

It's working fine now. I just needed to do some same changes in the code.

I wanted only particular state to be shown like for state is "In Progress" and "New" and i don't want "On Hold" to be there. How can we filter that ? Do you have any idea about it? 

 

find_real_file.png