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

Retrieve Data From The Contact Entity Information

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

We specialize in providing top-notch FetchXML and software development services tailored to meet your business needs. Whether you’re looking to enhance your data retrieval processes with FetchXML or develop robust software solutions, our team of experts is here to help. Reach out to us today to discuss your project requirements, get a free consultation, or ask any questions you might have at info@aegisinfoways.com.

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

Your Website Is Not the Problem. Your Workflo...

Your Website Is Not the Problem. Your Workflo...

Nobody plans for this. The business starts with a website maybe something knocked together quickly, fine for the time and then one day there's a request for a booking widget. Then a client login. Then a spreadsheet that one particular person updates every Thursday...

Top Microsoft Dynamics 365 Business Central I...

Top Microsoft Dynamics 365 Business Central I...

Beyond simple accounting and inventory programs, software programs are needed to process the finance, sales, and purchasing functions of a business and connect them to the inventory and customer data. Microsoft Dynamics 365 Business Central software addresses this...

Dynamics 365 Connectors: How to Connect D365 ...

Dynamics 365 Connectors: How to Connect D365 ...

A sales rep calls a customer about a renewal. The customer says they cancelled last week, and the billing system agrees. Dynamics 365 still shows the account as active, so the call is awkward and the forecast is wrong. Most Dynamics 365 rollouts run into this within a...