Looking for an Expert Development Team? Take two weeks Trial! Try Now or Call: +91.9824127020

Blog, C#

Accessing different type of attributes from fetchXML link entity

This blog you may use as a quick guide for accessing different type of attributes value from the link-entity using C#. So here in this blog I am retrieving data from the contact entity information with that I also retrieve link entity which is account entity where I am basically retrieving all type of attributes. I went through many blogs but seems there is no blog available where I could get all type attribute access code. I almost cover all the type of attributes which we usually retrieve in our day to the day development.

The tricky things which we usually miss alias name in the fetchXML. Alias name comes automatically when you build FetchXML using advanced find. You can give any name which you like in the fetchXML.

<link-entity name="" account"" from="" accountid"" to="" parentcustomerid"" visible="" false"" link-type="" outer"" alias="" ab"">

We need to use the alias name and dot before use the attributes name when you wanted to retrieve data using link entity. For example, if we retrieve accountnumber attributes value from account entity we need to use ab.accountnumber.

Here is the sample code you could refer

var fethXML = @"<fetch version="" 1.0"" output-format="" xml-platform"" mapping="" logical"" distinct="" true"">
<entity name="" contact"">
<attribute name="" fullname"" /><attribute name="" telephone1"" />
<attribute name="" contactid"" />
<order attribute="" fullname"" descending="" false"" />
<link-entity name="" account"" from="" primarycontactid"" to="" contactid"" link-type="" inner"" alias="" ae"">
<filter type="" and""><condition attribute="" accountcategorycode"" operator="" eq"" value="" 1"" /></filter>
</link-entity><link-entity name="" account"" from="" accountid"" to="" parentcustomerid"" visible="" false"" link-type="" outer"" alias="" ab"">
<attribute name="" accountnumber"" /><attribute name="" tel_hobby"" />
<attribute name="" accountcategorycode"" />
<attribute name="" address1_latitude"" />
<attribute name="" createdby"" /><attribute name="" createdon"" />
<attribute name="" description"" /><attribute name="" donotemail"" /><attribute name="" exchangerate"" />
<attribute name="" onholdtime"" /><attribute name="" ownerid"" /><attribute name="" processid"" />
<attribute name="" revenue"" />
</link-entity>
</entity>
</fetch>";
EntityCollection entityCollectionResult = new EntityCollection();
entityCollectionResult = Service.RetrieveMultiple(new FetchExpression(fethXML));
if (entityCollectionResult.Entities.Count > 0)
{
foreach (Entity entity in entityCollectionResult.Entities)
{
//---------Getting accountnumber (AccountNumber) - Single Line of Text Field--------//
string accountnumber;
AliasedValue accountnumberAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.accountnumber");
if (accountnumberAliasVal != null)
{
accountnumber = accountnumberAliasVal.Value.ToString();
}
//---------revenue (Revenue) - Currency Field--------//
decimal revenue;
AliasedValue revenueAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.revenue");
if (revenueAliasVal != null)
{
revenue = ((revenueAliasVal.Value) as Money).Value;
}
//---------createdon (CreatedOn) - Date and Time Field--------//
DateTime? createdon;
AliasedValue createdonAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.createdon");
if (createdonAliasVal != null)
{
createdon = (DateTime)(createdonAliasVal.Value);
}
//---------exchangerate (ExchangeRate) - Decimal Number Field--------//
decimal exchangerate;
AliasedValue exchangerateAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.exchangerate");
if (exchangerateAliasVal != null)
{
exchangerate = Convert.ToDecimal(exchangerateAliasVal.Value);
}
//---------address1_latitude (Address1_Latitude) - Floating Point Number Field--------//
decimal latitude;
AliasedValue latitudeAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.address1_latitude");
if (latitudeAliasVal != null)
{
latitude = Convert.ToDecimal(latitudeAliasVal.Value);
}
//---------createdby (CreatedBy) - Lookup Field--------//
Guid createdByGuid = new Guid();
string createdByName = string.Empty;
AliasedValue createdByAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.createdby");
if (createdByAliasVal != null)
{
createdByGuid = ((createdByAliasVal.Value) as EntityReference).Id;
createdByName = ((createdByAliasVal.Value) as EntityReference).Name;
}
//---------description (Description) - Multiple Lines of Text Field--------//
string description;
AliasedValue multilineTextAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.description");
if (multilineTextAliasVal != null)
{
description = multilineTextAliasVal.Value.ToString(); ;
}
//---------accountcategorycode (Category) - Optionset Field--------//
Int32 categoryVal;
AliasedValue categoryAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.accountcategorycode");
if (categoryAliasVal != null)
{
categoryVal = ((categoryAliasVal.Value) as OptionSetValue).Value;
}
//---------ownerid (OwnerId)-Owner type Field--------//
Guid ownerId = new Guid();
string ownerName = string.Empty;
AliasedValue ownerAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.ownerid");
if (ownerAliasVal != null)
{
ownerId = ((ownerAliasVal.Value) as EntityReference).Id;
ownerName = ((ownerAliasVal.Value) as EntityReference).Name;
}
//---------donotemail - DoNotEMail--Two Options Field--------//
bool donotemailFlag;
AliasedValue donotemailAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.donotemail");
if (donotemailAliasVal != null)
{
donotemailFlag = (Boolean)donotemailAliasVal.Value;
}
//---------onholdtime - OnHoldTime - Whole Number Field--------//
Int32 onholdtime;
AliasedValue onholdtimeAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.onholdtime");
if (onholdtimeAliasVal != null)
{
onholdtime = (Int32)onholdtimeAliasVal.Value;
}
//---------Getting tel_hobby(Hobby) - Multiselect optionset--------//
Int32 hobby;
AliasedValue hobbyAliasVal = entity.GetAttributeValue<AliasedValue>
("ab.tel_hobby");
if (hobbyAliasVal != null)
{
List<int>
optionsValue = new List<int>
();
OptionSetValueCollection hobbies = ((hobbyAliasVal.Value) as OptionSetValueCollection);
foreach (OptionSetValue opt in hobbies)
{
optionsValue.Add(opt.Value);
}
}
}
}

Post Tags

#fetchXML

Aegis Infoways

Aegis Infoways is a leading software development company that provides a wide range of business solutions like software development, data warehouse, or web development for specific business needs.

Related Posts

CompletableFuture in Java

CompletableFuture in Java

Technology CompletableFuture is used for asynchronous programming in Java. Asynchronous Programming means running tasks in a separate thread, other than the main thread, and notifying the execution progress like completion or failure. It helps improve application...

Best Practices Things That Help Ms CRM Develo...

Best Practices Things That Help Ms CRM Develo...

It is always the clever MS CRM developers who think about configuration first before customization. Although Dynamics CRM offers many things, such as flexibility and customization, developers need to be more careful about customizing CRM objects. Smarter developers...

10 Eclipse Java Plug-ins You Can’t Do Witho...

10 Eclipse Java Plug-ins You Can’t Do Witho...

Eclipse is the most widely used integrated development environment for Java. Used to develop the Java applications, Eclipse is also often used to develop applications. Its extensive plug-ins give it the flexibility to be customized. This open-source software has...

×