Sunday 25 December 2011

Sending Email using outlook in AX 2009

Hi Friends,

    Today we are going to send an email in Ax 2009 by using the outlook.

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:

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()
{
    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;
}
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();
    if (!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.  In AOT, create a new Action menu item with the following properties
Property Value
Name SendEmail
ObjectType Class
Object SendEmail
Label Send email
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



How it works...

For this  we created a new RunBase-based class. It contains the following
member methods:
 f new() is the class constructor, and here we initialize the language variable.
 f parmCustTable() sets or gets the custTable parameter.
 f pack() prepares user data to be stored for the next time.
 f unpack() retrieves stored data (if any) .
 f construct() creates a new instance of this class.
 f dialog() changes the default dialog caption and adds the email template
selection field.
 f getFromDialog() stores the user file selection into the emailId variable.
 f subject() returns the email subject from the email template table.
 f processMappings() replaces the template placeholders with the actual values.
Here we use stringExpand() of the SysEmailMessage class for this purpose.

 f 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.
 f validate() checks if a valid buffer is passed.
 f 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.
 f 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.














1 comment: