You can use this piece of code for your requirement..
using System;
using Microsoft.Xrm.Sdk;
namespace YourNamespace
{
public class AssignCaseToDummyCustomerPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Check if the plugin is triggered by the creation of a new case
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity &&
context.PrimaryEntityName == "incident" &&
context.MessageName == "Create")
{
// Get the new case record
Entity newCase = (Entity)context.InputParameters["Target"];
// Check if the case has a customer (account/contact) associated with it
if (newCase.Attributes.Contains("customerid"))
{
// Get the customer id
EntityReference customerRef = (EntityReference)newCase.Attributes["customerid"];
// Check if the customer is a contact
if (customerRef.LogicalName == "contact")
{
// Obtain the organization service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Update the case to assign it to the dummy customer
Entity updatedCase = new Entity("incident");
updatedCase.Id = newCase.Id;
updatedCase["customerid"] = new EntityReference("account", Guid.Parse("YOUR_DUMMY_CUSTOMER_ID_HERE"));
service.Update(updatedCase);
}
}
}
}
}
}
Hope this helps..
If my response was helpful, please click 'Like.' To confirm the solution, mark it as 'Verified' to help others find right solutions. For further queries, feel free to reach out to me.
Blog:https://ecellorscrm.com
Cheers,
PMDY