Context SPWidget: Send email directly from button + file (created by querybuilder)

Alex441
Tera Contributor

Hi everyone, hope you are doing well.

From a button, I want to directly send an email with the file generated by the query generator. 
I created this which allows me to download a file from the script client : 

HTML : 

<div style="position: relative; display: inline-block;">
<button tabindex="0"
id="sn_customerservice_case-download-filter-button"
style="float: left; margin-top: 20px; "
title="Download Filter"
class="btn btn-primary clear-filter-btn sn-filter-tooltip"
type="button"
ng-click="toggleDownloadDropdown()">
<i class="fa fa-download"></i>
</button>


<div tabindex="0" class="downloadDropdown" ng-show="downloadDropdownVisible" style="position: absolute; bottom: 100%; right: 0; z-index: 1; background-color: #fff; border: 1px solid #ccc; white-space: nowrap;">
<div ng-click="downloadFilter('EXCEL')" class="excel-item">Export to Excel</div>
<div ng-click="downloadFilter('CSV')" class="csv-item">Export to CSV</div>
</div>

</div>


CLIENT SIDE : 

$scope.downloadDropdownVisible = false;

$scope.toggleDownloadDropdown = function () {
    $scope.downloadDropdownVisible = !$scope.downloadDropdownVisible;
};

$scope.downloadFilter = function (downloadType) {
    $scope.downloadDropdownVisible = false;

    // Assurez-vous que la variable query est définie avant de l'utiliser
    var query = buildQuery($scope.c.data); 

    // Logique de download + Query
var downloadUrl = "/sn_customerservice_case.do?" +
    downloadType +
    "&sysparm_query=" + query +
    "&sysparm_fields=" + 'number,u_case_type,short_description,product,account,account.u_correlation_id,priority,contact,state,sys_created_on,sys_updated_on,close_notes&' + 'ORDERBYDESCsys_created_on';
    console.log (downloadUrl);
    $window.location.href = downloadUrl;

};


AND i created this button for send an email, I don't know how to recover a file created nowhere.
The first button works and downloads a file.
The last button does not work, it opens a window with the email template, without a folder.
And I would like to send an email with the generated file, directly without it opening anything.

HTML : 

<!-- btn  mail CSV/EXCEL -->
<div style="position: relative; display: inline-block;">
<button tabindex="0"


id="sn_customerservice_case-export-filter-button"
style="float: left; "
title="Export Filter"
class="btn btn-primary clear-filter-btn sn-filter-tooltip"
type="button"
ng-click="toggleExportDropdown()">
<i class="fa fa-envelope"></i>
</button>

<div tabindex="0" class="exportDropdown" ng-show="exportDropdownVisible" style="position: absolute; bottom: 100%; right: 0; z-index: 1; background-color: #fff; border: 1px solid #ccc; white-space: nowrap;">
<script src="https://cdn.emailjs.com/dist/email.min.js"></script>
<div href="mailto:" ng-click="c.mailTo('EXCEL')" class="excel-item">Send Excel</div>
<div href="mailto:" ng-click="c.mailTo('CSV')" class="csv-item" >Send CSV</div>

</div>
</div>
</div>
</div>



CLIENT SIDE :

$scope.downloadDropdownVisible = false;

$scope.toggleExportDropdown = function () {
    $scope.exportDropdownVisible = !$scope.exportDropdownVisible;
};

c.mailTo = function(downloadType) {  
var query = buildQuery($scope.c.data); 
var votreAdresseEmail = 'xxxxxxxxxxxx@gmail.com';
var Emailsubject = 'Your file';
var emailbody='Please find attached your document';
        var url = "mailto:xxxxxxxxx@gmail.com?subject=Emailsubject&body=emailbody";
        $window.open(url, '_self');
    };

Could anyone here help me please? thx 

2 REPLIES 2

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

I don't think there is any way you can send attachments with a mailto URL.

 

Here's an idea of how I would try to implement a solution for this:

  1. Save the attachment
    • You will need the file, so I would rework the first button to use the RESTMessageV2 API, and more specifically, the saveResponseBodyAsAttachment method. This will need a table and record sysID, along with a filename as parameters, so your parameters may depend on the context of where your button is placed.
  2. Move the whole email sending into a Flow Designer subflow (or even the attachment saving, if it doesn't need to be a separate step)
    • The subflow will need a minimum of 3 actions:
      1. A 'Look Up Record' action on the sys_attachment table to get the file you saved in the previous step
      2. A 'Send Email' action to create an outbound email record based on your requirements
      3. A 'Copy Attachment' action where you map the file found in step i) to the record created in step ii)
  3. Have your button use the ScriptableFlowRunner API to trigger the subflow from step 2) with the necessary inputs (such as the recipient and subject of the email, attachment or record to look-up, etc.)

 

Let me know how and if this works out. Hope it helps!

Alex441
Tera Contributor

Thank a lot for all the explanations, i think too isn't possible, so I will try to do this 🙂