How to send Email with Attached Dynamics 365 CRM Report in Resco Mobile App

By | March 20, 2020

Introduction:

Recently we had a business requirement, where we needed to send Dynamics 365 CRM Report through Email from Resco Mobile App. In this blog, you will see how to do the same. To achieve this,  follow the steps given below:

1) We added a custom button (i.e.Command) on the Entity Form of the Invoice entity. Please refer below screenshots:

2) Then, we added a ‘HTML’ file (with ‘JavaScript’ code) in the ‘Iframe’ of Entity Form of Invoice entity in order to bind ‘Click Event’ with the ‘Send Invoice’ button. Please refer below screenshot:

3) Next, we added two function (i.e. downloadReport and sendEmailWithAttachment) in our script, as shown below:

//Funtion for downloading reports

downloadReport = function (fileName, format, reportId, regarding, success, failed, scope) {

/// <summary>Downloads the MS Dynamics report into a file.</summary>

/// <param name=”fileName” type=”String”>A file name for resulting file. Leave &quot;null&quot; to let app to safely generate the file name and extension.</param>

/// <param name=”format” type=”String”>One of following formats (must be supported in Dynamics): XML, CSV, PDF, MHTML, EXCELOPENXML, WORDOPENXML, IMAGE.</param>

/// <param name=”success” type=”function(location)”>A callback function that is called with the full path to downloaded file.</param>

/// <param name=”failed” type=”function(errorMsg)”>A callback which is called in case of error.</param>

/// <param name=”scope” type=”Object”>The scope for callbacks.</param>

try {

var params = JSON.stringify({

format: format || “PDF”,

outputFileName: fileName,

outputFolder: this.outputFolder,

outputFilePath: this.outputFilePath, // for internal use only

reportId: reportId,

regardingEntity: regarding ? regarding.entityName : null,

regardingId: regarding ? regarding.id : null

});

 

//command for downloading report

MobileCRM.bridge.command(“downloadReport”, params, success, failed, scope);

} catch (e) {

MobileCRM.bridge.alert(“An Error Has occurred ” + e);

}

};

//Funtion for sending email

sendEmailWithAttachment = function (filePath, refInvoice) {

try {

/// <summary>Sends an email</summary>

/// <param name=”filePath” type=”String”> A path of the resulting file</param>

/// <param name=”entity” type=”MobileCRM.Reference”>The related entity reference.</param>

 

var attachments = [];  /// Array of attachments to send. Element must be a full path or a reference to a note.

attachments.push(filePath);

 

MobileCRM.Platform.emailWithAttachments(

“#####@#######.onmicrosoft.com”, /// email address

“Invoice”,  /// email subject

“\nHi,\n\n Please Find Attached Invoice. \n\n Regards.\n Kabir”,       /// email body

attachments,

refInvoice, null,

function (error) {

_resultingReport = null;

MobileCRM.bridge.alert(error.message);

}, null);

} catch (e) {

MobileCRM.bridge.alert(“An Error Has occurred ” + e);

}
};

4) And then used these functions in the ‘MobileCRM.UI.EntityForm.onCommand’ event, as shown below:

UI.EntityForm.onCommand(“custom_SendInvoice”,

function (EntityForm) {

_entInvoice = EntityForm;

try {

_refInvoice = new MobileCRM.Reference(EntityForm.entity.entityName, EntityForm.entity.id, EntityForm.entity.primaryName);

 

 

downloadReport(“”, “pdf”, “32C85C59-7D04-E711-80E6-00155DB8652A”, _refInvoice,

function (result) {

MobileCRM.bridge.alert(“Report Downloaded Successfully!”);

_resultingReport = result;

if (_resultingReport != null) {

//This function sents email with attached report

sendEmailWithAttachment(_resultingReport, _refInvoice);

}

},

function (error) {

_resultingReport = null;

MobileCRM.bridge.alert(error.message);

 

});

}

} catch (e) {

MobileCRM.bridge.alert(“An Error Has occurred ” + e);

}

});

5) Next, we published the Resco Mobile Project and synced the Resco Mobile App with the same. And then navigated to the Invoice record and clicked on the ‘Send Invoice’ button (i.e. Custom Command). Please refer below screenshot:

6) After the button is clicked, the email window is displayed with ‘Invoice Report’ added to the email as an attachment. Please refer below screenshot:

7) Next, ‘Send’ button was clicked and the Email with Attachment was sent to the recipient. Please refer below screenshot:

Notes:

  • In order to send the Email from Resco Mobile App, we need to configure ‘Online Exchange’ (i.e. Outlook) in the Resco Mobile Project
  • While using ‘Offline Mode’, we need to sync the Resco Mobile App after clicking the ‘Send’ button in order to send an email to the recipient
  • We need to make sure that the Dynamics 365 CRM Reports are enabled for Resco Mobile App in the organization

 

Conclusion:
In this way, technicians can use the ‘Send Email’ functionality in order to send emails with an attached report (such as invoice) through Resco Mobile App while working on a job at the client-side.

 

 

6 thoughts on “How to send Email with Attached Dynamics 365 CRM Report in Resco Mobile App

  1. Ray Beste

    the code example shows using an email address as a string. What if you are not using the local email client and instead want the email sent through CRM? CRM’s TO field doesn’t like an email address and wants a partylist item for each email contact/user/account being sent an email. how would you change this code to use this approach instead of relying on the local email client and using an email address string?

    1. Inogic

      The “downloadReport()” function downloads the provided report based on its “guid” and returns the “Path” of the downloaded report (which can be accessed programmatically). And this “Path” can be used in “MobileCRM.Platform.emailWithAttachments()” command to open a new “Email” with the attached file (based on the provided path) in Local Email Client. These are the OOB Resco commands which open an email with the provided information. So the “Path” retrieved by the “downloadReport()” command might not work in any custom functionality.

      So, you will need to find a custom functionality for creating a CRM Email entity record with an attachment in CRM from Resco Mobile App. We didn’t find any API for creating the CRM Email record with an attachment.

      Hope this helps.

      Thanks!

  2. arijeet chowdhury

    Nice Explanantion.
    Although, I need to attach the generated report file from RunMobileReport command to a annotation (notes) entity record to sync back to CRM.
    Could you please help?

    1. Inogic

      Hi,

      Thank you for your valuable comment.

      Yes, we can achieve this functionality using the “MobileCRM.DynamicEntity.saveDocumentBody” function of the “JSBridge” reference file. We have written a blog on this functionality, it will help you with adding downloaded dynamics365 reports in annotations (i.e. Notes) of the entity record.

      Please refer below link,

      https://www.inogic.com/blog/2020/09/how-to-add-dynamics365-report-in-notes-of-the-current-entity-record-in-resco-mobile-app/

      Hope this help,
      Thanks!

  3. Rohit Chavan

    Hi Team,

    Very nice blog… very helpful. I wanted to just generate report based on some record like Account/Contact. Can i use “downloadReport()” function ? Can i get more information on this “downloadReport()” command or function ?

    1. Inogic

      Hi Rohit,

      Yes, you can use the downloadReport() function in order to generate the report based on any entity record of Dynamics 365 CRM. You can get the parameter details for the downloadReport() function within our blog where we have included the code snippet. Please refer to the below screenshot for the same.

      Hope this helps!

Comments are closed.