Hi partner,
Plug-in is a custom business logic which is developed by C# and can be integrated with D365 and be used to do some custom actions like create/modify/delete/query records automatically when the trigger conditions are met.
You could refer to the following steps.
1.Create a plug-in with C#. The code logic is when orders are updated, the plug-in will get the order number of the record and query order lines with the same order number of the order record and then set the order record as a value to lookup filed on order line form.
I've wrote a sample code for your reference.
using System;
using System.Linq;
using System.ServiceModel;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CRMPlugin
{
public class Test4s : IPlugin
{
///
/// A plug-in that updates lookup field in orderline when updating orders.
///
/// Register this plug-in on the Update message, order entity,
/// and asynchronous mode.
///
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
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.
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)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an order.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "order")
return;
try
{
//get the order number of order record,you should replace the field name with your correct field name in your order entity
string orderNumber = entity.Attributes["new_orderNumber"].ToString();
//get the order ID
string orderId = entity.Id.ToString();
//query order lines whose order number is the same as the order
var query = new QueryExpression("orderline")
{
ColumnSet = new ColumnSet("orderlineid"),
Criteria = new FilterExpression(LogicalOperator.And),
TopCount = 50
};
//add query condition if orderNumber(orderline) equals orderNumber(order)
query.Criteria.AddCondition("ordernume", ConditionOperator.Equal, orderNumber);
EntityCollection results = service.RetrieveMultiple(query);
results.Entities.ToList().ForEach(x =>
{
string orderlineId = x.Attributes["orderlineid"].ToString();
//update the orderline lookup field
var retrieveOrderLine = new Entity("orderline", new Guid(orderlineId));
var orderLine = new Entity("orderline");
orderLine.Id = retrieveOrderLine.Id;
//set the lookup value
orderLine["lookupfield"] = new EntityReference("orderline",new Guid(orderId));
service.Update(orderLine);
});
// Create the contact in Microsoft Dynamics CRM.
tracingService.Trace("TestPlug: Updating the OrderLine.");
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred", ex);
}
catch (Exception ex)
{
tracingService.Trace("TestPlug: {0}", ex.ToString());
throw;
}
}
}
}
}
2.And you need to register this plug-in with pluginregisterationtool.
For more steps, please refer to the following blogs.
https://carldesouza.com/deploying-plugin-across-different-dynamics-365-environments/
https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/register-deploy-plugins
https://crmbook.powerobjects.com/extending-crm/plug-in-development-and-workflow-extensions/plug-ins/registering-and-deploying-plug-ins/
Hope it helps.
Best Regasrds,
Leo