Hi all,
I have a requirement, where I need to update contact fields on create event before the creation(Pre-Operation).
Like I have integrated the Dynamics with the third party application and from there I am creating the contacts but the time of creation few address fields are mandatory on contact level, which is blank on third party app side, so during the sync operation from third party app to dynamics 365, records is not getting create because of these mandatory fields are blank.
So I am thinking like, I will write a plugin to update those mandatory fields as N/A, during the creation of contact records so that creation of contact will not be fail.
And I am trying to Update one string type of id field (which is I am getting during the sync process from third party app) to another string type of field for my reference.
But the issue is, I am getting the null value in every fields which is having the values during the sync operation.
Anyone has any Idea about it, please provide your inputs.
I have pasted my code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace ACL.Plugins.Fintrx
{
public class PrePopulatingAccountsField : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory orgFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService orgService = orgFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.Depth > 1)
{
tracingService.Trace(/Inside Depth Check, Depth is grater than 1. Actual Depth:/ + context.Depth);
return;
}
// Obtain the target entity from the input parameters.
Entity target = null;
Entity entPreImage = null;
if (context.MessageName.ToLower().Equals(/create/))
{
target = (Entity)context.InputParameters[/Target/];
tracingService.Trace(/Create Event Triggered.../);
}
string firmcrdid = target.Contains(/abc_firmcrdid /) ? target.GetAttributeValue<string>(/abc_firmcrdid /) : null;
tracingService.Trace(/updated firmcrdid: /+ firmcrdid);
if (firmcrdid != null )
{
target[/abc_id/] = firmcrdid;
tracingService.Trace(/Copied the firmcrdid./);
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}