using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
public class OpportunityCreatePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an opportunity.
if (entity.LogicalName != "opportunity")
return;
if (entity.Attributes.Contains("description"))
{
// Get the description field value.
string description = entity.Attributes["description"];
// Split the description into GUIDs.
string[] guids = description.Split(',');
// Check if we have three GUIDs.
if (guids.Length == 3)
{
foreach (string guid in guids)
{
// Parse the GUID.
Guid userGuid = new Guid(guid.Trim());
// Create a reference to the user.
EntityReference userRef = new EntityReference("systemuser", userGuid);
// Create a new relationship entity record.
Entity relationship = new Entity("new_opportunity_sales_team");
relationship["new_opportunityid"] = entity.ToEntityReference();
relationship["new_userid"] = userRef;
// Create the relationship entity record.
service.Create(relationship);
}
}
}
}
}
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,113 Super User 2024 Season 2
Martin Dráb 229,918 Most Valuable Professional
nmaenpaa 101,156