CRM 2013

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

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

CRM Online Partner Instance

Posted on

Hi,

If you are Microsoft CRM Partner and looking for a creating a demo instance that will be valid for more than a month please follow the link below –

https://profile.microsoft.com/RegSysProfileCenter/wizardnp.aspx?wizid=4aa23057-22aa-465d-9064-556dbebce590

keep in mind that you can request up to 5 instances and instance will remain active for upto 12 months

Trigger plugin for N:N relationships in Dynamics CRM

Image Posted on

Yesterday I was working on a requirement to trigger a plugin for N:N relationship. The idea is as soon as a record on N:N subgrid is added, trigger the plugin. Follow steps below for this –

1. Create a PreUpdate plugin on the entity

2. Check Message Name in from the PluginExecutionContext. Use Associate if checking for addition of a record in N:N or Disassociate for deletion of record in N:N

3. Validate Context depth is 1, this is to avoid recursive / infinite loops

4. Use “Relationship” input parameter to get current entity

5. Use Relation Entity schema name to get related entity record. This can be found by opening the respective N:N relationship.

6. Ensure the Plugin is registered with following values –

3

 

 

Code snippet below –

1

Hope it helps..