private void RestoreCheckedValues() { foreach (GridViewRow row in NewOpportunitiesList.Rows) { var dataKey = NewOpportunitiesList.DataKeys[row.RowIndex].Value.ToString(); var cellCount = row.Cells.Count; if (_acceptChecked.Contains(dataKey)) { var acceptCell = row.Cells[cellCount - 2]; var accept = acceptCell.FindControl(dataKey + "_accept") as CheckBox; accept.Checked = true; } if (_declineChecked.Contains(dataKey)) { var declineCell = row.Cells[cellCount - 1]; var decline = declineCell.FindControl(dataKey + "_decline") as CheckBox; decline.Checked = true; } if (SavedOpportunities.Contains(dataKey)) { row.CssClass = "saved" + (row.RowIndex % 2 == 1 ? " alternate-row" : string.Empty); } } }
protected void SaveButton_Click(object sender, EventArgs args) { foreach (GridViewRow row in NewOpportunitiesList.Rows) { var dataKey = new Guid(NewOpportunitiesList.DataKeys[row.RowIndex].Value.ToString()); var cellCount = row.Cells.Count; var acceptCell = row.Cells[cellCount - 2]; var accept = acceptCell.FindControl(dataKey + "_accept") as CheckBox; var declineCell = row.Cells[cellCount - 1]; var decline = declineCell.FindControl(dataKey + "_decline") as CheckBox; var opportunity = XrmContext.CreateQuery("opportunity").FirstOrDefault(o => o.GetAttributeValue <Guid>("opportunityid") == dataKey); if (SavedOpportunities.Contains(dataKey.ToString()) || (!accept.Checked && !decline.Checked) || (accept.Checked && decline.Checked)) { continue; } var partnerReference = opportunity.GetAttributeValue <EntityReference>("msa_partnerid"); if (partnerReference == null) { continue; } var partner = XrmContext.CreateQuery("account").First(a => a.GetAttributeValue <Guid>("accountid") == partnerReference.Id); var oppPermissions = XrmContext.GetOpportunityAccessByContactForParentAccount(Contact).Where(oa => oa.GetAttributeValue <bool?>("adx_acceptdecline").GetValueOrDefault(false)); if (partner.GetAttributeValue <int?>("adx_numberofopportunitiesdelivered").GetValueOrDefault(0) == 0) { partner.SetAttributeValue("adx_numberofopportunitiesdelivered", 1); } var touchrate = (double)(partner.GetAttributeValue <int?>("adx_numberofopportunitiesaccepted").GetValueOrDefault(0) + (partner.GetAttributeValue <int?>("adx_numberofopportunitiesdeclined").GetValueOrDefault(0))) / (partner.GetAttributeValue <int?>("adx_numberofopportunitiesdelivered").GetValueOrDefault(1)); partner.SetAttributeValue("adx_touchrate", touchrate); if (accept.Checked) { if (oppPermissions.ToList().Any()) { //we mark this opportunity as accepted opportunity.SetAttributeValue("statuscode", new OptionSetValue((int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Accepted)); opportunity.SetAttributeValue("stepname", OpportunityHistory.PipelinePhaseAccepted); opportunity.SetAttributeValue("adx_accepteddate", DateTime.UtcNow); partner.SetAttributeValue("adx_numberofopportunitiesaccepted", partner.GetAttributeValue <int?>("adx_numberofopportunitiesaccepted").GetValueOrDefault(0) + 1); } } else if (decline.Checked) { if (oppPermissions.ToList().Any()) { //we mark this opportunity as declined opportunity.SetAttributeValue("statuscode", new OptionSetValue((int)Adxstudio.Xrm.Partner.Enums.OpportunityStatusReason.Declined)); opportunity.SetAttributeValue("stepname", OpportunityHistory.PipelinePhaseDeclined); partner.SetAttributeValue("adx_numberofopportunitiesdeclined", partner.GetAttributeValue <int?>("adx_numberofopportunitiesdeclined").GetValueOrDefault(0) + 1); partner.SetAttributeValue("adx_activeopportunitycount", partner.GetAttributeValue <int?>("adx_activeopportunitycount").GetValueOrDefault(0) - 1); XrmContext.AddLink(opportunity, new Relationship("adx_account_declinedopportunity"), partner); } } opportunity.SetAttributeValue("adx_expirydate", null); XrmContext.UpdateObject(opportunity); XrmContext.UpdateObject(partner); XrmContext.SaveChanges(); SavedOpportunities += dataKey + ","; } RestoreCheckedValues(); //Response.Redirect(Request.RawUrl); }