How to trigger a scheduled email report with an UI Action in a scoped application (Utah)

arnoldha
Tera Guru

Hi guys,

I'm facing a problem. I'm trying to fire a scheduled email report when someone clicks on an UI action in a list view.

Here's the code I've written :

 

function launchEvent() {

    var rec = new GlideAjax('sysauto_report');
    rec.addQuery('sys_id', '992296531b93sdijijç662d73604bcb3a');
    gs.executeNow(rec);

}

 

I've tried so many different things but it's not working because in a scoped application. 
Something I would love to have, is a pop-up message saying that the report had been sent (or not).

 

Could someone help me on this ?

Thanks in avance

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

HI @arnoldha ,
I trust you are doing great.

To resolve your issue and achieve the desired functionality, you can follow these steps:

  1. Create a UI Action:

    • Navigate to the list view where you want the UI action to be available.

    • Go to the "UI Actions" related list on the form.

    • Click on "New" to create a new UI action.

    • Provide a name for the UI action, such as "Send Report".

    • In the "Table" field, enter the name of the table you are working with.

    • In the "Action name" field, enter a unique name for the action, like "send_report_action".

    • Choose the appropriate conditions and roles for the UI action visibility.

    • In the "Script" section, enter the following code:

function launchEvent() {
    var rec = new GlideAjax('sysauto_report');
    rec.addQuery('sys_id', current.sys_id); // This will pass the current record's sys_id
    gs.executeNow(rec);

    // Show a pop-up message
    gs.addInfoMessage("The report has been sent.");
    action.setRedirectURL(current);
}
launchEvent();
    • Save the UI action.

  1. Create a Script Include:

    • Navigate to "System Definition" -> "Script Includes".
    • Click on "New" to create a new Script Include.
    • Provide a name for the Script Include, such as "sysauto_report".
    • In the script section, enter your implementation logic for generating and sending the report. Make sure to handle the provided sys_id parameter appropriately.
    • Save the Script Include.
  2. Test the UI Action:

    • Go to the list view where you added the UI action.
    • You should see the "Send Report" UI action available for each record.
    • Click on the UI action for a specific record, and it will trigger the script you defined.
    • The report will be sent, and a pop-up message saying "The report has been sent" will appear.

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks Amit for your answer! I adapted my code with your input, and received an error saying:

rec.addQuery is not a function

I changed the code to look like this :

function launchEvent() {
    var rec = new GlideAjax('sysauto_report');
    rec.addParam('sys_id', '<sys_id>');
    gs.executeNow(rec);

    // Show a pop-up message
    gs.addInfoMessage("The report has been sent.");
    action.setRedirectURL(current);
}

But now the error changed to:

Uncaught ReferenceError: gs is not defined

 

For the second part about the Script Include, is it really needed ? Because I just want the UI Action to trigger the Scheduled Email Report

arnoldha
Tera Guru

Apparently, I have to create a client script as well.

Here's the code I have

 

UI Action Script:

function launchReport() {
    var ga = new GlideAjax('report');
    ga.addParam('sysparm_name', 'runReport');
    ga.addParam('sysparm_report_sys_id', '<sys_id>');
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            alert(answer);
        } else {
            alert('No response received');
        }
    });
}

And on the Script Include:

var report= Class.create();
report.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    runReport: function() {
        var reportSysId = this.getParameter('sysparm_report_sys_id');
        var reportGR = new GlideRecord('sysauto_report');
        if (reportGR.get(reportSysId)) {
            var sysReport = new GlideSysReport(reportGR);
            sysReport.execute();
            return 'Report executed successfully';
        } else {
            return 'Report not found';
        }
    },

    type: 'report'
});

But when clicking on the UI Action button, I receive a "No response received" message. Does anyone see what I'm missing ?