locked
CRM 4.0 Plugin causing Workflow Loop Detected Infinite Loop Error RRS feed

  • Question

  • Hi there,

    I have a plugin that gets triggered on Post Staging on Create and Update based on some rules I try to update the same entity but by doing so I get an error code 0X80040265. The workflow job was cancelled because the workflow started it included in infinite loop. I dont have any workflow. Please help

    Monday, May 23, 2011 4:25 PM

All replies

  • I think the error message is confusing in this case, but it sounds like your plugin is causing an infinite loop (by your update in the plugin causing the update to fire again, etc.), and CRM has detected this condition.

    You should be able to resolve this by:

    1. Ensuring that, when you pass an entity to the Update method, you only include the primary key, and the fields that you are modifying
    2. Either specify filtering attributes in the plugin step registration to only pick up changes to attributes that would cause your update to happen, or write logic in the plugin to check which attributes have changed

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk
    Monday, May 23, 2011 7:09 PM
    Moderator
  • Here is the method

     

     

    private void AfterUpdateRegistration(IPluginExecutionContext context)

    {

     

    var crmContext = new XrmDataContext("CacheDisabled", context, true);

     

    Guid? ccfRegistrationId = null;

     

     

    if (CTCDEPropHelper.PropertyExists(this.InputTargetDE, "ccf_registrationid"))

    {

    ccfRegistrationId =

    CTCDEPropHelper.GetKeyValue(this.InputTargetDE, "ccf_registrationid");

     

    var ccfRegistrationEntity = crmContext.ccf_registrations.Where(o => o.ccf_registrationid == ccfRegistrationId).SingleOrDefault();

     

     

    if (ccfRegistrationEntity != null)

    {

     

    //Check for Member Documentation for the Registration Year. 05/23/2011
     

     

    var ccf_memberId = ccfRegistrationEntity.ccf_memberid;

     

    int ccf_registrationYear = (int)ccfRegistrationEntity.ccf_registrationyear;

     

     

    var DocumentEntity = crmContext.ccf_memberdocumentations.Where(o => o.ccf_memberid == ccf_memberId && o.ccf_registrationyear == ccf_registrationYear).FirstOrDefault();

     

    if (DocumentEntity == null)

    {

    ccfRegistrationEntity.ccf_registrationstatus = (

    int)RegistrationStatus.Violation; //Violation
     

    ccfRegistrationEntity.ccf_violationreason = (

    int)RegistrationViolationReasons.MemberDocumentation; // Program Dropped
     

    crmContext.UpdateObject(ccfRegistrationEntity);

    crmContext.SaveChanges();

     

    throw new InvalidPluginExecutionException("Member missing documentation for the selected registration year.");

    }

     

     

     

    //#region Voucher
     

     

    if (ccfRegistrationEntity.ccf_programsessionid == null)

    {

     

    var ccfProgramId = ccfRegistrationEntity.ccf_programid;

     

    var ccfMemberid = ccfRegistrationEntity.ccf_memberid;

     

    bool ccfDocumented = (bool)ccfRegistrationEntity.ccf_documented;

     

    int ccfDocumentedPL = 0;

     

    if (ccfDocumented)

    ccfDocumentedPL = 1;

     

    else
     

    ccfDocumentedPL = 2;

     

    var registrationYear = ccfRegistrationEntity.ccf_registrationyear;

     

    var ccfMemberEntity = crmContext.contacts.Where(o => o.contactid == ccfMemberid).SingleOrDefault();

     

    if (ccfMemberEntity != null)

    {

     

    var annualSalary = ccfMemberEntity.ccf_annualsalary;

     

    var numberOfAllowances = ccfMemberEntity.ccf_dependents;

     

    var voucherEntity = crmContext.ccf_vouchers.Where(o => o.ccf_programid == ccfProgramId

    && o.ccf_registrationyear == registrationYear

    && (o.ccf_numberofchildren == numberOfAllowances || o.ccf_numberofchildren == 0)

    && ((o.ccf_documented == ccfDocumentedPL) || (o.ccf_documented == 3))

    && o.ccf_minimumsalary <= annualSalary

    && o.ccf_maximumsalary >= annualSalary).FirstOrDefault();

     

    if (voucherEntity != null)

    {

     

    var paymentPeriod = voucherEntity.ccf_paymentperiod;

     

    var paymentAmount = voucherEntity.ccf_voucheramount;

    ccfRegistrationEntity.ccf_projectedcost = paymentAmount * paymentPeriod;

    ccfRegistrationEntity.ccf_actualcost = paymentAmount * paymentPeriod;

    ccfRegistrationEntity.ccf_weeks = ConvertStringToNullable(voucherEntity.ccf_paymentperiodLabel);

    crmContext.UpdateObject(ccfRegistrationEntity);

    crmContext.SaveChanges();

    }

     

    else
     

    {

     

    var highestChildrenNoVoucherEntity = crmContext.ccf_vouchers.Where(o => o.ccf_programid == ccfProgramId

    && o.ccf_registrationyear == registrationYear

    && ((o.ccf_documented == ccfDocumentedPL) || (o.ccf_documented == 3))

    && o.ccf_minimumsalary <= annualSalary

    && o.ccf_maximumsalary >= annualSalary).OrderByDescending(o => o.ccf_numberofchildren).Take(1).FirstOrDefault();

     

     

     

    if (highestChildrenNoVoucherEntity != null)

    {

     

    var paymentPeriod = highestChildrenNoVoucherEntity.ccf_paymentperiod;

     

    var paymentAmount = highestChildrenNoVoucherEntity.ccf_voucheramount;

    ccfRegistrationEntity.ccf_projectedcost = paymentAmount * paymentPeriod;

    ccfRegistrationEntity.ccf_actualcost = paymentAmount * paymentPeriod;

    ccfRegistrationEntity.ccf_weeks = ConvertStringToNullable(highestChildrenNoVoucherEntity.ccf_paymentperiodLabel);

    crmContext.UpdateObject(ccfRegistrationEntity);

    crmContext.SaveChanges();

    }

     

    }

    }

    }

     

    //#endregion
     

    }

    }

    }

    Monday, May 23, 2011 8:22 PM