Enhancing User Engagement: Sending Push Notifications for Opportunity Status Changes in Dynamics 365 CRM
Ehtisham Siddiqui
Dynamics CRM | Power Platform | Dynamics 365 Business Central | AL | C#
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
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
Testing the Plugin
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.