Upcoming Free Webinar on Empowering NGOs | 27th Sept 2023 | 7:30 PM IST | 10:00 AM EDT

Enroll TODAY!
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

How to Apply Field-Level Security in System Fields in CRM?

How to Apply Field-Level Security in System Fields in CRM?

4 Min
How to Create a Pre-Validation  Plugin by Plugin Registration Tool?

How to Create a Pre-Validation  Plugin by Plugin Registration Tool?

3 Min
How to Store and Retrieve Images in D365 by JavaScript?

How to Store and Retrieve Images in D365 by JavaScript?

3 Min
To Top