locked
CRM 2011 Create record and get its GUID RRS feed

  • Question

  • Greetings.

    I have a post-create plug-in that is triggered after creation of a record in ENTITY_A and should be performing the following:

    1) Create a record in ENTITY_B

    2) Get GUID of record created in 1)

    3) Update a lookup in ENTITY_A with GUID retrieved in 2)

    It seems to be straightforward and yet in spite of many trials, I haven't been able to make this work.

    Thank you for your help/suggestions.


    Frank

    Tuesday, March 24, 2015 6:59 PM

Answers

  • You're right, that is a pretty straightforward problem.  The best advice I can give is get comfortable with the Plugin Profiler that way you can step through the code.

    Here's some code pulled a MSDN sample and tweaked for your situation (another relevant sample):

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    Entity entityBToCreate = new Entity("new_entityB");
    entityBToCreate["Name"] = "Example EntityB";
    
    Guid _newId = _service.Create(entityBToCreate);
    
    // Obtain the target entity from the input parameters.
    Entity targetEntity = (Entity)context.InputParameters["Target"];
    
    targetEntity["Entity_A_Lookup"] = new EntityReference("new_entityB", _newId);
    

    If you post your code or errors it will be easier to assist.

    Tuesday, March 24, 2015 10:54 PM

All replies

  • You're right, that is a pretty straightforward problem.  The best advice I can give is get comfortable with the Plugin Profiler that way you can step through the code.

    Here's some code pulled a MSDN sample and tweaked for your situation (another relevant sample):

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
    Entity entityBToCreate = new Entity("new_entityB");
    entityBToCreate["Name"] = "Example EntityB";
    
    Guid _newId = _service.Create(entityBToCreate);
    
    // Obtain the target entity from the input parameters.
    Entity targetEntity = (Entity)context.InputParameters["Target"];
    
    targetEntity["Entity_A_Lookup"] = new EntityReference("new_entityB", _newId);
    

    If you post your code or errors it will be easier to assist.

    Tuesday, March 24, 2015 10:54 PM
  • hi frank,

    try to run the plugin in async mode so that this plugin will not be running in the same transaction.


    Thanks & Regards Vijji

    Wednesday, March 25, 2015 7:45 AM