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;
}
}
}