D365: Send email in X++ using email templates

Product:

Dynamics 365 for Finance and Operations

Purpose:

The purpose of this document is to demonstrate how we can send emails in X++ using built-in email templates. For demonstration purposes we’ll be using standard email template CnfmOrder. This email template is used to send email to customer when a sales order is confirmed.

Business requirement:

Automate sending email to customer’s contact when a sales order is confirmed.

Prerequisites:

Email parameters must be configured.

Development:

You can find built-in email templates in the system under:

Organization administration > Setup > Email templates

Note that we have keyed in the sender email field as it will be used in the code as the “Sender”.

image

You can use the following code to automatically send email using X++. The given code also handles inserting placeholder values in the email template. It uses salesId variable which is a global class member and is provided by the caller class.

public void generateEmail()
{
   // Tokens for email template replacement
   #define.SalesIdToken('salesid')
   #define.CustNameToken('customername')
   #define.DeliveryAddress('deliveryaddress')
   #define.CustomerAddress('customeraddress')
   #define.ShipDate('shipdate')
   #define.ModeOfDelivery('modeofdelivery')
   #define.Charges('charges')
   #define.Discount('discount')
   #define.Tax('tax')
   #define.Total('total')
   #define.LineProductName('lineproductname')
   #define.LineProductDesc('lineproductdescription')
   #define.LinePrice('lineprice')
   #define.LineQuantity('linequantity')
   #define.LineNetAmount('linenetamount')

   SalesTable salesTable;
   SalesLine salesLine;
   CustTable custTable;
   ContactPerson contactPerson;
   SalesTotals salesTotals;
   SysEmailId emailId;
   Map templateTokens;

   str emailSenderName;
   str emailSenderAddr;
   str emailSubject;
   str emailBody;
   str emailToAddress;

   salesTable = SalesTable::find(salesId);
   salesLine = SalesLine::find(salesTable.SalesId);
   custTable = CustTable::find(salesTable.CustAccount);
   contactPerson = ContactPerson::find(salesTable.SixCustContactPersonId);
   salesTotals = SalesTotals::construct(salesTable);
   emailToAddress = contactPerson.email();
   emailId = salesTable.SixSysEmailId;

   templateTokens = new Map(Types::String, Types::String);
   templateTokens.insert(#SalesIdToken, salesTable.SalesId);
   templateTokens.insert(#CustNameToken, custTable.name());
   templateTokens.insert(#DeliveryAddress, salesTable.deliveryAddress().Address);
   templateTokens.insert(#CustomerAddress, custTable.address());
   templateTokens.insert(#ShipDate, strFmt("%1", salesTable.ShippingDateRequested));
   templateTokens.insert(#ModeOfDelivery, strFmt("%1", salesTable.DlvMode));
   templateTokens.insert(#Charges, strFmt("", salesTotals.totalMarkup()));
   templateTokens.insert(#Discount, strFmt("", salesTotals.totalEndDisc()));
   templateTokens.insert(#Tax, strFmt("", salesTotals.totalTaxAmount()));
   templateTokens.insert(#Total, strFmt("", salesTotals.totalAmount()));
   templateTokens.insert(#LineProductName, salesLine.itemName());
   templateTokens.insert(#LineProductDesc, salesLine.itemLineDisc());
   templateTokens.insert(#LinePrice, strFmt("%1", salesLine.SalesPrice));
   templateTokens.insert(#LineQuantity, strFmt("%1", salesLine.SalesQty));
   templateTokens.insert(#LineNetAmount, strFmt("%1", salesLine.LineAmount));
      
   if (emailId)
   {
    [emailSubject, emailBody, emailSenderAddr, emailSenderName] =
      SixCustVendEmailController::getEmailTemplate(emailId, custTable.languageId());
   }
      
   var messageBuilder = new SysMailerMessageBuilder();
   messageBuilder.addTo(emailToAddress)
    .setSubject(emailSubject)
    .setBody(SysEmailMessage::stringExpand(
      emailBody, SysEmailTable::htmlEncodeParameters(templateTokens)));

   if (emailSenderAddr)
   {
    messageBuilder.setFrom(emailSenderAddr, emailSenderName);                
    SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(messageBuilder.getMessage());
   }
}

The generateEmail() method calls the following method to get the email template:

protected static container getEmailTemplate(SysEmailId _emailId, LanguageId _languageId)
{
   var messageTable = SysEmailMessageTable::find(_emailId, _languageId);
   var emailTable = SysEmailTable::find(_emailId);

   if (!messageTable && emailTable)
   {
      // Try to find the email message using the default language from the email parameters
      messageTable = SysEmailMessageTable::find(_emailId, emailTable.DefaultLanguage);
   }

   if (messageTable)
   {
      return [messageTable.Subject, messageTable.Mail, emailTable.SenderAddr, emailTable.SenderName];
   }
   else
   {
      warning("@SYS135886"); // Let the user know we didn't find a template
      return ['', '', emailTable.SenderAddr, emailTable.SenderName];
   }
}

Once triggered, the code sends out the email through the configured SMTP server and can be found in the inbox:

Advertisements

D365: Create lookup in X++

Product:

Dynamics 365 for Finance and Operations

Purpose:

The purpose of this document is to demonstrate how we can create a lookup in X++ and attach it to an extension field added to the form extension of standard Sales order form. This is a good example to see how we can use event handling to achieve our goal without modifying the standard code.

Business requirement:

Display contacts of the customer in a lookup for which the sales order has been raised.

Development:

You can find the event handler below which handles the lookup event of a form control. This form control is added to the extension of SalesTable standard AX form.

[FormControlEventHandler(formControlStr(SalesTable, SixSalesOrderConfirmation_SixCustContactPersonId),
FormControlEventType::Lookup)]
public static void SixSalesOrderConfirmation_SixCustContactPersonId_OnLookup(FormControl sender, FormControlEventArgs e)
{
Query query;
QueryBuildDataSource qbdsContactPerson;
QueryBuildDataSource qbdsCustTable;
QueryBuildDataSource qbdsSalesTable;
SysTableLookup sysTableLookup;
SalesId salesId;
FormControlCancelableSuperEventArgs event;

event = e as FormControlCancelableSuperEventArgs;
salesId = sender.formRun().design().controlName(formControlStr(SalesTable, SalesTable_SalesId)).valueStr();

query = new Query();
qbdsContactPerson = query.addDataSource(tableNum(ContactPerson));

qbdsCustTable = qbdsContactPerson.addDataSource(tableNum(CustTable));
qbdsCustTable.joinMode(JoinMode::InnerJoin);
qbdsCustTable.relations(false);
qbdsCustTable.addLink(
fieldNum(ContactPerson, ContactForParty),
fieldNum(CustTable, Party));

qbdsSalesTable = qbdsCustTable.addDataSource(tableNum(SalesTable));
qbdsSalesTable.joinMode(JoinMode::InnerJoin);
qbdsSalesTable.relations(false);
qbdsSalesTable.addLink(
fieldNum(CustTable, AccountNum),
fieldNum(SalesTable, CustAccount));

qbdsSalesTable.addRange(fieldNum(SalesTable, SalesId)).value(SysQuery::value(salesId));

sysTableLookup = SysTableLookup::newParameters(tableNum(ContactPerson), sender);
sysTableLookup.addLookupfield(fieldNum(ContactPerson, ContactPersonId));
sysTableLookup.addLookupfield(fieldNum(ContactPerson, Party));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();

event.CancelSuperCall();
}

D365: Reverse customer transaction in X++

Purpose:

The purpose of this document is to demonstrate how we can reverse a posted customer transaction through X++. The code below can be used as a script to automate reversal of posted customer transactions.

Product:

Dynamics 365 for Finance and Operations

Development:

Please find below the code which can be used to reverse a posted customer transaction. It will actually create and post a negative entry transaction against the transaction to reverse. Please note the code defaults reason code to “ERROR” while posting a reversal transaction. Once developed this can be triggered using the following URL:

https://usnconeboxax1aos.cloud.onebox.dynamics.com/?mi=SysClassRunner&cmp=USMF&cls=MAKCustTransReversal

Code:

class MAKCustTransReversal extends TransactionReversal_Cust
{
    public static MAKCustTransReversal construct()
    {
        return new MAKCustTransReversal();
    }

    public boolean showDialog()
    {
        return false;
    }

    public static void main(Args _args)
    {
        CustTrans custTrans;
        MAKCustTransReversal makCustTransReversal;
        ReasonTable reasonTable;
        ReasonCode reasonCode;
        ReasonRefRecID reasonRefRecID;
        InvoiceId invoiceId;
        Args args;
        ;

        invoiceId = "3392";
        reasonCode = "ERROR";        
        reasonTable = ReasonTable::find(reasonCode);
        reasonRefRecID = ReasonTableRef::createReasonTableRef(
            reasonTable.Reason, reasonTable.Description);

        custTrans = CustTrans::findFromInvoice(invoiceId);
            
        if (custTrans.RecId && !custTrans.LastSettleVoucher)
        {
            args = new Args();
            args.record(custTrans);

            makCustTransReversal = MAKCustTransReversal::construct();
            makCustTransReversal.parmReversalDate(systemDateGet());
            makCustTransReversal.parmReasonRefRecId(reasonRefRecID);
            makCustTransReversal.reversal(args);
            
            info(strFmt("%1 %2 %3 %4 reversed.",
                custTrans.Voucher,
                custTrans.TransDate,
                custTrans.Invoice,
                custTrans.Txt));
        }        
    }
}

AX7/D365/Operations: Create customer postal address through data entity from .NET console application

Purpose:

The purpose of this document is to demonstrate how we can create customer postal addresses in Dynamics 365 for Finance and Operations using an external .NET console application.

Code:

static void Main(string[] args)
{
      Program program = new Program();
      Uri oDataUri = new Uri(ODataEntityPath, UriKind.Absolute);
      var context = new Resources(oDataUri);

      context.SendingRequest2 += new EventHandler(delegate (object sender, SendingRequest2EventArgs e)
      {
          var authenticationHeader = OAuthHelper.GetAuthenticationHeader(useWebAppAuthentication: true);
          e.RequestMessage.SetHeader(OAuthHelper.OAuthHeader, authenticationHeader);
      });

      program.testCustomerAddressCreate(context);
}
public void testCustomerAddressCreate(Resources _context)
{
      DataServiceCollection dataServiceCollection = new DataServiceCollection(_context);
      CustomerPostalAddress customerPostalAddress = new CustomerPostalAddress();

      dataServiceCollection.Add(customerPostalAddress);

      customerPostalAddress.CustomerLegalEntityId = "CPL";
      customerPostalAddress.CustomerAccountNumber = "C0000010";
      customerPostalAddress.AddressDescription = "Test address from OData 44.";
      customerPostalAddress.IsRoleBusiness = NoYes.Yes;
      customerPostalAddress.IsPostalAddress = NoYes.Yes;
      customerPostalAddress.AddressLocationRoles = "Address role";
      customerPostalAddress.AddressCountryRegionId = "AUS";
      customerPostalAddress.AddressZipCode = "2151";
      customerPostalAddress.AddressCity = "NORTH ROCKS";
      customerPostalAddress.AddressState = "NSW";

      DataServiceResponse response = null;

      try
      {
          response = _context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex.Message + ex.InnerException);
      }

      Console.ReadLine();
}

AX7/D365/Operations: Enable/Disable form control in X++

Purpose:

The purpose of this document is to demonstrate how we can enable/disable a form control in X++ based on a business logic.

Product:

Dynamics 365 for Finance and Operations, Platform Update 9.

Development approach:

Customization through extension.

Development:

  1. Create a post event handler of a standard form method enabling/disabling the form controls. In this case, we are creating a post event handler of a standard method setFieldAccessHeader of SalesQuotationProjTable form.
  2. Please find the event handler method definition as follows.
[PostHandlerFor(formStr(SalesQuotationProjTable), formMethodStr(SalesQuotationProjTable, setFieldAccessHeader))]
public static void SalesQuotationProjTable_Post_setFieldAccessHeader(XppPrePostArgs args)
{
    FormRun             sender = args.getThis();
    FormDataSource      salesQuotationTable_ds;
    SalesQuotationTable salesQuotationTable;

    salesQuotationTable_ds = sender.dataSource(1);
    salesQuotationTable = salesQuotationTable_ds.cursor();

    sender.control(sender.controlId(formControlStr(SalesQuotationProjTable, DeviceUsageGroup))).enabled((salesQuotationTable.AMDeviceId) ? true : false);
}

AX7/D365/Operations: Create Details Master pattern form

Purpose:

The purpose of this document is to demonstrate how we can develop a Details Master form in Dynamics 365 for Operations. While doing so, it also shows how to apply the new form patterns and subpatterns.

Please note that the new Details Master pattern (Dynamics 365 for Operations) obsoletes the older ListPage pattern (AX 2012).

Prerequisites:

  • Access to Dynamics 365 for Operations instance via remote desktop.
  • To be provisioned as an administrator for the instance.
  • Visual Studio project, model, package have been created.
  • Custom table MAKInventRawMaterial has been created in the project.

Business requirement:

Ability to maintain records in a grid and detail view within the same form.

Development:

1. Click Ctrl+Shift+A to add new item to the project.
2. Navigate to User Interface > Form. Give a suitable name, MAKInventRawMaterial.

Form1

3. Drag and drop MAKInventRawMaterial table from the project to the Data Sources node.

Form2

4. Right-click Design node. Click Apply pattern > Details Master.

Form3

5. Once the pattern gets applied, open the Pattern pane to see the missing controls which need to be added to comply with the pattern.

Form4

6. Add the identified missing controls to the Design node in the same order as shown in the pattern tab.

Form5

7. As you can see after adding the required controls the violations have been but still there are some warnings.
8. Navigate to FormGroupControlNavigationList to view its Pattern tab. We still have missing controls for our Navigation List control.

Form6

9. Adding the identified missing controls resolves the pattern issues.

Form7

10. Repeat steps 6 – 9 till all the pattern and subpattern issues have been resolved by adding the missing controls. At the end of the process, you will have a completed form ready to add data source fields to.
11. Add the following field groups to the Details panel from data source table.

Form8DetailsTab

12. Add the AutoReport field group to the Grid control of the Grid panel.

Form9GridTab

13. Navigate to FormCommandButtonControl and set Command property to DetailsView.

Form10CommandButtonDetailsView

14. Navigate to FormGridControl and set Default Action property to the FormCommandButtonControl.

Form11GridDefaultAction

15. Right-click Project > Forms > MAKInventRawMaterial. Click Set as Startup Object.

StartupObject

16. Select project in your Solution Explorer and click Start in Visual Studio.
17. You should be able to see your form in action in the browser.
18. Grid view.

Form12GridView

19. Details view.

Form13DetailsView

AX7/D365/Operations: Create custom data entity

Purpose:

The purpose of this document is to demonstrate how we can develop a custom data entity for a custom table in Dynamics 365 for Operations.

Prerequisites:

  • Access to Dynamics 365 for Operations instance via remote desktop.
  • To be provisioned as an administrator for the instance.
  • Visual Studio project, model, package have been created.
  • Custom table has been created in the project.

Business requirement:

Ability to perform data operations on custom table.

Development:

We’ll be developing a data entity for a custom table, MAKInventRawMaterial for this scenario.

1. Click Ctrl+Shift+A to add new item to the project.
2. Select Data Model > Data Entity, giving name MAKInventRawMaterialEntity. Click Add.

DataEntity1

3. Specify data entity properties as shown below and select MAKInventRawMaterial table as the Primary datasource. Click Next.

DataEntity2

4. On the next screen of the wizard, review the data entity fields. You may choose to Convert labels to field names. Click Finish.

DataEntity3

5. The following highlighted development artifacts must be created.

DataEntity5

6. Open project properties and set Synchronize Database on Build to True to synchronize the newly created table and data entity with database.

DataEntity4

7. You have now successfully created a custom data entity!

To see the custom data entity in action, please see the following post.

https://dynamicsaxinsight.wordpress.com/2017/06/13/ax7-d365-operations-data-import-using-data-entities/