Hi User,
To write and register a plugin in Dynamics 365 to create a contact when an account is created in the system, follow these steps:
1. Create a new plugin project. You can use Visual Studio to create a class library-type project in the .net framework for plugin in c#. After that, you need to add references for Microsoft SDK mentioned below:
Microsoft.Crm.Sdk.Proxy.dll
Microsoft.Xrm.Sdk.dll
Microsoft.Xrm.Tooling.Connector.dll
2. Implement the IPlugin interface. Your plugin class must implement the IPlugin interface, which defines a single IPlugin.Execute method.
3. Write the code to create a contact. In the IPlugin.Execute method, write the code to create a new contact record. You can use the IOrganizationService interface to create the contact record.
Here is an example of a plugin that creates a contact when an account is created:
public class CreateContactPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the IOrganizationService interface.
IOrganizationService service = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));
// Get the entity that is being created.
Entity entity = (Entity)serviceProvider.GetService(typeof(Entity));
// Create a new contact record.
Entity contact = new Entity("contact");
contact["lastname"] = entity.Attributes["name"];
contact["telephone1"] = entity.Attributes["telephone1"];
// Create the contact record.
service.Create(contact);
}
}
4. Register the plugin. To register the plugin, you can use the Plug-in Registration Tool or the Power Platform CLI.
5. Test the plugin. Once the plugin is registered, you can test it by creating a new account record. If the plugin is working correctly, a new contact record will be created automatically.
To register the plugin, you can use the Plug-in Registration Tool:
1. Open the Plug-in Registration Tool and sign in with environment credentials.
2. Click the Register New Assembly button.
3. Browse to the plugin assembly and click the Open button.
4. Click the Load Assembly button.
5. Expand the assembly node and right-click the (Plugin) CreateContactPlugin.
6. Select Register New Step.
7. In the Register New Step dialog box, select the following values:
1. Message: Create
2. Primary Entity: Account
3. Event Pipeline Stage of Execution: PostOperation
4. Execution Mode: Asynchronous
Click the Register button.
Once the plugin is registered, it will be triggered whenever an account record is created. The plugin will create a new contact record with the same name and telephone number as the account record.
Hope this helps!
Please mark verified if answer is helpful!