Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Sending email using Outlook

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/2/2012 11:29:52 AM
In most projects it is required to send emails using Dynamics AX. It might be business alerts, monthly reports, customer statements, and so on. Dynamics AX has a number of ways to send emails. One of them is to use Microsoft Office Outlook. Although Dynamics AX supports tight integration with Outlook contacts, calendar, and tasks as a part of the CRM module, emailing using Outlook is still very basic across the application. Normally, required emailing functions have to be implemented as additional application customizations.

In this recipe, we will see how emails can be sent from Dynamics AX using Outlook. Dynamics AX provides an API to access the Outlook application. We will use it to create a function that allows sending template-based personalized emails to customers.

Getting ready

Before we start with this recipe, we need to create an email template. This is standard Dynamics AX functionality. Open Basic | Setup | E-mail templates, and create the following record:

Next click on the Template button, and enter the email body:

How to do it...

  1. 1. In AOT, create a new class called SendEmail with the following code:

    class SendEmail extends RunBase
    
    {
    SysEmailId emailId;
    DialogField dlgEmailId;
    CustTable custTable;
    LanguageId languageId;
    #define.CurrentVersion(1)
    #localmacro.CurrentList
    emailId
    #endmacro
    }
    void new()
    
    {;
    super();
    languageId = infolog.language();
    }
    public CustTable parmCustTable(
    CustTable _custTable = custTable)
    
    {;
    custTable = _custTable;
    return custTable;
    }
    public container pack()
    
    emailemailsending in Oulook, steps{
    return [#CurrentVersion, #CurrentList];
    }
    public boolean unpack(container packedClass)
    
    {
    int version = RunBase::getVersion(packedClass);
    ;
    switch (version)
    {
    case #CurrentVersion:
    [version, #CurrentList] = packedClass;
    return true;
    default :
    return false;
    }
    return false;
    }
    public static SendEmail construct(CustTable _custTable)
    
    {
    SendEmail sendEmail;
    ;
    sendEmail = new SendEmail();
    sendEmail.parmCustTable(_custTable);
    return sendEmail;
    }
    protected Object dialog()
    
    {
    Dialog dialog;
    ;
    dialog = super();
    dialog.caption("Select email template");
    dlgEmailid = dialog.addFieldValue(
    typeid(SysEmailId), emailId);
    return dialog;
    }
    public boolean getFromDialog()
    
    {;
    emailId = dlgEmailId.value();
    return true;
    emailemailsending in Oulook, steps}
    str subject()
    
    {
    return SysEmailMessageTable::find(
    emailId, languageId).Subject;
    }
    str processMappings(str _message)
    
    {
    Map mappings;
    ;
    mappings = new Map(Types::String, Types::String);
    mappings.insert('name', custTable.Name);
    mappings.insert('company', CompanyInfo::name());
    return SysEmailMessage::stringExpand(_message, mappings);
    }
    str message()
    
    {
    COM document;
    COM body;
    str ret;
    SysEmailMessageTable message;
    #help
    ;
    message = SysEmailMessageTable::find(emailId, languageId);
    ret = this.processMappings(message.Mail);
    ret = WebLet::weblets2Html4Help(ret, '');
    document = new COM(#HTMLDocumentClassName);
    SysEmailTable::insertHTML2Document(document, ret);
    body = document.body();
    emailemailsending in Oulook, stepsif (!body)
    {
    return '';
    }
    return body.outerText();
    }
    public boolean validate()
    
    {;
    if (!custTable)
    {
    return false;
    }
    return true;
    }
    public void run()
    
    {
    SysInetMail mail;
    ;
    mail = new SysInetMail();
    mail.parmForceSendDialog(true);
    mail.sendMail(
    custTable.Email,
    this.subject(),
    this.message(),
    true);
    }
    public static void main(Args _args)
    
    {
    SendEmail sendEmail;
    ;
    if (!_args || !_args.record())
    {
    throw error(Error::missingRecord(funcname()));
    }
    sendEmail = SendEmail::construct(
    _args.record());
    if (sendEmail.prompt())
    {
    sendEmail.run();
    }
    }
    
    
    					  
  2. 2. In AOT, create a new Action menu item with the following properties

    Property Value
    Name SendEmail
    ObjectType Class
    Object SendEmail
    Label Send email

  1. 3. Add the newly created menu item to the bottom of the CustTable form's ButtonGroup group by creating a new MenuItemButton with the following properties:

    Property Value
    Name SendEmail
    MenuItemType Action
    MenuItemName SendEmail
    DataSource CustTable

  1. 4. In AOT, form should look like the following screenshot:

  1. 5. Open Accounts receivable | Customer Details, and select any customer:

  1. 6. Click the Send e-mail button, and choose the previously created email template:

  1. 7. Click OK. If Outlook displays a security warning, then click Allow:

  1. 8. And finally, an Outlook message editing form is displayed allowing us to change its content before actually sending it:

How it works...

For this recipe we created a new RunBase-based class. It contains the following member methods:

  • new() is the class constructor, and here we initialize the language variable.

  • parmCustTable() sets or gets the custTable parameter.

  • pack() prepares user data to be stored for the next time.

  • unpack() retrieves stored data (if any) .

  • construct() creates a new instance of this class.

  • dialog() changes the default dialog caption and adds the email template selection field.

  • getFromDialog() stores the user file selection into the emailId variable.

  • subject() returns the email subject from the email template table.

  • processMappings() replaces the template placeholders with the actual values. Here we use stringExpand() of the SysEmailMessage class for this purpose.

  • message() returns the email body from the email template set up with the processed placeholders. Because the body text is stored as a Dynamics AX weblet, we need to convert it into text format.

    First, we get rid of the weblet tags by using weblets2Html4Help() method of WebLet application class.

    Next, we convert HTML to text by using a COM class to create an HTML document object and get its content as text by calling outerText() on its body element.

  • validate() checks if a valid buffer is passed.

  • run() creates a new instance of SysInetMail and uses its sendMail() to send the email. This method takes customer email address, message subject, and message body as arguments.

    Here we also call parmForceSendDialog() with true right before sendMail(). This stops email from being sent automatically and allows the user to modify the message. This parameter overrides the last parameter of sendMail(), which does exactly the same but only if there is no email body text.

  • main() puts everything together.

Lastly, we create an Action menu item pointing to this class, and add it to the Customers form as a button. This allows us to use the button to send an email to the selected customer.

Other -----------------
- Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Exporting data to Microsoft Project
- Microsoft Content Management Server : The ASP.NET Stager Application (part 3) - Staging Attachments
- Microsoft Content Management Server : The ASP.NET Stager Application (part 2) - Staging Channels and Postings
- Microsoft Content Management Server : The ASP.NET Stager Application (part 1) - The DotNetSiteStager Project, Recording Messages to a Log File
- Microsoft Content Management Server : Staging Static Pages - Site Stager in Brief
- BizTalk 2010 : WCF LOB SQL Adapter - Consuming ASDK SQL Adapter in Visual Studio (part 2)
- BizTalk 2010 : WCF LOB SQL Adapter - Consuming ASDK SQL Adapter in Visual Studio (part 1)
- Windows Server 2008 Server Core : Renaming a File with the Ren and Rename Commands, Sorting File Content with the Sort Utility
- Windows Server 2008 Server Core : Moving Files and Renaming Files and Directories with the Move Command, Recovering Lost Files with the Recover Utility
- Windows Server 2008 : Moving Accounts with dsmove, Removing Objects with dsrm, Retrieving Information about Objects with dsquery
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server