How to Create a Plugin for Contact Creation on Account Creation, with the Same Name?

How to Create a Plugin for Contact Creation on Account Creation, with the Same Name?

By following this blog, users can create a plugin that will automatically create a contact record in CRM whenever any new account record is created in CRM.

To implement this functionality, follow the below code.

Create a class File and paste the below code.

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContactCreation
{
public class ContactCreation : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference which you will need for
// web service calls.
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)
{
try {
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
Entity Contact = new Entity("contact");
Contact["lastname"] = entity.Attributes["name"];
Contact["telephone1"] = entity.Attributes["telephone1"];
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the Contact activity.");
service.Create(Contact);
}
catch (Exception Ex)
{
throw new NotImplementedException(Ex.Message);
}
}
}
}
}
Install necessary NuGET Packages. In the above code put Account Name as Contact LastName.
-> Contact["lastname"] = entity.Attributes["name"];

After that, build your project and update the registered plugin.

Step0001

Step0002

Conclusion:

Following the above plugin for Dynamics 365, users can create a contact with the same name as an account when the account is created.

All product and company names are trademarks™, registered® or copyright© trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.

Read Related Blogs About Dynamics 365 Portal

Power Apps Portal Pricing, Plans, and More

Power Apps Portal Pricing, Plans, and More

6 Min
Grow Your Business With Microsoft Dynamics 365

Grow Your Business With Microsoft Dynamics 365

3 Min
Enhancing Customer Success for Insurance Companies with Dynamics 365 Portal

Enhancing Customer Success for Insurance Companies with Dynamics 365 Portal

4 Min
To Top