Enhancing User Engagement: Sending Push Notifications for Opportunity Status Changes in Dynamics 365 CRM

Enhancing User Engagement: Sending Push Notifications for Opportunity Status Changes in Dynamics 365 CRM

Introduction

In Dynamics 365 CRM, keeping users informed about the status of important records like opportunities is crucial. One effective way to achieve this is through in-app notifications. This article outlines the steps to send a push notification to the owner of an opportunity when it is marked as won or lost.


Enabling In-App Notifications

Before you can send notifications, you need to enable the in-app notification feature in your model-driven app.



Steps to Enable In-App Notifications

  1. Sign in to Power Apps:
  2. Open the Solution:
  3. Enable In-App Notifications:
  4. Save and Publish:

Sending In-App Notifications Using a Plugin

To send a notification when an opportunity is marked as won or lost, you'll need to create a plugin. This plugin will trigger on the SetState or SetStateDynamicEntity message for the opportunity entity.

Plugin Code

Below is an example of the plugin code that sends a notification when the opportunity status changes:



using System;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

public class OpportunityStatusChangePlugin : IPlugin

{

public void Execute(IServiceProvider serviceProvider)

{

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)

{

EntityReference entityMoniker = (EntityReference)context.InputParameters["EntityMoniker"];

if (entityMoniker.LogicalName == "opportunity")

{

OptionSetValue state = (OptionSetValue)context.InputParameters["State"];

if (state.Value == 1 || state.Value == 3) // 1 = Won, 3 = Lost

{

Entity opportunity = service.Retrieve(entityMoniker.LogicalName, entityMoniker.Id , new ColumnSet("ownerid"));

if (opportunity != null && opportunity.Attributes.Contains("ownerid"))

{

EntityReference ownerRef = (EntityReference)opportunity["ownerid"];

string status = state.Value == 1 ? "won" : "lost";

string message = $"The opportunity with ID: {entityMoniker.Id } has been {status}.";

SendAppNotification(service, ownerRef.Id , status, message);

}

}

}

}

}

public static void SendAppNotification(IOrganizationService service, Guid userId, string status, string message)

{

var request = new OrganizationRequest()

{

RequestName = "SendAppNotification",

Parameters = new ParameterCollection

{

["Title"] = $"Opportunity {status}",

["Recipient"] = new EntityReference("systemuser", userId),

["Body"] = message,

["IconType"] = new OptionSetValue(100000000), //info

["ToastType"] = new OptionSetValue(200000000) //timed

}

};

service.Execute(request);

}

}



Deploying the Plugin

  1. Build the Plugin Project:
  2. Use the Plugin Registration Tool:

Testing the Plugin

  • After registering the plugin, change the status of an opportunity to won or lost.
  • Ensure that the owner of the opportunity receives a notification in their CRM application.



Conclusion

By following these steps, you can configure Dynamics 365 CRM to send push notifications to the owner of an opportunity when it is marked as won or lost. This ensures that users are promptly informed of important changes, helping to keep them engaged and informed.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了