After PO Confirmation Send Email to the End user In D365 X++

Requirement:

We need to trigger the email to end user whenever PO has been confirmed from D365.

Before start the implementation we need the following.

  1. Take the Email ID without Multifactor Authentication
  2. Create/Update the SMTP Configuration Permission in D365.
  3. Permission for Create the New User Group in D365
  4. D365 X++ Development environment.

SMTP Configuration:

Open D365  >> Module >> System Administration >> Setup >> Email >> Email Parameters .

1.    Select "SMTP" in the Batch email provider drop down under the "Configuration" Setup



 2.    Configure the SMTP Settings with your disabled multifactor email.



Note: After configure the SMTP setting, check the "Test Connection." you should get the "Success" Message.

New User Group in D365

Use the following setup for create new user group in D365 Application.

1.    Go to D365 >> Modules >> System Administration >> Users  >> Users Group.

2.    Click New >> Create New User group ( 'XXX' Same you need to use it in to Source Code.

3.    After Create the Group >> add the user in to the group who ever want to receive the email for po confirmation.


D365 X++ Development

Step 1: 

Create New/Open Existing the Visual Studio Solution under the correct Model

Step 2: 

Go to AOT >> Data Model >> Tables >> Select PurchTable >> Right Click and Select the "Create Code Extension" or "Create Code extension in New Project"



Note : Now PurchTable Code extension will create under your projects.

Step 3: 

Write the following code :

[ExtensionOf(tableStr(PurchTable))]

final class PurchTable_YOURMODELXXX_Extension

{

       public void update(boolean _updateDistributions)

    {

        PurchTable _purchTable ;

        next update(_updateDistributions);

        if(this.canConfirmationRequestBeUpdated())

        {

            select _purchTable where _purchTable.PurchId == this.PurchId &&

            _purchTable.DocumentState == VersioningDocumentState::Confirmed;

            if(_purchTable)

            {

                PurchTable::SentEmail(this.PurchId);

            }

        }

    }

    public static void SentEmail(PurchId purchId = PurchId)

    {

        SysEmailParameters _SysEmailParameters;

        var builder = new SysMailerMessageBuilder();

        select SMTPUserName from _SysEmailParameters;

        builder.setFrom(_SysEmailParameters.SMTPUserName);

        str toUserId ;

        UserGroupList   userGroupList;

        SysUserInfo     SysUserInfo;

        while select userGroupList

            where userGroupList.groupId == 'XXX' -- Create new user group in D365 and map it here.

            Join SysUserInfo

            where SysUserInfo.Id == userGroupList.UserId

        {

            toUserId += SysUserInfo.Email + ';';

        }

        builder.addTo(strFmt("%1",toUserId));

        str subject = "Alert ! - PO # :( "+ purchId+" ) Confirmed " ; // Email subject

        str body = "<HTML> <body> Hi Team, <Br><Br>Have a Good Day!<Br><Br> The following purchase order # : <B>"+ purchId +" </B> has been confirmed.<Br><Br>";

        body += " Please take a necessary action. Do not reply this is an automated email sent from D365 F&O.<Br><Br><Br> Thanks, <Br> PO Confirmation Job.";

        body += "</body></HTML>";

        builder.setSubject(subject);

        builder.setBody(body);

        var message = builder.getMessage();

        SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(message);

        Info("Email Sent Successfully");

    }

Step 4: 

Build and Sync the Source code from Visual Studio.

Step 5: 

Check and confirm the same.

Sample Email:

You will receive the following email format.



Comments

Popular posts from this blog

DMF (1073676279) - Issue Fix

Read the JSON data via REST API (X++)

Activate Financial Dimension in Dynamics 365 FO. (On- Premises)