dynamics-crmdynamics-marketing

Microsoft Dynamics 365 Marketing: Is there a way to update a custom entity when an email is sent


I'm using Microsoft Dynamics 365 CRM with the Marketing module.

When a marketing journey sends an email, I want to create a record in a custom entity containing the contacts details and details of the email sent.

I can't find a way to intercept the email send process in order to run some custom code, e.g. a plugin

Is there a way, or is it not possible with the marketing module?


Solution

  • "Send email" is an action on the email table. You can create a custom button with HTML, PCF controls, or ribbon workbench.

    If you decided to got for the Ribbon this js should take care of it:

    /* jslint browser: true, for: true, this: true */
    /* global Xrm, Common, BBYH, BUSINESS_REQUIREMENT_LEVEL */
    if (!window.hasOwnProperty("Org")) {
      window.Org = {};
    }
    
    Org.emailRibbon = (function() {
      "use strict";
      const Helpers = {
        handleEmail: async(primaryControl) => {
          const formContext = primaryControl.ui.formContext;
          //use formContext to get your guid
          const executeSendEmailRequest = {
            // Parameters
            entity: {
              entityType: "email",
              id: "<guid here>"
            }, // entity
            IssueSend: true, // Edm.Boolean
            TrackingToken: "Optional Token", // Edm.String
    
            getMetadata() {
              return {
                boundParameter: "entity",
                parameterTypes: {
                  entity: {
                    typeName: "mscrm.email",
                    structuralProperty: 5
                  },
                  IssueSend: {
                    typeName: "Edm.Boolean",
                    structuralProperty: 1
                  },
                  TrackingToken: {
                    typeName: "Edm.String",
                    structuralProperty: 1
                  }
                },
                operationType: 0,
                operationName: "SendEmail"
              };
            }
          };
    
          try {
            const response = await Xrm.WebApi.execute(executeSendEmailRequest);
            if (response.ok) {
              const responseBody = await response.json();
              console.log(responseBody);
              // Return Type: mscrm.SendEmailResponse
              // Output Parameters
              const subject = responseBody["Subject"]; // Edm.String
              return subject;
            }
          } catch (error) {
            console.log(error.message);
          }
        }
      };
    
      return {
        handleEmail: Helpers.handleEmail
      };
    })();

    Then just create your button and add Org.emailRibbon.handdleEmail to the command:

    enter image description here

    Then you can do whatever you want with it. I used Dataverse REST Builder to create part of the code. But be careful, most times it doesn't generate very good code. If you use it to create an "execute multiple" or "create record" or anything else, make sure you apply ES6 best practices.