locked
ICrmService.Update method not working correctly RRS feed

  • Question

  • I'm having issues getting the ICrmService.Update method to work correctly.  Here is my code:

    //This is the Execute function


    OnBoardingRecord[] levelOneEntities; ICrmService service = (ICrmService)context.CreateCrmService(false); levelOneEntities = function_that_gets_DynamicEntites(); Array.Sort(levelOneEntities); for (int i = 0; i < levelOneEntities.Length; i++) { DynamicEntity de = levelOneEntities[i].getDynamicEntity(); CrmNumber temp = new CrmNumber(); temp.Value = i+1; CrmNumberProperty subLevel = new CrmNumberProperty("new_sublevel", temp); de.Properties.Add(subLevel); //Now, update the CrmService. BusinessEntity be = (BusinessEntity)de; try { service.Update(be); //This line runs 5 times in a row and then crashes on the sixth when debugging } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.InnerException.ToString()); } }




    public class OnBoardingRecord : IComparable { DynamicEntity dent; public OnBoardingRecord(DynamicEntity de) { dent = de; }
    public int CompareTo(object obj) { //Do our sorting magic }
    public DynamicEntity getDynamicEntity() { return dent; }




    I've left out some parts for the sake of brevity, namely the query to get the records, because I know for a fact that this works correctly.   Everything works correctly except the service.Update(subLevel) call, which for some reason gets called 6 times in a row during the same iteration of the for loop, and then throws an exception stating "The server was unable ot process your request."

    Any ideas on what the problem may be?  THe user has the permissions to write to the entity, and the plugin is registered with the UPdate message. 

    • Edited by Dan Landberg Monday, March 1, 2010 4:15 PM Formatting
    Monday, March 1, 2010 4:10 PM

Answers

  • Are you updating the same record on which the plugin is fired. It means the same plugin is getting called again and again unless it is detected as infinte loop by MS CRM.

    so use this at the top of your Plugin code.

    if(context.Depth>1)
        return;

    This is simply to avoid infinite loop.
    Muhammad Ali Khan
    My MS CRM blog
    Monday, March 1, 2010 4:14 PM

All replies

  • Are you updating the same record on which the plugin is fired. It means the same plugin is getting called again and again unless it is detected as infinte loop by MS CRM.

    so use this at the top of your Plugin code.

    if(context.Depth>1)
        return;

    This is simply to avoid infinite loop.
    Muhammad Ali Khan
    My MS CRM blog
    Monday, March 1, 2010 4:14 PM
  • You're absolutely right.  I didn't even think of that.  I tested it out and it works now.  Thank you
    Monday, March 1, 2010 4:47 PM