**On Creation of Account, Create a Contact and Displaying Associated Record in Account Entity Form using Plugin Code in MS Dynamics CRM**
Recently I have face a scenario which requires to display associated records when account gets created along with related contact creates. Finally that record has to be displayed on Account Entity Form.
Actually there is a relationship between Account and Contact. Relationship name is "account_primary_contact". We want to display record in this field.
I have written following plugin code and registered on Post Account Creation. After executing this code associated record will display in account entity form.
Following Screen shots shows result of this scenario.
========================
PLUGIN CODE
========================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
namespace Account.Create.Contact
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target"))
{
if (context.InputParameters["Target"] is Entity)
{
Entity account = (Entity)context.InputParameters["Target"];
Guid accountId = account.Id;
if (account.LogicalName == "account")
{
try
{
Entity contact = new Entity("contact");
contact["firstname"] = "ggg";
contact["lastname"] = "www";
Guid contactid = service.Create(contact);
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", accountId));
Relationship relationship = new Relationship("account_primary_contact");
service.Associate("contact", contactid, relationship, relatedEntities);
}
catch (Exception e) { throw new InvalidPluginExecutionException(e.ToString()); };
}
else
return;
}
}
}
}
}
Recently I have face a scenario which requires to display associated records when account gets created along with related contact creates. Finally that record has to be displayed on Account Entity Form.
Actually there is a relationship between Account and Contact. Relationship name is "account_primary_contact". We want to display record in this field.
I have written following plugin code and registered on Post Account Creation. After executing this code associated record will display in account entity form.
Following Screen shots shows result of this scenario.
========================
PLUGIN CODE
========================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
namespace Account.Create.Contact
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target"))
{
if (context.InputParameters["Target"] is Entity)
{
Entity account = (Entity)context.InputParameters["Target"];
Guid accountId = account.Id;
if (account.LogicalName == "account")
{
try
{
Entity contact = new Entity("contact");
contact["firstname"] = "ggg";
contact["lastname"] = "www";
Guid contactid = service.Create(contact);
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", accountId));
Relationship relationship = new Relationship("account_primary_contact");
service.Associate("contact", contactid, relationship, relatedEntities);
}
catch (Exception e) { throw new InvalidPluginExecutionException(e.ToString()); };
}
else
return;
}
}
}
}
}