locked
CRM 2011-Plugin shows Error for Multiple create RRS feed

  • Question

  • Hi Experts,

    I am getting error when try to create multiple records for a custom entity in a plugin.

    Actually I have one list which holds a number of records .

    I added the entity.Create(entityObject) within the foreach loop up to the number of items present inside the List.

     foreach (var item in lstProp)
                                {
                                    entity.Attributes.Add("new_name", " No.-  " + i);
                                    entity.Attributes.Add("new_key", item.Key);
                                    entity.Attributes.Add("new_type", item.Type);
                                    entity.Attributes.Add("new_code", item.Code);
                                    entity.Attributes.Add("new_description", item.Description);
                                    service.Create(entity);
                                    i += 1;
                                }

    Error:An item with same key has already been added

    Can you please suggest how I will execute this plugin?

    Thanking you


    • Edited by Ashis_K Wednesday, January 16, 2013 6:28 AM
    Wednesday, January 16, 2013 6:25 AM

Answers

  • My Friend,

    You are trying to create the same entity in create plugin, so it surely case of infinite loop.

    Do you want to create multiple instance of the same entity, while user will create entity record ??

    you could try to check depth property to exit infinite loop (exit from your plugin) after record is created (you need to check with number of the record that you want to create).


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Edited by HIMBAPModerator Wednesday, January 16, 2013 7:15 AM
    • Marked as answer by Ashis_K Wednesday, January 16, 2013 7:22 AM
    Wednesday, January 16, 2013 7:14 AM
    Moderator
  • Hi Mahender,

    Thank you, I checked the depth property and added the below code

     if (context.Depth > 1)
                    return;

    Now my plugins working fine.

    Thanks to all my CRM Experts.

    • Marked as answer by Ashis_K Wednesday, January 16, 2013 7:22 AM
    Wednesday, January 16, 2013 7:22 AM

All replies

  • what is the error?



    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Wednesday, January 16, 2013 6:27 AM
  • Error:An item with same key has already been added
    Wednesday, January 16, 2013 6:28 AM
  • that must be from lstProp,can you please show me your code,so we can resolve. 


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Wednesday, January 16, 2013 6:38 AM
  • Hi,

    Please try this :

    Enity entity;
     foreach (var item in lstProp)
                                {
                                   entity=new Entity("customentityname");
                                    entity.Attributes.Add("new_name", " No.-  " + i);
                                    entity.Attributes.Add("new_key", item.Key);
                                    entity.Attributes.Add("new_type", item.Type);
                                    entity.Attributes.Add("new_code", item.Code);
                                    entity.Attributes.Add("new_description", item.Description);
                                    service.Create(entity);
                                    i += 1;
                                }


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Wednesday, January 16, 2013 6:41 AM
    • Proposed as answer by pogo69 Wednesday, January 16, 2013 6:45 AM
    Wednesday, January 16, 2013 6:38 AM
  • Hi Vikram,

    I tried with your suggestion but it is show error as infinite loop.

    Wednesday, January 16, 2013 6:44 AM
  • Hi san,

    I gave the code in my question.

    lstProp is the list which contains multiple items.

    Wednesday, January 16, 2013 6:45 AM
  • got the problem,

    add an attribute entity.Id=Guid.NewGuid();


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    • Edited by san Sanz Wednesday, January 16, 2013 6:46 AM
    Wednesday, January 16, 2013 6:46 AM
  • Hi Bunty,

    Please share your lstProp complete code also (declaration and insertion for list).

    Assuming your custom entity having all fields datatypes are Single Line of Text.

      entity.Attributes.Add("new_name", " No.-  " + i); 
                                    entity.Attributes.Add("new_key", item.Key);
                                    entity.Attributes.Add("new_type", item.Type);
                                    entity.Attributes.Add("new_code", item.Code);
                                    entity.Attributes.Add("new_description", item.Description);


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Wednesday, January 16, 2013 6:56 AM
  •                               

    entity.Attributes.Add("new_key", item.Key);

    what is key field, is it loolup ?? 



    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Wednesday, January 16, 2013 6:56 AM
    Moderator
  • Hi San,

    This time I used the code like below,but getting error

      Entity entity = (Entity)context.InputParameters["Target"];                  
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                        if (entity.LogicalName == "new_customEntity")
                        {

                             int i = 1;
                                foreach (var item in lstProp)
                                {
                                    entity = new Entity("new_customEntity");
                                    entity.Id = Guid.NewGuid();
                                    entity.Attributes.Add("new_name", "No.-  " + i);
                                    entity.Attributes.Add("new_key", item.Key);
                                    entity.Attributes.Add("new_type", item.Type);
                                    entity.Attributes.Add("new_code", item.Code);
                                    entity.Attributes.Add("new_description", item.Description);
                                    service.Create(entity);
                                    i += 1;
                                }                   

                        }

    If I am running the above query getting infinite loop error.

    If I commented the line  entity = new Entity("new_customEntity"); getting 

    Entity Id must be the same as the value set in property bagDetail error.

    please suggest the proper way.

    Thanks


    • Edited by Ashis_K Wednesday, January 16, 2013 7:00 AM
    Wednesday, January 16, 2013 7:00 AM
  • Hi mahender,

    All fields are string field.

    Wednesday, January 16, 2013 7:03 AM
  • My Friend,

    You are trying to create the same entity in create plugin, so it surely case of infinite loop.

    Do you want to create multiple instance of the same entity, while user will create entity record ??

    you could try to check depth property to exit infinite loop (exit from your plugin) after record is created (you need to check with number of the record that you want to create).


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Edited by HIMBAPModerator Wednesday, January 16, 2013 7:15 AM
    • Marked as answer by Ashis_K Wednesday, January 16, 2013 7:22 AM
    Wednesday, January 16, 2013 7:14 AM
    Moderator
  • Hi Mahender,

    Thank you, I checked the depth property and added the below code

     if (context.Depth > 1)
                    return;

    Now my plugins working fine.

    Thanks to all my CRM Experts.

    • Marked as answer by Ashis_K Wednesday, January 16, 2013 7:22 AM
    Wednesday, January 16, 2013 7:22 AM