How to check if two records are related N:N

Posted on

QueryExpression query = new QueryExpression(relationshipName)
     {
         NoLock = true,
         ColumnSet = new ColumnSet(false),
         Criteria =
         {
             Filters =
             {
                 new FilterExpression
                 {
                     FilterOperator = LogicalOperator.And,
                     Conditions =
                     {
                         new ConditionExpression("accountid", ConditionOperator.Equal, accountID.ToString()),
                         new ConditionExpression("contactid", ConditionOperator.Equal, contactId.ToString()),
                     },
                 },
             }
         }
     };
     var result = service.RetrieveMultiple(query);
There is a catch here, if you have created a self referential N:N relationship then the primary fields are “<fieldname>one” and “<fieldname>two”, so incidentidone and incidentidtwo
Advertisements

Dynamics CRM 2016 Preview!!!

Posted on

Microsoft made an announcement around release of Dynamics CRM 2016, have a look at the video for some exciting features that will be introduced in CRM 2016 –

Microsoft_Dynamics_CRM_2016_Release_Preview_Guide

Get Status Value for Text in CRM

Posted on

public static int? GetStatusValueForText(IOrganizationService service, string entityName, string attributeName, string selectedText)
{
var retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
int? statusCodeValue = null;
try
{
RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
AttributeMetadata attrMetadata = attributeResponse.AttributeMetadata;

StatusAttributeMetadata statusAttrMetadata = (StatusAttributeMetadata)attrMetadata;

foreach (StatusOptionMetadata statusMeta in statusAttrMetadata.OptionSet.Options)
{
if (statusMeta.Label.UserLocalizedLabel.Label == selectedText)
{
statusCodeValue = statusMeta.Value;
}
}
}
catch (Exception ex)
{
var retrieveAttributeResponse =
(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
var retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Label.LocalizedLabels[0].Label == selectedText)
{
statusCodeValue = oMD.Value;
break;
}
}
}
return statusCodeValue;
}

Get OptionSet Value from Text in CRM

Posted on Updated on

public static int? GetOptionSetValueFromText(string optionSetText, OrganizationServiceContext orgContext,
string optionSetLogicalName, string entityLogicalName)
{
try
{
var request = new RetrieveAttributeRequest
{
EntityLogicalName = entityLogicalName,
LogicalName = optionSetLogicalName,
RetrieveAsIfPublished = true
};

var response = (RetrieveAttributeResponse)orgContext.Execute(request);

var optionsetMetadata = response.AttributeMetadata as PicklistAttributeMetadata;
var statusMetadata = response.AttributeMetadata as StatusAttributeMetadata;

if (optionsetMetadata != null) // optionset
{
var picklist = (PicklistAttributeMetadata)response.AttributeMetadata;

var query = from option in picklist.OptionSet.Options
where option.Label.UserLocalizedLabel.Label == optionSetText
select option.Value;
var returnValue = query.FirstOrDefault();

if (returnValue != null) return (int)returnValue;
}

if (statusMetadata != null) // status and state
{
var picklist = (StatusAttributeMetadata)response.AttributeMetadata;

var query = from option in picklist.OptionSet.Options
where option.Label.UserLocalizedLabel.Label == optionSetText
select option.Value;
var returnValue = query.FirstOrDefault();

if (returnValue != null) return (int)returnValue;
}

return null;
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(“Exception in GetOptionSetValueFromText function —” + ex.Message);
}
}

Get Status Text for a Value in CRM

Posted on

public static string GetStatusTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
{
var retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
string statusCodeLabel = “”;
try
{

RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
AttributeMetadata attrMetadata = attributeResponse.AttributeMetadata;

StatusAttributeMetadata statusAttrMetadata = (StatusAttributeMetadata)attrMetadata;

foreach (StatusOptionMetadata statusMeta in statusAttrMetadata.OptionSet.Options)
{
if (statusMeta.Value == selectedValue)
{
statusCodeLabel = statusMeta.Label.UserLocalizedLabel.Label;
break;
}
}
}
catch (Exception ex)
{
var retrieveAttributeResponse =
(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
var retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == selectedValue)
{
statusCodeLabel = oMD.Label.LocalizedLabels[0].Label;
break;
}
}
}
return statusCodeLabel;
}

Get OptionSetText for a Value in CRM

Posted on

public static string GetOptionsSetTextForValue(IOrganizationService service, string entityName,string attributeName,int selectedValue)
{
var retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};

var retrieveAttributeResponse =
(RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
var retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
string selectedOptionLabel = null;
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == selectedValue)
{
selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label;
break;
}
}
return selectedOptionLabel;
}