web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How do I get the global option set or local option set?

(0) ShareShare
ReportReport
Posted on by

Below is my code..I want to get the option set but it is giving me the generic error that the given key is not in the dictionary. The local option set is capg_calculatorutilized and the global one is capg_calculatorrequired. I am assuming I have to get the global option set but I don't need all the metadata just the label or value I can change my code but here is my code so far:

using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;

namespace test
{
    public class DuesCalculator : IPlugin
    {
        /// <summary>
        /// This plugin is used to generate autonumber and add it to a new record during it's creation
        /// </summary>
        /// <param name="serviceProvider"></param>
        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {

                var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                tracingService.Trace("Start");
                //Get the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                //Get current entity's name
                string _currentEntityName = entity.LogicalName;

                //Create the organization service
                IOrganizationService service = factory.CreateOrganizationService(context.InitiatingUserId);
                tracingService.Trace("Organization Service Created");
                
                if(_currentEntityName == "lead")
                {
                    tracingService.Trace("_currentityName == lead");
                }



                string calculatorUtilized = entity.FormattedValues["capg_calculatorutilized"];


                tracingService.Trace("found branches");
                OrganizationServiceContext svcContext = new OrganizationServiceContext(service);

                //calculator utilized
                switch (calculatorUtilized)
                {
                    //section A 
                    case "Section A":
                        var sectionA = from c in svcContext.CreateQuery("lead")
                                       join a in svcContext.CreateQuery("product")
                                       on c["capg_primaryproduct"].ToString() equals a["name"]
                                       where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001

                                       select new
                                       {
                                           _a = c["capg_commericalhmoposlives"],
                                           _b = c["capg_medicalhmotanflives"],
                                           _c = c["capg_medicareadvantageseniorlives"],
                                           _d = c["capg_medicalwithmedicarecomponent"],
                                           dues = c["capg_calculatedduesbilling"],
                                           product = c["capg_primaryproduct"],
                                           max = a["capg_maxcriteria"],
                                           min = a["capg_mincriteria"],
                                           listprice = a["price"]
                                       };
                        foreach (var c in sectionA)
                        {
                            double people = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);

                            if (c.product.ToString().Contains("Medical Groups and IPA in CA"))
                            {
                                if ((int)c.max == 10000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("E21AB99E-8CF2-E511-80DE-C4346BAC29F8"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 10001 && (int)c.max == 43000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("9DC6FE09-9CF2-E511-80DE-C4346BAC29F8"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;

                                }
                                else if ((int)c.min == 43001 && (int)c.max == 100000)
                                {
                                    people = people * 0.029;
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("36206EC5-9CF2-E511-80DE-C4346BAC29F8"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    people = (double)price;
                                    //------------>little confusion here<---------------------
                                    due = people;

                                }
                                else if ((int)c.min == 100001 && (int)c.max == 235000)
                                {
                                    people = people * 0.0262;
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("5B9F9FED-9DF2-E511-80DE-C4346BAC29F8"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    people = (double)price;
                                    //------------>little confusion here<---------------------
                                    due = people;
                                    due = price;
                                }
                                else if ((int)c.min == 235001 && (int)c.max == 2147483647)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0782E123-9EF2-E511-80DE-C4346BAC29F8"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                            }
                        }
                        service.Update(entity);

                        break;


                    //Section B
                    case "Section B":

                        var sectionB = from c in svcContext.CreateQuery("lead")
                                       join a in svcContext.CreateQuery("product")
                                       on c["capg_primaryproduct"].ToString() equals a["name"]
                                       where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001

                                       select new
                                       {
                                           _a = c["capg_commericalhmoposlives"],
                                           _b = c["capg_medicalhmotanflives"],
                                           _c = c["capg_medicareadvantageseniorlives"],
                                           _d = c["capg_medicalwithmedicarecomponent"],
                                           dues = c["capg_calculatedduesbilling"],
                                           product = c["capg_primaryproduct"],
                                           max = a["capg_maxcriteria"],
                                           min = a["capg_mincriteria"],
                                           listprice = a["price"]
                                       };

                        foreach (var c in sectionB)
                        {
                            double dues = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);


                            if (c.product.ToString().Contains("Medical Groups Outside CA"))
                            {
                                if ((int)c.max == 25)
                                {
                                    entity["capg_primaryproduct"] = new EntityReference("product", new Guid("110F60B5-9EF2-E511-80DE-C4346BAC29F8"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 26 && (int)c.max == 125)
                                {
                                    entity["capg_primaryproduct"] = new EntityReference("product", new Guid("68962A09-A0F2-E511-80DE-C4346BAC29F8"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;

                                }
                                else if ((int)c.min == 126 && (int)c.max == 300)
                                {
                                    entity["capg_primaryproduct"] = new EntityReference("product", new Guid("C5F4E97B-A0F2-E511-80DE-C4346BAC29F8"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 301 && (int)c.max == 700)
                                {
                                    entity["capg_primaryproduct"] = new EntityReference("product", new Guid("46C424A7-A1F2-E511-80DD-C4346BACE100"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 700)
                                {
                                    entity["capg_primaryproduct"] = new EntityReference("product", new Guid("45312591-A2F2-E511-80DD-C4346BACE100"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                            }
                        }
                        service.Update(entity);

                        break;



                    //section C 
                    case "Section C":
                        var sectionC = from c in svcContext.CreateQuery("lead")
                                       join a in svcContext.CreateQuery("product")
                                       on c["capg_primaryproduct"].ToString() equals a["name"]
                                       where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001

                                       select new
                                       {
                                           _a = c["capg_commericalhmoposlives"],
                                           _b = c["capg_medicalhmotanflives"],
                                           _c = c["capg_medicareadvantageseniorlives"],
                                           _d = c["capg_medicalwithmedicarecomponent"],
                                           dues = c["capg_calculatedduesbilling"],
                                           product = c["capg_primaryproduct"],
                                           max = a["capg_maxcriteria"],
                                           min = a["capg_mincriteria"],
                                           listprice = a["price"]
                                       };

                        foreach (var c in sectionC)
                        {
                            double dues = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);

                            if (c.product.ToString().Contains("IPA outside CA"))
                            {
                                if ((int)c.max == 10000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("4F52A692-A5F2-E511-80DD-C4346BACE100"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 10001 && (int)c.max == 43000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0E893168-A3F2-E511-80DD-C4346BACE100"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;

                                }
                                else if ((int)c.min == 43001 && (int)c.max == 100000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("3CB30306-A4F2-E511-80DD-C4346BACE100"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 100001 && (int)c.max == 235000)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("2B3BDE84-A4F2-E511-80DD-C4346BACE100"));
                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                                else if ((int)c.min == 235001 && (int)c.max == 2147483647)
                                {
                                    var k = c.product;
                                    k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("85D51500-A5F2-E511-80DD-C4346BACE100"));

                                    var due = c.dues;
                                    var price = c.listprice;
                                    due = price;
                                }
                            }
                        }
                        service.Update(entity);

                        break;

                    //Section D
                    case "Section D":
                        var sectionD = from c in svcContext.CreateQuery("lead")
                                       join a in svcContext.CreateQuery("product")
                                       on c["capg_primaryproduct"].ToString() equals a["name"]
                                       where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001

                                       select new
                                       {
                                           dues = c["capg_calculatedduesbilling"],
                                           product = c["capg_primaryproduct"],
                                           listprice = a["price"]
                                       };
                        foreach (var c in sectionD)
                        {
                            var k = c.product;
                            k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("4F52A692-A5F2-E511-80DD-C4346BACE100"));


                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
                        service.Update(entity);

                        break;


                    //Advocacy 

                    case "Advocacy":
                        var advocacy = from c in svcContext.CreateQuery("lead")
                                       join a in svcContext.CreateQuery("product")
                                       on c["capg_primaryproduct"].ToString() equals a["name"]
                                       where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350000

                                       select new
                                       {
                                           dues = c["capg_calculatedduesbilling"],
                                           product = c["capg_primaryproduct"],
                                           listprice = a["price"],
                                           eligibility = c.FormattedValues["capg_eligibility"]
                                       };
                        foreach (var c in advocacy)
                        {
                            if (c.eligibility.Contains("If you do not meet the criteria provoided in 1-3, please provide an acceptable letter of support from a current CAPG member"))
                            {
                                var k = c.product;
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("5D93D147-A6F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else
                            {
                                var k = c.product;
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("6DEF4EFF-A5F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }

                        }
                        service.Update(entity);
                        break;

                    //Partner

                    case "Partner":
                        var partner = from c in svcContext.CreateQuery("lead")
                                      join a in svcContext.CreateQuery("product")
                                      on c["capg_primaryproduct"].ToString() equals a["name"]
                                      where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350000

                                      select new
                                      {
                                          _a = c.FormattedValues["capg_partnertype"],
                                          dues = c["capg_calculatedduesbilling"],
                                          product = c["capg_primaryproduct"],
                                          listprice = a["price"]

                                      };
                        foreach (var c in partner)
                        {
                            if (c._a == "Affiliate Partner")
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("998FDD8B-A6F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if (c._a == "Associate Partner")
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0F5029B6-A6F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("D50CD3F2-A6F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }

                        }
                        service.Update(entity);
                        break;


                    default:

                        break;

                }

            }




            catch (FaultException<OrganizationServiceFault>)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

 

 

Thanks!

*This post is locked for comments

I have the same question (0)
  • jraesly Profile Picture
    on at

    Here is the important part.

     

            public void Execute(IServiceProvider serviceProvider)
            {
                try
                {
    
                    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
    
                    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
                    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                    tracingService.Trace("Start");
                    //Get the target entity from the input parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
                    //Get current entity's name
                    string _currentEntityName = entity.LogicalName;
    
                    //Create the organization service
                    IOrganizationService service = factory.CreateOrganizationService(context.InitiatingUserId);
                    tracingService.Trace("Organization Service Created");
                    
                    if(_currentEntityName == "lead")
                    {
                        tracingService.Trace("_currentityName == lead");
                    }
    right here
                    string calculatorUtilized = entity.FormattedValues["capg_calculatorutilized"];
    
    
                    tracingService.Trace("found branches");
                    OrganizationServiceContext svcContext = new OrganizationServiceContext(service);
    
                    //calculator utilized
                    switch (calculatorUtilized)
  • jraesly Profile Picture
    on at

    it is supposed to run on update too

  • Suggested answer
    Community Member Profile Picture
    on at

    Use below to ensure there is no error

    if(entity.Contains("capg_calculatorutilized"))

    entity.FormattedValues["capg_calculatorutilized"];

    If there is no value selected in the option set field, it will not be present here. Or it is possible that you are not getting this field in the Target. You can also try to retrieve  columns required by you like this.

    Entity entity1 = service.Retrieve(entity.LogicalName,entity.Id,new ColumnSet("capg_calculatorutilized","othercolumns"));

  • Verified answer
    jraesly Profile Picture
    on at

    It is because it is a global optionset I solved it by using a method found from guido online 

    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System.Linq;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using System.Diagnostics;
    using System.Collections.Generic;
    
    namespace test
    {
        public class DuesCalculator : IPlugin
        {
            /// <summary>
            /// This plugin is used to calculate price
            /// </summary>
            /// <param name="serviceProvider"></param>
            /// 
    
            public void Execute(IServiceProvider serviceProvider)
            {
                #region logic
                var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                tracingService.Trace("Start");
                //Get the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                //Get current entity's name
                string _currentEntityName = entity.LogicalName;
    
                //Create the organization service
                IOrganizationService service = factory.CreateOrganizationService(context.InitiatingUserId);
                tracingService.Trace("Organization Service Created");
    
                if (_currentEntityName == "lead")
                {
                    tracingService.Trace("_currentityName == lead");
                }
    
                OrganizationServiceContext svcContext = new OrganizationServiceContext(service);
                #endregion logic
    
                #region section A 
                if (GetoptionsetText("lead", "capg_calculatorutilized", 126350000, service) == "Section A")
                {
                    var sectionA = from c in svcContext.CreateQuery("lead")
                                   join a in svcContext.CreateQuery("product")
                                   on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                   where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001
    
                                   select new
                                   {
                                       _a = c["capg_commericalhmoposlives"],
                                       _b = c["capg_medicalhmotanflives"],
                                       _c = c["capg_medicareadvantageseniorlives"],
                                       _d = c["capg_medicalwithmedicarecomponent"],
                                       dues = c["capg_calculatedduesbilling"],
                                       product = c["capg_primaryproduct"],
                                       max = a["capg_maxcriteria"],
                                       min = a["capg_mincriteria"],
                                       listprice = a["price"]
                                   };
                    foreach (var c in sectionA)
                    {
                        double people = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);
    
                        if (c.product.ToString().Contains("Medical Groups and IPA in CA"))
                        {
                            if ((int)c.max == 10000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("E21AB99E-8CF2-E511-80DE-C4346BAC29F8"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 10001 && (int)c.max == 43000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("9DC6FE09-9CF2-E511-80DE-C4346BAC29F8"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
    
                            }
                            else if ((int)c.min == 43001 && (int)c.max == 100000)
                            {
                                people = people * 0.029;
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("36206EC5-9CF2-E511-80DE-C4346BAC29F8"));
                                var due = c.dues;
                                var price = c.listprice;
                                people = (double)price;
                                //------------>little confusion here<---------------------
                                due = people;
    
                            }
                            else if ((int)c.min == 100001 && (int)c.max == 235000)
                            {
                                people = people * 0.0262;
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("5B9F9FED-9DF2-E511-80DE-C4346BAC29F8"));
                                var due = c.dues;
                                var price = c.listprice;
                                people = (double)price;
                                //------------>little confusion here<---------------------
                                due = people;
                                due = price;
                            }
                            else if ((int)c.min == 235001 && (int)c.max == 2147483647)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0782E123-9EF2-E511-80DE-C4346BAC29F8"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                        }
                    }
                    service.Update(entity);
    
                }
                #endregion
    
                #region Section B
                else if (GetoptionsetText("lead", "capg_calculatorutilized", 126350001, service) == "Section B")
                {
                    var sectionB = from c in svcContext.CreateQuery("lead")
                                   join a in svcContext.CreateQuery("product")
                                   on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                   where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001
    
                                   select new
                                   {
                                       _a = c["capg_commericalhmoposlives"],
                                       _b = c["capg_medicalhmotanflives"],
                                       _c = c["capg_medicareadvantageseniorlives"],
                                       _d = c["capg_medicalwithmedicarecomponent"],
                                       dues = c["capg_calculatedduesbilling"],
                                       product = c["capg_primaryproduct"],
                                       max = a["capg_maxcriteria"],
                                       min = a["capg_mincriteria"],
                                       listprice = a["price"]
                                   };
    
                    foreach (var c in sectionB)
                    {
                        double dues = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);
    
    
                        if (c.product.ToString().Contains("Medical Groups Outside CA"))
                        {
                            if ((int)c.max == 25)
                            {
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("110F60B5-9EF2-E511-80DE-C4346BAC29F8"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 26 && (int)c.max == 125)
                            {
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("68962A09-A0F2-E511-80DE-C4346BAC29F8"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
    
                            }
                            else if ((int)c.min == 126 && (int)c.max == 300)
                            {
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("C5F4E97B-A0F2-E511-80DE-C4346BAC29F8"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 301 && (int)c.max == 700)
                            {
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("46C424A7-A1F2-E511-80DD-C4346BACE100"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 700)
                            {
                                entity["capg_primaryproduct"] = new EntityReference("product", new Guid("45312591-A2F2-E511-80DD-C4346BACE100"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                        }
                    }
                    service.Update(entity);
    
                }
                #endregion
    
                #region section C 
                else if (GetoptionsetText("lead", "capg_calculatorutilized", 126350002, service) == "Section C")
                {
                    var sectionC = from c in svcContext.CreateQuery("lead")
                                   join a in svcContext.CreateQuery("product")
                                   on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                   where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001
    
                                   select new
                                   {
                                       _a = c["capg_commericalhmoposlives"],
                                       _b = c["capg_medicalhmotanflives"],
                                       _c = c["capg_medicareadvantageseniorlives"],
                                       _d = c["capg_medicalwithmedicarecomponent"],
                                       dues = c["capg_calculatedduesbilling"],
                                       product = c["capg_primaryproduct"],
                                       max = a["capg_maxcriteria"],
                                       min = a["capg_mincriteria"],
                                       listprice = a["price"]
                                   };
    
                    foreach (var c in sectionC)
                    {
                        double dues = (Convert.ToInt32(c._b) * 0.5) + (Convert.ToInt32(c._d) * 3) + Convert.ToInt32(c._a) + (Convert.ToInt32(c._c) * 3);
    
                        if (c.product.ToString().Contains("IPA outside CA"))
                        {
                            if ((int)c.max == 10000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("4F52A692-A5F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 10001 && (int)c.max == 43000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0E893168-A3F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
    
                            }
                            else if ((int)c.min == 43001 && (int)c.max == 100000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("3CB30306-A4F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 100001 && (int)c.max == 235000)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("2B3BDE84-A4F2-E511-80DD-C4346BACE100"));
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                            else if ((int)c.min == 235001 && (int)c.max == 2147483647)
                            {
                                var k = c.product;
                                k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("85D51500-A5F2-E511-80DD-C4346BACE100"));
    
                                var due = c.dues;
                                var price = c.listprice;
                                due = price;
                            }
                        }
                    }
                    service.Update(entity);
                }
                #endregion
    
                #region Section D
                else if (GetoptionsetText("lead", "capg_calculatorutilized", 126350003, service) == "Section D")
                {
                    var sectionD = from c in svcContext.CreateQuery("lead")
                                   join a in svcContext.CreateQuery("product")
                                   on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                   where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350001
    
                                   select new
                                   {
                                       dues = c["capg_calculatedduesbilling"],
                                       product = c["capg_primaryproduct"],
                                       listprice = a["price"]
                                   };
                    foreach (var c in sectionD)
                    {
                        var k = c.product;
                        k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("4F52A692-A5F2-E511-80DD-C4346BACE100"));
    
    
                        var due = c.dues;
                        var price = c.listprice;
                        due = price;
                    }
                    service.Update(entity);
                }
                #endregion
    
                #region Advocacy
    
                else if (GetoptionsetText("lead", "capg_calculatorutilized", 126350004, service) == "Advocacy")
                {
                    var advocacy = from c in svcContext.CreateQuery("lead")
                                   join a in svcContext.CreateQuery("product")
                                   on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                   where (int)(a["capg_billingtimeframe"]) == 126350000
    
                                   select new
                                   {
                                       dues = c["capg_calculatedduesbilling"],
                                       product = c["capg_primaryproduct"],
                                       listprice = a["price"],
                                       eligibility = c.FormattedValues["capg_eligibility"]
                                   };
                    foreach (var c in advocacy)
                    {
                        if (c.eligibility.Contains("If you do not meet the criteria provoided in 1-3, please provide an acceptable letter of support from a current CAPG member"))
                        {
                            var k = c.product;
                            k = new EntityReference("product", new Guid("5D93D147-A6F2-E511-80DD-C4346BACE100"));
                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
                        else
                        {
                            var k = c.product;
                            entity["capg_primaryproduct"] = new EntityReference("product", new Guid("6DEF4EFF-A5F2-E511-80DD-C4346BACE100"));
                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
    
                    }
                    service.Update(entity);
                }
                #endregion
    
                #region Partner
    
                else if (GetoptionsetText("lead", "capg_calculatorutilized", 126350005, service) == "Partner")
                {
                    var partner = from c in svcContext.CreateQuery("lead")
                                  join a in svcContext.CreateQuery("product")
                                  on c["transactioncurrencyid"] equals a["transactioncurrencyid"]
                                  where Convert.ToInt32(a["capg_billingtimeframe"]) == 126350000
    
                                  select new
                                  {
                                      _a = c.FormattedValues["capg_partnertype"],
                                      dues = c["capg_calculatedduesbilling"],
                                      product = c["capg_primaryproduct"],
                                      listprice = a["price"]
    
                                  };
                    foreach (var c in partner)
                    {
                        if (c._a == "Affiliate Partner")
                        {
                            var k = c.product;
                            k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("998FDD8B-A6F2-E511-80DD-C4346BACE100"));
                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
                        else if (c._a == "Associate Partner")
                        {
                            var k = c.product;
                            k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("0F5029B6-A6F2-E511-80DD-C4346BACE100"));
                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
                        else
                        {
                            var k = c.product;
                            k = entity["capg_primaryproduct"] = new EntityReference("product", new Guid("D50CD3F2-A6F2-E511-80DD-C4346BACE100"));
                            var due = c.dues;
                            var price = c.listprice;
                            due = price;
                        }
    
                    }
                    service.Update(entity);
                }
                #endregion
            }
            // Retrieves Global Option set Selected Text
            // Parameters: 1. Entity Name  2. Global Option Set Name 3. optionset selected value  4. service
            public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
            {
                string AttributeName = attributeName;
                string EntityLogicalName = entityName;
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = EntityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
                EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
                PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
                OptionSetMetadata options = picklistMetadata.OptionSet;
                IList<OptionMetadata> OptionsList = (from o in options.Options
                                                     where o.Value.Value == optionSetValue
                                                     select o).ToList();
                string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
                return optionsetLabel;
            }
        }
    }
    
    
    
    

    The only thing now is that I had to get rid of my switch statements

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans