The below example shows how to create an Contact record whenever we had an account record getting created.
Find the code as below.
Step 1:
using System;
using Microsoft.Xrm.Sdk;// include this dll from SDK
namespace plugin_721
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider service1)
{
IPluginExecutionContext context = (IPluginExecutionContext)service1.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];
entity.LogicalName = “account”;
try
{
Entity createcontact = new Entity(“contact”);
if (context.PrimaryEntityId != null)
{
createcontact[“firstname”] = “pavan”;
}
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)service1.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
service.Create(createcontact);
}
catch (Exception ex)
{
//code you for displaying when exception was caught, not compulsory
}
}
}
}
}
Step 2:
Dont forget to sign in the assembly.
Build the code in the visual studio to get the dll file.
Step 3:
Open Plugin registration tool from crm 2016 SDK\SDK\Tools\PluginRegistration
- create a new connection
- give the organization login details select O365 if online
- Select your region
- Displays the organizations if found
- Click Register–>Register new Assembly–>Select your DLL
- Select sandbox & Database if online–> Register
- Goto the Assembly and create a new step for that
- Select the details asked, can select required pipeline execution order, stage, execution mode, deployment
- Enter register step
- Your plugin was registered
- Go and check, when you had created a new account, this code would create a contact pavan(You can give whatever you want)