Most Used Plugin Syntax in Dynamics 365 CRM

We always get requirement which can not be possible using OOB features of CRM and hence we move to the plugin to achieve such requirements.

Whenever we need to retrieve field value from plugin, we always check whether field contains value or not by using if else block or ternary operator which makes code lengthy and not readable.

In this blog, we will see how to efficiently retrieve field values of different data types.


//Single Line Of Text {Defualt Value = null}
string businessWebsite = account.GetAttributeValue<string>("new_website");
Console.WriteLine($"Single Line Of text : {businessWebsite}");

//get optionset text
string cohort = account.Attributes.Contains("new_type") ? account.FormattedValues["new_type"] : "";

//Optionset {Default Value = null}
int? customerCohort = account.GetAttributeValue<OptionSetValue>("new_type")?.Value; //currency {Default Value = null}
int? mostRecentACR = (int?)account.GetAttributeValue<money>("new_totalrevenue")?.Value; //Two option {Default Value = false}
bool sdsGradReassign = account.GetAttributeValue<bool>("new_issharedaccount"); //Default Value = null OptionSetValueCollection language = user.GetAttributeValue<OptionSetValueCollection>("csm_languagessupported"); if (language != null) { foreach (var options in language) { int i = options.Value; Console.WriteLine($"Value : {i}"); } } //Lookup EntityReference lookupObj = contact.GetAttributeValue<EntityReference>("contactid"); if(lookupObj != null) { Guid lookupGuid = lookupObj.Id; string lookupText = lookupObj.Name; string entityLogicalName = lookupObj.logicalName }

Retrieve Record -

//Retrieve single record - 
Entity contact = service.Retrive("contact", Guid, new columnset("firstname", "lastname"));

//Retrieve Multiple  [QueryExpression OR Fetch Expression]

//Using Fetch Expression - 
string fetchXml = string.Format(@"
<fetch>
  <entity name='contact'>
    <attribute name='contactid' />
    <attribute name='firstname' />
    <attribute name='lastname' />
    <filter type='and'>
      <condition attribute='firstname' operator='like' value='AB%' />
      <condition attribute='parentcustomerid' operator='eq' value={0} />
    </filter>
  </entity>
</fetch>", accountId);


        // Execute FetchXML
        EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchXml));

        // Loop through results
        foreach (Entity contact in results.Entities)
        {
            string firstName = contact.Contains("firstname") ? contact["firstname"].ToString() : "";
            string lastName = contact.Contains("lastname") ? contact["lastname"].ToString() : "";
            Console.WriteLine($"Contact: {firstName} {lastName}, ID: {contact.Id}");
        }
        
        
        
//Query Expression 
QueryExpression query = new QueryExpression("contact");
query.ColumnSet = new ColumnSet("firstname", "lastname");
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
query.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, contact.Id);
EntityCollection contactCollection = service.RetrieveMultiple(query);


Comments

Popular posts from this blog

Accessing Fields on QuickView Form through javaScript

Custom Subgrid Using HTML Web resource

Check Dirty Fields or Forms in Dynamics 365