Create excelsheet from scipt

prashant8
Tera Expert

Hi everyone hope you doing well,

I have a requirement -

send a notification to assigned_to(user select in assigned to),

Email(notification) should include all incident (with same assigned_to & state= in progress),

& these incident should be in an excel sheet format.

Thanks 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to store file to that incident record and include that in email

So just before triggering the email write script to generate excel. I would suggest to use CSV as it's easy

then trigger the email using gs.eventQueue()

Generate CSV file through script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

suvro
Mega Sage
Mega Sage

Check this out

https://community.servicenow.com/community?id=community_question&sys_id=585851e71bc18d54ccc253da234bcbef

 

Please mark my response as correct if it helped you

@Ankur Bawiskar need your suggestions .

Mahendra RC
Mega Sage

Hello Prashant,

To implement your requirement you will need to write after BR on sys_email and use the script shown below:

This will generate and attach the CSV file and may be you can check for .XLS file if you want.

(function executeRule(current, previous /*null when async*/ ) {

    var fileData = [];
    fileData.push("Number,Caller,Short Desc,Assignment Group,Assigned To");

    var tableFieldName = ["number", "caller_id", "short_description", "assignment_group", "assigned_to"];

    var assignedTo = "";
	//CHECK IF THERE IS ANY OTHER WAY TO GET THE ASSIGNED TO USER
	//IF NOT THEN USE THE BELOW APPROACH
    var gr = new GlideRecord("incident");
    if (gr.get(current.getValue("instance")))
        assignedTo = gr.getValue("assigned_to");
	//GET ALL THE INCIDENT IF ASSIGNED TO IS NOT EMPTY
    if (assignedTo) {
        var incRecord = new GlideRecord("incident");
        incRecord.addEncodedQuery("assigned_to=" + assignedTo + "^state=2");
        incRecord.query();
        while (incRecord.next()) {
            var fieldsData = [];
            for (var fields in tableFieldName) {
                fieldsData.push(incRecord.getDisplayValue(tableFieldName[fields]));
            }
            fileData.push(fieldsData.join(","));
        }
        //ATTACH THE FILE TO EMAIL
        var attachment = new Attachment();
        var attachmentRec = attachment.write("sys_email", current.getUniqueValue(), "Incident in your queue.csv", "CSV", fileData.join("\n"));

    }
})(current, previous);

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

Hello Prashant,

Just wanted to check with you, if the my above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

If you still need any further help or guidance on this then please update those on this question.

Thanks