locked
CRM 2011 - Plugins - Target Entity Update all record issue - HELP me understand RRS feed

  • Question

  • Dear Experts/Gurus/MVP/Anybody,

    Conceptually i am not clear about something and would appreciate if somebody can help me understand the mechanism.

    My scenario is:

    • I have parent entity Contact and child entity Contact Address
    • One contact can have multiple Contact Addresses
    • On Contact address i have boolean field called "Primary Address", which tells that only contact address record is primary. (so for example, lets assume a contact has addresses A, B, C, now if i set the flag = true on address B, the flag on all the existing addresses A & C has to be NO

    Issue and Solution

    This issue is working and i have fixed the problem but conceptually i am still not as to WHY???

    • Please note for my i have registered the plugin as PRE - Synchronous for the entity
    • The plugin is binded on new_contactaddress table

    Code:

               // Created Target Entity instance (that has current contact address B selected)

             Entity targetEntity = null;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    targetEntity = (Entity)context.InputParameters["Target"];
                    if (targetEntity.LogicalName != "new_contactaddress")
                    { return; }
                }

                targetEntity.Attributes["new_street2"] = (string)"Crm testing";        //problem line of code


                 // building the query expressions so i can loop through all the records with flag = false
                           QueryExpression userQuery = new QueryExpression
                            {
                                EntityName = targetEntity.LogicalName,
                                ColumnSet = new ColumnSet("new_contactaddressesid", "new_primaryaddress","new_street1","new_street2"),
                                Criteria =
                                {
                                    Conditions = {
                                            new ConditionExpression("new_contactaddressesid",ConditionOperator.Equal,contactaddid) // i have retrieved this id using PreImage... dont worry
                                    }
                                }
                            };

                            DataCollection<Entity> ContactAddresses = service.RetrieveMultiple(userQuery).Entities;
                            foreach (Entity caddress in ContactAddresses)
                            {
                                caddress.Attributes["new_primaryaddress"] = false;
                                caddress.Attributes["new_street2"] = ii.ToString();
                                service.Update(caddress);
                            }

    Issue:

    If you notice the above code, there are two lines of code with bold and italic, in the first line of code i am hard coding the value for new_street2 = Crm testing

    But if you notice in the loop that runs for all the addresses i am setting the value new_street2 = Loop counter

    My understanding is when the above code is run, value in the street2 should be 1 for address A, 2 for Address B, 3 for Address C but instead i can see Crm testing in all the three addresses in Street 2 field.

    I really find this bizarre and i don't have any clue why it is updating the data for all when the are only defined once through target entity and down in the loop we are updating it anyway.

     

    The solution to above issue is to move new_street2 = Crm testing after the loop and works perfect, but i want to know why it is behaving so strangely with the above code. It is very strange. 

     

    I WILL BE ANXIOUSLY WAITING FOR YOUR FEEDBACK.

    Tuesday, July 26, 2011 12:00 AM

Answers

  • I was working on "Contact Address" and "Account Address" part together.

    The solution to the entire issue is posted at:

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/72de5921-d367-4801-a491-edc9cdd523ed

     

    Thanks

    • Marked as answer by itsgonabeme Thursday, August 11, 2011 11:34 AM
    Thursday, August 11, 2011 11:34 AM

All replies

  • Hi,

    I think the service.Update(caddress); is causing issue if you have registered your plugin on Update message, because you are updating the same entity record as the plugin is beging registered with, the plugin will fire again & again.

     


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, July 26, 2011 7:24 AM
  • Hey Thomas,

    Yes the plugin is registered to Update event and what you are saying here, def make sense.

    But if service.update is the problem then i have two observations, can you please confirm if am on the same page as you are.

    • Shouldn't we be part of the infinite loop because of service.update?
    • If i move the line before for loop new_street2 = Crm testing and place it after the loop, No problem it updates the data in street1 correctly
    • Also if i take the service.update off, how do i set the PrimaryAddress = False for the other records.

    Look forward to hear back from you.

     

    Thanks

    Tuesday, July 26, 2011 7:40 AM
  • I was working on "Contact Address" and "Account Address" part together.

    The solution to the entire issue is posted at:

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/72de5921-d367-4801-a491-edc9cdd523ed

     

    Thanks

    • Marked as answer by itsgonabeme Thursday, August 11, 2011 11:34 AM
    Thursday, August 11, 2011 11:34 AM