Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Create a record and associate it to related record in same executemultiplerequest

Posted on by Microsoft Employee

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.

Below is the code:

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;
        }

*This post is locked for comments

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans