Example #1
0
        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);
        }
Example #2
0
        protected void OnItemInserted(object sender, CrmEntityFormViewInsertedEventArgs e)
        {
            var contact = XrmContext.CreateQuery("contact").FirstOrDefault(c => c.GetAttributeValue <Guid>("contactid") == e.EntityId);

            if (contact == null)
            {
                throw new Exception(string.Format("A contact couldn't be found with a contact ID equal to {0}.", e.EntityId));
            }

            var parentCustomerAccount = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : ServiceContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);

            if (parentCustomerAccount == null)
            {
                throw new Exception("Parent Customer Account could not be found associated to the current user's contact.");
            }

            Guid accountId = (Guid.TryParse(CompanyNameList.SelectedValue, out accountId)) ? accountId : Guid.Empty;

            var account = XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == accountId);

            var opportunity = OriginatingOpportunity == null ? null : XrmContext.CreateQuery("opportunity").FirstOrDefault(o => o.GetAttributeValue <Guid>("opportunityid") == OriginatingOpportunity.Id);

            if (opportunity != null)
            {
                var contactCrossover = opportunity.GetRelatedEntities(XrmContext, new Relationship("adx_opportunity_contact")).FirstOrDefault(c => c.GetAttributeValue <Guid>("contactid") == contact.GetAttributeValue <Guid>("contactid"));

                var oppnote = new Entity("adx_opportunitynote");

                if (contactCrossover == null)
                {
                    XrmContext.AddLink(opportunity, new Relationship("adx_opportunity_contact"), contact);

                    oppnote.SetAttributeValue("adx_name", "Contact Added: " + contact.GetAttributeValue <string>("fullname"));
                    oppnote.SetAttributeValue("adx_date", DateTime.UtcNow);
                    oppnote.SetAttributeValue("adx_description", "Contact Added: " + contact.GetAttributeValue <string>("fullname"));
                    oppnote.SetAttributeValue("adx_opportunityid", opportunity.ToEntityReference());
                    var assignedToContact = opportunity.GetRelatedEntity(XrmContext, new Relationship("msa_contact_opportunity"));
                    oppnote.SetAttributeValue("adx_assignedto", assignedToContact != null ? assignedToContact.GetAttributeValue <string>("fullname") : string.Empty);
                    XrmContext.AddObject(oppnote);
                    XrmContext.UpdateObject(opportunity);
                }
            }

            contact.SetAttributeValue("msa_managingpartnerid", parentCustomerAccount.ToEntityReference());

            if (account != null)
            {
                contact.SetAttributeValue("parentcustomerid", account.ToEntityReference());

                XrmContext.UpdateObject(account);
            }

            XrmContext.UpdateObject(contact);

            if (SetAsPrimary)
            {
                if (account == null)
                {
                    throw new Exception(string.Format("An account with the account ID equal to {0} couldn’t be found.", accountId));
                }

                account.SetAttributeValue("primarycontactid", contact.ToEntityReference());

                XrmContext.UpdateObject(account);
            }

            XrmContext.SaveChanges();

            if (opportunity != null)
            {
                var url = GetUrlForRequiredSiteMarker("Opportunity Details");

                url.QueryString.Set("OpportunityID", opportunity.GetAttributeValue <Guid>("opportunityid").ToString());

                Response.Redirect(url.PathWithQueryString);
            }
            else if (ReturnToAccount)
            {
                if (account == null)
                {
                    throw new Exception(string.Format("An account with the account ID equal to {0} couldn’t be found.", accountId));
                }

                var url = GetUrlForRequiredSiteMarker("Edit Customer Account");

                url.QueryString.Set("AccountID", account.Id.ToString());

                Response.Redirect(url.PathWithQueryString);
            }
            else if (FromCreateOpportunity)
            {
                if (account == null)
                {
                    throw new Exception(string.Format("An account with the account ID equal to {0} couldn’t be found.", accountId));
                }

                var url = GetUrlForRequiredSiteMarker("Create Opportunity");

                url.QueryString.Set("AccountId", account.Id.ToString());

                Response.Redirect(url.PathWithQueryString);
            }
            else
            {
                var url = GetUrlForRequiredSiteMarker("Manage Customer Contacts");

                Response.Redirect(url.PathWithQueryString);
            }
        }
        protected void OnItemInserted(object sender, CrmEntityFormViewInsertedEventArgs e)
        {
            var opportunity = XrmContext.CreateQuery("opportunity").First(o => o.GetAttributeValue <Guid>("opportunityid") == e.EntityId);

            var opportunityProductsFromLead = opportunity.GetAttributeValue <string>("adx_opportunityproductsfromlead");

            var productsList = new List <Entity>();

            if (!string.IsNullOrEmpty(opportunityProductsFromLead))
            {
                var products = XrmContext.CreateQuery("product");

                var words = opportunityProductsFromLead.Split(',');

                foreach (var word in words)
                {
                    foreach (var product in products)
                    {
                        if (product.GetAttributeValue <string>("name").Trim().ToUpper() == word.Trim().ToUpper())
                        {
                            productsList.Add(product);
                        }
                    }
                }
            }

            foreach (var leadProduct in productsList)
            {
                if (!XrmContext.IsAttached(leadProduct))
                {
                    XrmContext.Attach(leadProduct);
                }

                XrmContext.AddLink(opportunity, new Relationship("adx_opportunity_product"), leadProduct);
            }

            opportunity.SetAttributeValue("adx_referencecode", GetOpportunityReferenceCode());

            var salesStage = opportunity.GetAttributeValue <OptionSetValue>("salesstagecode") == null ? 0 : opportunity.GetAttributeValue <OptionSetValue>("salesstagecode").Value;

            var response = (RetrieveAttributeResponse)ServiceContext.Execute(new RetrieveAttributeRequest
            {
                EntityLogicalName = "opportunity",
                LogicalName       = "salesstagecode"
            });

            var picklist = response.AttributeMetadata as PicklistAttributeMetadata;

            if (picklist == null)
            {
                return;
            }

            foreach (var option in picklist.OptionSet.Options)
            {
                if (option != null && option.Value != null && option.Value.Value == salesStage)
                {
                    opportunity.SetAttributeValue("stepname", option.Label.GetLocalizedLabelString());
                }
            }

            var leadType = XrmContext.CreateQuery("adx_leadtype").FirstOrDefault(lt => lt.GetAttributeValue <string>("adx_name") == "Partner Created");

            if (leadType != null)
            {
                opportunity.SetAttributeValue("adx_leadtypeid", leadType.ToEntityReference());
            }

            XrmContext.UpdateObject(opportunity);

            XrmContext.SaveChanges();

            var url = GetUrlForRequiredSiteMarker("Accepted Opportunities");

            Response.Redirect(url);
        }