locked
How can I retrieve the id of an entity that has just been created. RRS feed

  • Question

  • Hi,

    I'm creating my first plugin where I want to create a "grm_contract" entity record when a salesorderdetail record is created (if the criteria is correct.) I have the plugin working fine only I would now like to add a lookup from the salesorderdetail record to the newly created grm_contract record.

    The code I have is as follows:

    using System; using System.ServiceModel; using System.Runtime.Serialization; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace GRMPlugins { public class CreateGRM_Contract : IPlugin { 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)); // 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 product. // If not, this plug-in was not registered correctly. if (entity.LogicalName != "salesorderdetail") return; try { // Get the product type value from salesorderdetail so we can tell if this is a contract product or not. int product_type = ((OptionSetValue)entity["grm_product_type"]).Value; // If grm_product_type is set to 172300001 (Contract Product) then proceed to create the contract record. if (product_type == 172300001) { // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Get id of the parent order record (salesorder). Entity parent_order = service.Retrieve("salesorder", ((EntityReference)entity["salesorderid"]).Id, new ColumnSet("customerid")); Guid parent_order_id = parent_order.Id; // Use parent_order_id to get customerid of the parent order record. Entity parent_order_customer = service.Retrieve("account", ((EntityReference)parent_order["customerid"]).Id, new ColumnSet(false)); Guid customerid = parent_order_customer.Id; // Get remaining fields from salesorderdetail OptionSetValue contract_type_value = entity.Attributes["grm_contract_type"] as OptionSetValue; // Contract type as value (e.g. 172XXXXXX) bool renew_contract = entity.GetAttributeValue<bool>("grm_renew_this_contract"); // Create a grm_contract entity. Entity createcontract = new Entity("grm_contract"); createcontract["grm_contract_type"] = contract_type_value; createcontract["grm_customer"] = new EntityReference("account", customerid); createcontract["grm_originating_order"] = new EntityReference("salesorder", parent_order_id); createcontract["grm_renew_this_contract"] = renew_contract; // Create the grm_contract record in Microsoft Dynamics CRM. tracingService.Trace("GRMContractPlugin: Creating the contract record."); service.Create(createcontract);


    // SOMETHING HERE TO GET THE ID OF THE NEWLY CREATED CONTRACT AND SET THE ID ON A LOOKUP FIELD (grm_contract_lookup) ON THE SALESORDERDETAIL RECORD

    } } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occurred in the plug-in 'GRMContractPlugin'.", ex); } catch (Exception ex) { tracingService.Trace("GRMContractPlugin: {0}", ex.ToString()); throw; } } } } }

    Any ideas how I can get the ID of this newly created contract and set it against a lookup field (grm_contract_lookup) on the originating salesorderdetail record? I'd rather do this in the above plugin rather than having to have a second plugin registered on create of a grm_contract entity record


    • Edited by JamesGRM Monday, May 25, 2015 12:25 PM wrong term entered in the description
    Monday, May 25, 2015 12:24 PM

Answers

  • Hi,

    Guid YourEntityId = service.Create(Entity);

    is your code.

    More examples:

    https://msdn.microsoft.com/en-us/library/bb928994.aspx?f=255&MSPPError=-2147217396


    • Edited by xjomanx Monday, May 25, 2015 1:10 PM
    • Marked as answer by JamesGRM Monday, May 25, 2015 1:29 PM
    Monday, May 25, 2015 1:10 PM

All replies

  • Hi,

    Guid YourEntityId = service.Create(Entity);

    is your code.

    More examples:

    https://msdn.microsoft.com/en-us/library/bb928994.aspx?f=255&MSPPError=-2147217396


    • Edited by xjomanx Monday, May 25, 2015 1:10 PM
    • Marked as answer by JamesGRM Monday, May 25, 2015 1:29 PM
    Monday, May 25, 2015 1:10 PM
  • Perfect, that works now, thank you!
    Monday, May 25, 2015 1:29 PM