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.

Riddhi Faldu

Riddhi Faldu

Riddhi is a firm believer of serenity and writing is something that keeps her grounded. Being a Computer Engineer by qualification, understanding and writing about tech comes to her naturally. It wouldn't be wrong to call her diligent as she likes pushing herself and others to bring the best on table. In leisure time, she likes practicing calligraphy and wishes to master the beautiful concoction of ink and words.

Read Related Blogs About Dynamics 365 Portal

How to Clear the Cache in the Power Apps Portal

How to Clear the Cache in the Power Apps Portal

3 Min
Revolutionizing Financial Services with Dynamics 365 Customer Portal

Revolutionizing Financial Services with Dynamics 365 Customer Portal

5 Min
Why You Need D365 Developers in Your Team

Why You Need D365 Developers in Your Team

4 Min
To Top