Not able get work notes in email script notification

Madala Chaitany
Giga Guru

Hi,

I have service request incident ticket is linked with service request ticket.

SR001001 it's has Primary Task with ITINC001001, now I need to get work notes or last comments from primary task when service request is resolved.

condition - In notification, work notes should be visible when work notes are not empty. If work notes are empty then comments should be visible in notification.

I have written the script but not working. 

(function runMailScript(current, template, email, email_action, event) {

    var emailadd = gs.getProperty("instance_name") + "@service-now.com";

    var vSubject = gs.getMessage('Ticket Resolved') + ' ' + current.number + ' ' + current.short_description;
    email.setSubject(vSubject);

    var comment = "";
    if (current.primary_task.close_notes != '') {
        var a = new GlideRecord('incident');
        a.addQuery("close_notes", current.primary_task);
        a.query();
        if (a.next()) {
            comment = a.close_notes;

        }
    } else {

        var journal = new GlideRecord("sys_journal_field");

        if (current.primary_task)
            journal.addQuery("element_id", current.primary_task.sys_id);
        else
            journal.addQuery("element_id", current.sys_id);

        journal.addQuery("element", "comments");
        journal.orderByDesc("sys_created_on");
        journal.query();

        if (journal.next()) {
            comment = journal.value;

            var stringExclude = gs.getProperty('sn_uni_req.nbs.ons.notification.comment.exclude');
            var stringExcludeItems = stringExclude.split(',');

            for (var i = 0; i < stringExcludeItems.length; i++) {
                comment = comment.replace(stringExcludeItems[i], '');
            }
        }
    }

    template.print("<p>" + gs.getMessage("This ticket has been resolved.") + "</p><p>" + gs.getMessage("Resolution summary") + ": " + comment + "</p><p>" + gs.getMessage("The ticket is currently in 'Awaiting Acceptance' status and it will be automatically closed if no further action is taken in the next 5 days.") + '</p>');

    var anchorTag_approve = '<p>' + gs.getMessage('Click on') + ' <a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Approve&body=UR Ticket ' + current.number + ' - Approve the resolution  (';

    var anchorTag_approve_close = ')"> ' + gs.getMessage('ACCEPT') + ' </a>' + gs.getMessage('to confirm the resolution, please do not add any comments, as these will not be monitored, or') + '</p>';

    var anchorTag_reject = '<p>' + gs.getMessage('Click on ') + '<a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Reject&body=UR Ticket ' + current.number + ' - Reject the resolution  (';

    var anchorTag_reject_close = ')">' + gs.getMessage('REJECT') + ' </a>' + gs.getMessage('if you still have further questions and do not want the ticket to close. You will be required to add comments on how we can further support you.') + '</p>';

    template.print;
    template.print(anchorTag_approve + email.watermark + anchorTag_approve_close);
    template.print(anchorTag_reject + email.watermark + anchorTag_reject_close);

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

Please Someone help me out,

Thanks & Regards,

Chaitanya

1 ACCEPTED SOLUTION

Madala Chaitany
Giga Guru

Hi,

Updating Script, which is working

(function runMailScript(current, template, email, email_action, event) {

    var emailadd = gs.getProperty("instance_name") + "@service-now.com";

    var vSubject = gs.getMessage('Ticket Resolved') + ' ' + current.number + ' ' + current.short_description;
    email.setSubject(vSubject);

    var comment = "";
    if (current.primary_task.close_notes != '') {
            comment = current.primary_task.close_notes;
    } else {

        var journal = new GlideRecord("sys_journal_field");

        if (current.primary_task)
            journal.addQuery("element_id", current.primary_task.sys_id);
        else
            journal.addQuery("element_id", current.sys_id);

        journal.addQuery("element", "comments");
        journal.orderByDesc("sys_created_on");
        journal.query();

        if (journal.next()) {
            comment = journal.value;

            var stringExclude = gs.getProperty('sn_uni_req.nbs.ons.notification.comment.exclude');
            var stringExcludeItems = stringExclude.split(',');

            for (var i = 0; i < stringExcludeItems.length; i++) {
                comment = comment.replace(stringExcludeItems[i], '');
            }
        }
    }

    template.print("<p>" + gs.getMessage("This ticket has been resolved.") + "</p><p>" + gs.getMessage("Resolution summary") + ": " + comment + "</p><p>" + gs.getMessage("The ticket is currently in 'Awaiting Acceptance' status and it will be automatically closed if no further action is taken in the next 5 days.") + '</p>');

    var anchorTag_approve = '<p>' + gs.getMessage('Click on') + ' <a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Approve&body=UR Ticket ' + current.number + ' - Approve the resolution  (';

    var anchorTag_approve_close = ')"> ' + gs.getMessage('ACCEPT') + ' </a>' + gs.getMessage('to confirm the resolution, please do not add any comments, as these will not be monitored, or') + '</p>';

    var anchorTag_reject = '<p>' + gs.getMessage('Click on ') + '<a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Reject&body=UR Ticket ' + current.number + ' - Reject the resolution  (';

    var anchorTag_reject_close = ')">' + gs.getMessage('REJECT') + ' </a>' + gs.getMessage('if you still have further questions and do not want the ticket to close. You will be required to add comments on how we can further support you.') + '</p>';

    template.print;
    template.print(anchorTag_approve + email.watermark + anchorTag_approve_close);
    template.print(anchorTag_reject + email.watermark + anchorTag_reject_close);

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

Thanks & Regards,

Chaitanya

View solution in original post

2 REPLIES 2

Alex240
Giga Expert

Hey Chaitanya!

 

Apparently, there is no issues. However, this is not the first time I've seen strange behaviours when not using the SN API but let's say pure JS. Specially within "if" statements. I would recommend you to use the SN API. 

 

. . .
    if (!gs.nil(current.primary_task.close_notes)) {
        var a = new GlideRecord('incident');
        a.addQuery("close_notes", current.getValue('primary_task'));
        a.query();
        if (a.next()) {
            comment = a.getValue('close_notes');

        }
    } else {

        var journal = new GlideRecord("sys_journal_field");

        if (current.getValue('primary_task')) {
            journal.addQuery("element_id", current.getValue('primary_task'));
        }
        else {
            journal.addQuery("element_id", current.getUniqueValue());
        }

        journal.addQuery("element", "comments");
        journal.orderByDesc("sys_created_on");
        journal.setLimit(1);
        journal.query();

        if (journal.next()) {
            comment = journal.getValue('value');
. . .

 

I also included a setLimit(1) statement to the journal query. Since you just one the last element, system does not need to loop over all the elements fetched in the query.

 

And my last suggestion, please try to avoid meaningless variable names like "a" when possible 😛

 

Hope it works,

Alex

Madala Chaitany
Giga Guru

Hi,

Updating Script, which is working

(function runMailScript(current, template, email, email_action, event) {

    var emailadd = gs.getProperty("instance_name") + "@service-now.com";

    var vSubject = gs.getMessage('Ticket Resolved') + ' ' + current.number + ' ' + current.short_description;
    email.setSubject(vSubject);

    var comment = "";
    if (current.primary_task.close_notes != '') {
            comment = current.primary_task.close_notes;
    } else {

        var journal = new GlideRecord("sys_journal_field");

        if (current.primary_task)
            journal.addQuery("element_id", current.primary_task.sys_id);
        else
            journal.addQuery("element_id", current.sys_id);

        journal.addQuery("element", "comments");
        journal.orderByDesc("sys_created_on");
        journal.query();

        if (journal.next()) {
            comment = journal.value;

            var stringExclude = gs.getProperty('sn_uni_req.nbs.ons.notification.comment.exclude');
            var stringExcludeItems = stringExclude.split(',');

            for (var i = 0; i < stringExcludeItems.length; i++) {
                comment = comment.replace(stringExcludeItems[i], '');
            }
        }
    }

    template.print("<p>" + gs.getMessage("This ticket has been resolved.") + "</p><p>" + gs.getMessage("Resolution summary") + ": " + comment + "</p><p>" + gs.getMessage("The ticket is currently in 'Awaiting Acceptance' status and it will be automatically closed if no further action is taken in the next 5 days.") + '</p>');

    var anchorTag_approve = '<p>' + gs.getMessage('Click on') + ' <a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Approve&body=UR Ticket ' + current.number + ' - Approve the resolution  (';

    var anchorTag_approve_close = ')"> ' + gs.getMessage('ACCEPT') + ' </a>' + gs.getMessage('to confirm the resolution, please do not add any comments, as these will not be monitored, or') + '</p>';

    var anchorTag_reject = '<p>' + gs.getMessage('Click on ') + '<a href="mailto:' + emailadd + '?subject=re: ' + current.number + ' - Reject&body=UR Ticket ' + current.number + ' - Reject the resolution  (';

    var anchorTag_reject_close = ')">' + gs.getMessage('REJECT') + ' </a>' + gs.getMessage('if you still have further questions and do not want the ticket to close. You will be required to add comments on how we can further support you.') + '</p>';

    template.print;
    template.print(anchorTag_approve + email.watermark + anchorTag_approve_close);
    template.print(anchorTag_reject + email.watermark + anchorTag_reject_close);

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

Thanks & Regards,

Chaitanya