Skip to main content

TaxReturnChangeStatus

public void TaxReturnChangeStatus(EntityReference taxreturn, OptionSetValue state)
{
//Cascade tax return status to it's child records

string[] TaxReturnSchedules =
{
"tt_bankinterest",
"tt_capitalgain",
"tt_employment",
"tt_giftaid",
"tt_pensionincome",
"tt_personalpension",
"tt_propertyincome",
"tt_businesstax",
"tt_ukdividend"
};

foreach(string TaxReturnSchedule in TaxReturnSchedules)
{
tracingService.Trace($"SetState for {TaxReturnSchedule} for TaxReturn {taxreturn.Id}");

QueryExpression qe = new QueryExpression()
{
EntityName = TaxReturnSchedule
};

qe.Criteria.AddCondition("tt_taxreturnid", ConditionOperator.Equal, taxreturn.Id);

EntityCollection ScheduleRecords = crmService.RetrieveMultiple(qe);

foreach (Entity ScheduleRecord in ScheduleRecords.Entities)
{
SetStateRequest req = new SetStateRequest()
{
EntityMoniker = ScheduleRecord.ToEntityReference(),
State = state,
Status = new OptionSetValue(state.Value == 0 ? 1 : 2)
};

crmService.Execute(req);
}

tracingService.Trace($"{ScheduleRecords.Entities.Count} record(s) updated.");
}
}