Sync System Email Templates to all companies.

Challenge / Problem: Maintaining email templates across multiple companies.

Descritpion: Dynamics AX makes use of email templates for various bits of functionality in Dynamics AX, including workflow notifications and alert notifications. If you are using workflow in multiple companies and want to keep the same workflow template across the board, it can be quite frustrating to have to make the same changes in every company. The following script/job in X++ will help sync all (or some) system email templates and their respective languages into every company in Dynamics AX.

/// Copies All System Email Template to all companies
/// WARNING: Will create or overwrite existing templates in other companies
static void syncWorkflowTemplates(Args _args)
{
    DataArea  DataArea;
    SysEmailSystemTable email;
    SysEmailTable local;
    SysEmailMessageSystemTable message;
    SysEmailMessageTable localMess;
    WorkflowParameters params;

    void FindOrCreate()
    {
        local = SysEmailTable::find(email.EmailId, true);
        if (local.RecId)
        {
            info(strFmt("Deleting %1 (%2)", local.EmailId, curext()));
            local.delete();
        }
        local.DefaultLanguage = email.DefaultLanguage;
        local.Description       = email.Description;
        local.EmailId           = email.EmailId;
        local.Priority          = email.Priority;
        local.SenderAddr        = email.SenderAddr;
        local.SenderName        = email.SenderName;
        info(strFmt("Adding %1 (%2)", email.EmailId, curext()));
        local.insert();
    }

    void FindOrCreateMessage()
    {
        localMess.clear();
        localMess = SysEmailMessageTable::find(message.EmailId, message.LanguageId, true);
        if (localMess.RecId)
        {
            info(strFmt("Deleting %1 %2 (%3)", localMess.EmailId, localMess.LanguageId, curext()));
            localMess.delete();
        }
        localMess.EmailId       = message.EmailId;
        localMess.LanguageId    = message.LanguageId;
        localMess.LayoutType    = message.LayoutType;
        localMess.Mail          = message.Mail;
        localMess.Subject       = message.Subject;
        localMess.XSLTMail      = message.XSLTMail;
        info(strFmt("Adding %1 %2 (%3)", localMess.EmailId, localMess.LanguageId, curext()));
        localMess.insert();
    }
    ttsBegin;
    // Restrict to specific templates in a where clause if necessary
    while select email
    {
        while select DataArea where !DataArea.isVirtual && DataArea.id != email.dataAreaId
        {
            setPrefix(DataArea.Id);
            changecompany (DataArea.Id)
            {
                FindOrCreate();
            }
            while select message where message.EmailId == email.EmailId
            {
                changecompany (DataArea.Id)
                {
                    FindOrCreateMessage();
                }
            }
        }
    }

    ttsCommit;
}

Leave a Reply

Your email address will not be published.