locked
Create a record and associate it to related record in same executemultiplerequest RRS feed

  • Question

  • Hi All,

    I have written a custom logic in post create of custom entity(Entity A), in which i'm creating few child records(Entity B) and associating them to Entity A using executemultiplerequest(CreateRequest) .  I get a fault in the response, saying "Entity A with guid doesn't exists". But the same logic will work if I use IOrganizationService.Create method. Please help me in knowing why executemultiplerequest doesn't work.
    Thursday, January 5, 2017 9:29 AM

All replies

  • Please display your code

    Regards Faisal

    Thursday, January 5, 2017 9:51 AM
  • EntityCollection itemResourceCollection = RetrieveItemResources(itemResources, xrmService);

    var executeMultipleRequest = new ExecuteMultipleRequest
                    {
                        Settings = new ExecuteMultipleSettings
                        {
                            ContinueOnError = false,
                             ReturnResponses = true
                        },
                        Requests = new OrganizationRequestCollection()
                    };

                    foreach (var itemResource in itemResourceCollection.Entities)
                    {
                        var createRequest = new CreateRequest
                        {
                            Target = itemResource
                        };
                        executeMultipleRequest.Requests.Add(createRequest);
                    }

                    var response = (ExecuteMultipleResponse)xrmService.Execute(executeMultipleRequest);


    public virtual EntityCollection RetrieveItemResources(List<EntityItemResource> itemResources, XrmService xrmService)
            {
                var itemResourceCollection = new EntityCollection();

                foreach (var itemResource in itemResources)
                {
                    var entityItemResource = new Entity("new_resource");

                    entityItemResource.Attributes.Add("new_jobcardid", this.ToEntityReference());
                   //"this" is the target entity
                    foreach (var attributes in itemResource.ItemResourceFields)
                    {                    
                        if (attributes.Type == Constants.EntityReference)
                        {
                            var entityReference = RetrieveEntityReferenceForLookup(attributes.LookUpEntity, attributes.SearchField, attributes.AttributeValue, xrmService);
                            entityItemResource.Attributes.Add(attributes.AttributeName, entityReference);
                        }

                        else if (attributes.Type == Constants.optionset)
                        {
                            entityItemResource.Attributes.Add(attributes.AttributeName, new OptionSetValue(Convert.ToInt32(attributes.AttributeValue)));
                        }

                        else if (attributes.Type == Constants.money)
                        {
                            entityItemResource.Attributes.Add(attributes.AttributeName, new Money(Convert.ToDecimal(attributes.AttributeValue)));
                        }

                        else if (attributes.Type == Constants.WholeNumber)
                        {
                            entityItemResource[attributes.AttributeName] = Convert.ToInt32(attributes.AttributeValue);
                        }

                        else
                        {
                            entityItemResource[attributes.AttributeName] = attributes.AttributeValue;
                        }
                    }

                    itemResourceCollection.Entities.AddRange(entityItemResource);
                }

                return itemResourceCollection;
            }



    Thursday, January 5, 2017 10:05 AM
  • This code doesn't look to match to your original post - in the original post you say you are creating child records with ExecuteMultiple, but this code has UpdateRequests in the ExecuteMultiple

    Also, your original post refers to a 'post create', which is a term used in plugins. Is your code running in the post create event for entity A, or are you trying to do one ExecuteMultiple request that creates record A, and creates/updates records B ?


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, January 5, 2017 11:40 AM
    Moderator
  • Sorry for the type error in the code, it is CreateRequest not UpdateRequest. Yes, I'm writing the logic in plugin, post create event of Entity A. In the post create of Entity A, i'm creating records of Entity B and associating them with Entity A using executemultiplerequest in a batch.
    Thursday, January 5, 2017 11:59 AM
  • Thanks for the clarification. This may be a limitation of ExecuteMultiple within a plugin. If the plugin is running synchronously, then any changes you make using the derived IOrganizationService instance should run within the same transaction, so should have access to the new record A.

    It may be that ExecuteMultiple requests are run in a different transaction, which would explain the behaviour you see. I see 2 workarounds:

    • Use individual requests, rather than ExecuteMultiple. As your code is running in a plugin, you don't have any noticeable latency, so the performance benefits of ExecuteMultiple are not so significant
    • Register the plugin to run asynchronously. In this case, the original transaction would have committed before the ExecuteMultiple

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, January 5, 2017 2:53 PM
    Moderator