How can we retrieve attribute names from target in plugin??
*This post is locked for comments
How can we retrieve attribute names from target in plugin??
*This post is locked for comments
You can try below code & modify based on your need.
Inspired by: [View:https://stackoverflow.com/a/12403961/7920473]
Entity targetEntity = (Entity)context.InputParameters["Target"];
foreach (KeyValuePair<String, Object> attribute in targetEntity.Attributes)
{
//do something
//Console.WriteLine(attribute.Key + ": " + attribute.Value);
}
Problem is you cannot write single line of code for different datatypes, and have to do null check before consuming value.
//targetEntity.getAttributeValue<string>(“stringfield”)
if(targetEntity.getAttributeValue<EntityReference>(“Lookupfield”) != null)
targetEntity.getAttributeValue<EntityReference>(“Lookupfield”).Value();
Read more:
[View:https://community.dynamics.com/crm/f/117/t/234225]
[View:https://community.dynamics.com/crm/b/crmchap/archive/2016/08/14/the-benefits-of-using-getattributevalue-to-access-crm-attributes-late-bound-c]
Hi,
In addition it is very important to make sure that the fields you are looking into is available in the target.
So be sure you are putting field name or by default is all attributes.
Secondly before accessing any attributes make sure you are having Contains check like below -
string somestring = string.Empty;
if (targetentity.Attributes.Contains("FieldName"))
{
somestring = targetentity.GetAttributeValue<string>("FieldName"); // This is form single line text
}
Hi,
You can simply get the entity object using the below statement within the plugin
Entity targetEntity = (Entity)context.InputParameters["Target"];
And then you can use the below statement to get the attribute value-
var accountName = targetEntity.GetAttributeValue<string>("name");
If you notice in the above statement, you need to set the type (string, int, bool etc) while retrieving the attribute value. I would recommend goig through the below blogs on the similar topic-
www.crmanswers.net/.../getattributevalue-demystified.html
Hope this helps.
Hi ryou,
There are a few ways to do this. This thread has some examples.
Hi ryou,
Do you have existing plugin code. If so, please send a screenshot.
Thanks
André Arnaud de Cal...
293,027
Super User 2025 Season 1
Martin Dráb
231,852
Most Valuable Professional
nmaenpaa
101,156
Moderator