Update/Create Parent enity and Child entities using ExecuteMultiple RRS feed

  • Question

  • I'm trying to import Accounts(Update them if I already have them)and import upto 4x Contacts(Related Entities) per account:

    I've got my solution working, but wanted to use ExecuteMultiple(I think this would improve performance dramatically) so created a collection of updates and creates.

    foreach (var entity in addUpdateList)
    	if (entity != null && (entity.Id == null || entity.Id == new Guid()))
            	CreateRequest createRequest = new CreateRequest { Target = entity };
            	UpdateRequest updateRequest = new UpdateRequest { Target = entity };

    I used "{ENTITY}.SetRelatedEntity(RELATIONSHIP,ParentENTITY)", but noticed that I got the following errors ""Cannot insert duplicate key."

    after plenty of reading I found an article that mentioned that "Each request is independent of other requests in the same batch, so you could not, for example, execute a CreateRequest followed by an UpdateRequest using the newly created ID within the same batch." I presume I can't create a parent and a child in the same call?

    Any suggestions?

    Tuesday, September 9, 2014 12:00 AM

All replies

  • Hello VanderMental,

     Is this a one time operation or a recurring task? If this is a one time operation, is performance really a concern?

    you can split the job into two batches - one to create all the accounts and another to create 4x contacts for the accounts created in first batch.

    Also please note that no two guids are same so your condition "entity.Id == new Guid()" will always return false. What you would like to do is entity.Id != Guid.Empty I guess. 

    you can also change the entity.Id == null  into entity.Id == Guid.Empty

    If you want to do it in single batch, you could generate new guids and create accounts by assigning this new guid as account id, and use the same account id to create contacts

    the drawback with above approach is that CRM has built in function to generate sequential guids to make indexing faster. once you generate your own ids, you will not have that benefit.

    So please consider all these facts before creating the data.



    Tuesday, September 9, 2014 2:24 AM