Get OptionSet Text from Value and Get Optionset Value from Text in MS CRM using C#
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AkithaCaseQuoteOptionset
{
public class CreateQuoteFromCase:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(new Guid?(context.UserId));
if (context.Depth > 1)
{
return;
}
string caseDepartment = null;
int optSetVale;
if (context.PrimaryEntityName.ToLower() == "quote")
{
Entity quoteEnt = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("new_quoteoptions","new_linkedtojoblookup"));
if (quoteEnt != null)
{
Guid caselookup = quoteEnt.Contains("new_linkedtojoblookup ") ? quoteEnt.GetAttributeValue<EntityReference>("new_linkedtojoblookup ").Id : Guid.Empty;
if (caselookup != Guid.Empty)
{
Entity caseRecord = service.Retrieve("incident", caselookup, new ColumnSet("new_department"));
if (caseRecord != null)
{
int optionValue = caseRecord.Contains("new_department") ? caseRecord.GetAttributeValue<OptionSetValue>("new_department").Value : -1;
caseDepartment = GetOptionsSetTextForValue("quote", "new_department", optionValue, service);
optSetVale = getOptionSetValueforText("quote", "new_department", caseDepartment, service);
Entity quote = new Entity("quote");
quote.Id = quoteEnt.Id;
if (optionValue != -1)
{
if (optionValue == 100000001 || optionValue == 100000002 || optionValue == 100000003 || optionValue == 121450000 || optionValue == 121450001)
{
quote.Attributes["new_quoteoptions"] = new OptionSetValue(100000001);
}
else if (optionValue == 100000000)
{
quote.Attributes["new_quoteoptions"] = new OptionSetValue(121450000);
}
service.Update(quote);
}
}
}
}
}
if (context.PrimaryEntityName.ToLower() == "incident")
{
Entity caseEnt = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("new_department", "new_createdfromquote"));
if (caseEnt != null)
{
Guid quoteLookup = caseEnt.Contains("new_createdfromquote") ? caseEnt.GetAttributeValue<EntityReference>("new_createdfromquote").Id : Guid.Empty;
if (quoteLookup != Guid.Empty)
{
Entity quoteRecord = service.Retrieve("quote", quoteLookup, new ColumnSet("new_quoteoptions"));
if (quoteRecord != null)
{
int optionValues = quoteRecord.Contains("new_quoteoptions") ? quoteRecord.GetAttributeValue<OptionSetValue>("new_quoteoptions").Value : -1;
Entity ent = new Entity("incident");
ent.Id = caseEnt.Id;
if (optionValues != -1)
{
if (optionValues == 121450000)
{
ent.Attributes["new_department"] = new OptionSetValue(100000000);
}
else if (optionValues == 100000001)
{
ent.Attributes["new_department"] = new OptionSetValue(100000001);
}
else
{
ent.Attributes["new_department"] = null;
}
service.Update(ent);
}
}
}
}
}
}
private string GetOptionsSetTextForValue(string entityName, string attributeName, int optionValue, IOrganizationService service)
{
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
// Access the retrieved attribute.
Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
string selectedOptionLabel = null;
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == optionValue)
{
selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
break;
}
}
return selectedOptionLabel;
}
private int getOptionSetValueforText(string entityName, string attributeName, string optionsetText, IOrganizationService service)
{
int optionSetValue = 0;
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
retrieveAttributeRequest.EntityLogicalName = entityName;
retrieveAttributeRequest.LogicalName = attributeName;
retrieveAttributeRequest.RetrieveAsIfPublished = true;
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
PicklistAttributeMetadata picklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;
foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
{
if (optionMetadata.Label.UserLocalizedLabel.Label.ToLower() == optionsetText.ToLower())
{
optionSetValue = optionMetadata.Value.Value;
return optionSetValue;
}
}
return optionSetValue;
}
}
}
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AkithaCaseQuoteOptionset
{
public class CreateQuoteFromCase:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(new Guid?(context.UserId));
if (context.Depth > 1)
{
return;
}
string caseDepartment = null;
int optSetVale;
if (context.PrimaryEntityName.ToLower() == "quote")
{
Entity quoteEnt = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("new_quoteoptions","new_linkedtojoblookup"));
if (quoteEnt != null)
{
Guid caselookup = quoteEnt.Contains("new_linkedtojoblookup ") ? quoteEnt.GetAttributeValue<EntityReference>("new_linkedtojoblookup ").Id : Guid.Empty;
if (caselookup != Guid.Empty)
{
Entity caseRecord = service.Retrieve("incident", caselookup, new ColumnSet("new_department"));
if (caseRecord != null)
{
int optionValue = caseRecord.Contains("new_department") ? caseRecord.GetAttributeValue<OptionSetValue>("new_department").Value : -1;
caseDepartment = GetOptionsSetTextForValue("quote", "new_department", optionValue, service);
optSetVale = getOptionSetValueforText("quote", "new_department", caseDepartment, service);
Entity quote = new Entity("quote");
quote.Id = quoteEnt.Id;
if (optionValue != -1)
{
if (optionValue == 100000001 || optionValue == 100000002 || optionValue == 100000003 || optionValue == 121450000 || optionValue == 121450001)
{
quote.Attributes["new_quoteoptions"] = new OptionSetValue(100000001);
}
else if (optionValue == 100000000)
{
quote.Attributes["new_quoteoptions"] = new OptionSetValue(121450000);
}
service.Update(quote);
}
}
}
}
}
if (context.PrimaryEntityName.ToLower() == "incident")
{
Entity caseEnt = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("new_department", "new_createdfromquote"));
if (caseEnt != null)
{
Guid quoteLookup = caseEnt.Contains("new_createdfromquote") ? caseEnt.GetAttributeValue<EntityReference>("new_createdfromquote").Id : Guid.Empty;
if (quoteLookup != Guid.Empty)
{
Entity quoteRecord = service.Retrieve("quote", quoteLookup, new ColumnSet("new_quoteoptions"));
if (quoteRecord != null)
{
int optionValues = quoteRecord.Contains("new_quoteoptions") ? quoteRecord.GetAttributeValue<OptionSetValue>("new_quoteoptions").Value : -1;
Entity ent = new Entity("incident");
ent.Id = caseEnt.Id;
if (optionValues != -1)
{
if (optionValues == 121450000)
{
ent.Attributes["new_department"] = new OptionSetValue(100000000);
}
else if (optionValues == 100000001)
{
ent.Attributes["new_department"] = new OptionSetValue(100000001);
}
else
{
ent.Attributes["new_department"] = null;
}
service.Update(ent);
}
}
}
}
}
}
private string GetOptionsSetTextForValue(string entityName, string attributeName, int optionValue, IOrganizationService service)
{
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
// Access the retrieved attribute.
Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
string selectedOptionLabel = null;
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == optionValue)
{
selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
break;
}
}
return selectedOptionLabel;
}
private int getOptionSetValueforText(string entityName, string attributeName, string optionsetText, IOrganizationService service)
{
int optionSetValue = 0;
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
retrieveAttributeRequest.EntityLogicalName = entityName;
retrieveAttributeRequest.LogicalName = attributeName;
retrieveAttributeRequest.RetrieveAsIfPublished = true;
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
PicklistAttributeMetadata picklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;
foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
{
if (optionMetadata.Label.UserLocalizedLabel.Label.ToLower() == optionsetText.ToLower())
{
optionSetValue = optionMetadata.Value.Value;
return optionSetValue;
}
}
return optionSetValue;
}
}
}
Comments
Post a Comment