Email Subject for Report on sysauto_report

priyamkh199
Mega Contributor

Hello, 

   I want to dynamically set the report subject as The generated week report - 10/02/2025-19/02/2025.

I tried setting up javascript:new myscriptinclude().getSubject();, on the subject field, but no luck. I set the calculated date on the Subject field, but that's not a good idea as it will be global subject. I have a script which give me dates for the current date and the date of the last week's monday.

 But I need to dynamically populate that subject. How can I approach this??

email_report_Test.JPG

1 ACCEPTED SOLUTION

maheshkhatal
Mega Sage

@priyamkh199 You can create an email notification script and use APIs such as email.setSubject,email.setBody etc and have that script called in the message body part like ${mail_script:"name_of_your_script'}.

Mark my response as helpful if it worked for you.

Thanks,

Mahesh.

View solution in original post

3 REPLIES 3

maheshkhatal
Mega Sage

@priyamkh199 You can create an email notification script and use APIs such as email.setSubject,email.setBody etc and have that script called in the message body part like ${mail_script:"name_of_your_script'}.

Mark my response as helpful if it worked for you.

Thanks,

Mahesh.

Debasis Pati
Tera Guru

Hello @priyamkh199 ,

First of all create one script include which will create the subject dynamically 
Second create one email script and in the email script call the script include

Script include :

var DynamicSubjectGenerator = Class.create();
DynamicSubjectGenerator.prototype = {
initialize: function() {
},

// Method to get the report subject dynamically
getSubject: function() {
var today = new GlideDateTime(); // Current date and time
var currentDate = today.getDate(); // Get current date in MM/DD/YYYY format

// Calculate previous week's Monday
var previousMonday = this.getPreviousMonday(today);
var formattedPreviousMonday = previousMonday.getDate(); // Get date in MM/DD/YYYY format

// Format the subject string
return "The generated week report - " + formattedPreviousMonday + " - " + currentDate;
},

// Method to calculate the date of last week's Monday
getPreviousMonday: function(currentDate) {
var dayOfWeek = currentDate.getDayOfWeek();
var daysToSubtract = (dayOfWeek == 1) ? 7 : dayOfWeek - 1; // If today is Monday, subtract 7 days

var previousMonday = new GlideDateTime(currentDate);
previousMonday.addDaysUTC(-daysToSubtract); // Subtract to get previous Monday
return previousMonday;
},

type: 'DynamicSubjectGenerator'
};

Calling script include from email script:
var reportSubject = new DynamicSubjectGenerator().getSubject();
current.subject = reportSubject; // Assign the dynamic subject to the report
current.update(); // Save the report record with the new subject

calling email script from scheduled report:
${mail_script:"name_of_your_script'}.

DebasisPati_0-1740123861581.png


Please mark this as correct/helpful if this helps you.

Regards,
Debasis













sunil maddheshi
Tera Guru

@priyamkh199 

Create Script inlcude:

var ReportUtils = Class.create();
ReportUtils.prototype = {
    initialize: function() {},
    
    getSubject: function() {
        var today = new GlideDateTime();
        var lastMonday = new GlideDateTime();
        lastMonday.addDaysUTC(-today.getDay() + 1 - 7); // Last week's Monday
        
        var startDate = lastMonday.getLocalDate(); 
        var endDate = today.getLocalDate();
        
        return "The generated week report - " + startDate + " - " + endDate;
    },
    
    type: 'ReportUtils'
};

Create mail script:

(function runMailScript(current, template, email, email_action, event) {
    var subject = new ReportUtils().getSubject();
    email.setSubject(subject);
})(current, template, email, email_action, event);

In your report subject call this mail script:

${mail_script:"name_of_your_script'}

 

Please mark/corrcet helpful if this helps you!