How to identify the type of entity?How do I calculate someone's age in C#?Use of var keyword in C#How do I enumerate an enum in C#?String representation of an EnumHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get the type of T from a member of a generic class or method?How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?What is a NullReferenceException, and how do I fix it?Is there a reason for C#'s reuse of the variable in a foreach?Why not inherit from List<T>?
Difference between system uptime and last boot time in windows
Why is this sentence grammatical?
Make 2019 with single digits
Is it appropriate to CC a lot of people on an email
Can a character with good/neutral alignment attune to a sentient magic item with evil alignment?
Output a Super Mario Image
Why is belonging not transitive?
What is a "major country" as named in Bernie Sanders' Healthcare debate answers?
Why is it called a stateful and a stateless firewall?
In what sequence should an advanced civilization teach technology to medieval society to maximize rate of adoption?
What is the meaning of 「ぞんぞん」?
What 68-pin connector is this on my 2.5" solid state drive?
Bit one of the Intel 8080's Flags register
Would it be unbalanced to increase a druid's number of uses of Wild Shape based on level?
'Overwrote' files, space still occupied, are they lost?
Asked to Not Use Transactions and to Use A Workaround to Simulate One
Why is my fire extinguisher emptied after one use?
Ethernet, Wifi and a little human psychology
2000s space film where an alien species has almost wiped out the human race in a war
Is there any reason to concentrate on the Thunderous Smite spell after using its effects?
Planar regular languages
What are the advantages and disadvantages of tail wheels that cause modern airplanes to not use them?
How to make a bold sparkline in Google Sheets?
International Orange?
How to identify the type of entity?
How do I calculate someone's age in C#?Use of var keyword in C#How do I enumerate an enum in C#?String representation of an EnumHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to get the type of T from a member of a generic class or method?How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?What is a NullReferenceException, and how do I fix it?Is there a reason for C#'s reuse of the variable in a foreach?Why not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
If I have a GUID for a record, but I do not know whether it is an Account or a Contact, how can I retrieve this record and identify its type?
For example, I can retrieve a specified type like so:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
However, if I do not know what type of record it is, how can I retrieve it and identify its type?
Something like this:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
c# .net visual-studio dynamics-crm-2011 dynamics-crm
add a comment
|
If I have a GUID for a record, but I do not know whether it is an Account or a Contact, how can I retrieve this record and identify its type?
For example, I can retrieve a specified type like so:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
However, if I do not know what type of record it is, how can I retrieve it and identify its type?
Something like this:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
c# .net visual-studio dynamics-crm-2011 dynamics-crm
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47
add a comment
|
If I have a GUID for a record, but I do not know whether it is an Account or a Contact, how can I retrieve this record and identify its type?
For example, I can retrieve a specified type like so:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
However, if I do not know what type of record it is, how can I retrieve it and identify its type?
Something like this:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
c# .net visual-studio dynamics-crm-2011 dynamics-crm
If I have a GUID for a record, but I do not know whether it is an Account or a Contact, how can I retrieve this record and identify its type?
For example, I can retrieve a specified type like so:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
However, if I do not know what type of record it is, how can I retrieve it and identify its type?
Something like this:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
c# .net visual-studio dynamics-crm-2011 dynamics-crm
c# .net visual-studio dynamics-crm-2011 dynamics-crm
asked Jan 19 '17 at 16:22
l--''''''---------''''''''''''l--''''''---------''''''''''''
4,641243 gold badges557 silver badges901 bronze badges
4,641243 gold badges557 silver badges901 bronze badges
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47
add a comment
|
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47
add a comment
|
4 Answers
4
active
oldest
votes
You cannot search by the GUID alone.
You need both the GUID and the entity logical name.
Whenever you retrieve a entity reference using code, c# or javascript, the object will include the entity logical name
Update to try to find an account with a given ID:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
EntityName = "account",
TopCount = 1,
Criteria =
Conditions =
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
//No Account Record Found
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
When the condition expression is setup to compareGUIDs
, you don't need such a verbose query withTopCount=1
,organizationService.Retrieve()
will suffice.
– dynamicallyCRM
Jan 19 '17 at 17:09
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
|
show 2 more comments
If you reference the DLaB.Xrm from Nuget, you could write this like this:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
If you wanted to actually get the actual values, you could do this.
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
add a comment
|
If you only need to distinguish between with Account and Contact and want to make sure the record really still exists, you could also use their CustomerAddress, LEFT OUTER JOIN
ing both, Account and Contact:
var query = new QueryExpression
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
Conditions =
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
,
,
LinkEntities =
new LinkEntity
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
,
new LinkEntity
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
,
,
;
... will allow you to retrieve whichever, Account or Contact in one go:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
... but require to access their fields through AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
... which can make reading a lot of attributes a little messy.
add a comment
|
I know this is an old question, but I thought I would add something in case someone stumples upon this in the future with a problem similar to mine. In this case it might not be particularly the same as OP has requested.
I had to identify whether an entity was one or the other type, so I could use one method instead of having to write standalone methods for each entity. Here goes:
var reference = new EntityReference
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
And by passing it into the following method the type can be identified.
public void IdentifyType(EntityReference reference)
switch(reference.LogicalName)
case Account.EntityLogicalName:
//Do something if it's an account.
Console.WriteLine($"The entity is of type nameof(Account.EntityLogicalName)."
case Contact.EntityLogicalName:
//Do something if it's a contact.
Console.WriteLine($"The entity is of type nameof(Contact.EntityLogicalName)."
default:
//Do something if neither of above returns true.
Console.WriteLine($"The entity is not of type nameof(Account.EntityLogicalName) or nameof(Contact.EntityLogicalName)."
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41746737%2fhow-to-identify-the-type-of-entity%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot search by the GUID alone.
You need both the GUID and the entity logical name.
Whenever you retrieve a entity reference using code, c# or javascript, the object will include the entity logical name
Update to try to find an account with a given ID:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
EntityName = "account",
TopCount = 1,
Criteria =
Conditions =
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
//No Account Record Found
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
When the condition expression is setup to compareGUIDs
, you don't need such a verbose query withTopCount=1
,organizationService.Retrieve()
will suffice.
– dynamicallyCRM
Jan 19 '17 at 17:09
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
|
show 2 more comments
You cannot search by the GUID alone.
You need both the GUID and the entity logical name.
Whenever you retrieve a entity reference using code, c# or javascript, the object will include the entity logical name
Update to try to find an account with a given ID:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
EntityName = "account",
TopCount = 1,
Criteria =
Conditions =
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
//No Account Record Found
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
When the condition expression is setup to compareGUIDs
, you don't need such a verbose query withTopCount=1
,organizationService.Retrieve()
will suffice.
– dynamicallyCRM
Jan 19 '17 at 17:09
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
|
show 2 more comments
You cannot search by the GUID alone.
You need both the GUID and the entity logical name.
Whenever you retrieve a entity reference using code, c# or javascript, the object will include the entity logical name
Update to try to find an account with a given ID:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
EntityName = "account",
TopCount = 1,
Criteria =
Conditions =
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
//No Account Record Found
You cannot search by the GUID alone.
You need both the GUID and the entity logical name.
Whenever you retrieve a entity reference using code, c# or javascript, the object will include the entity logical name
Update to try to find an account with a given ID:
Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy
var accountQuery = new QueryExpression
EntityName = "account",
TopCount = 1,
Criteria =
Conditions =
new ConditionExpression("accountid", ConditionOperator.Equal, Id)
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
//No Account Record Found
edited Jan 19 '17 at 16:43
answered Jan 19 '17 at 16:26
Stefan William-WorrallStefan William-Worrall
6387 silver badges21 bronze badges
6387 silver badges21 bronze badges
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
When the condition expression is setup to compareGUIDs
, you don't need such a verbose query withTopCount=1
,organizationService.Retrieve()
will suffice.
– dynamicallyCRM
Jan 19 '17 at 17:09
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
|
show 2 more comments
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
When the condition expression is setup to compareGUIDs
, you don't need such a verbose query withTopCount=1
,organizationService.Retrieve()
will suffice.
– dynamicallyCRM
Jan 19 '17 at 17:09
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
is it possible to just try both and pick one that works? what would that look like?
– l--''''''---------''''''''''''
Jan 19 '17 at 16:29
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
Are you trying to retrieve a regarding object record ? how are you getting your GUID ?
– Stefan William-Worrall
Jan 19 '17 at 16:31
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
i'm trying to retrieve a customer record that would be attached to a serviceappointment activity. the GUID is passed into our API by the client, and the client requires information retrieved for the customer attached to that record. the customer can be an account or contact
– l--''''''---------''''''''''''
Jan 19 '17 at 16:35
1
1
When the condition expression is setup to compare
GUIDs
, you don't need such a verbose query with TopCount=1
, organizationService.Retrieve()
will suffice.– dynamicallyCRM
Jan 19 '17 at 17:09
When the condition expression is setup to compare
GUIDs
, you don't need such a verbose query with TopCount=1
, organizationService.Retrieve()
will suffice.– dynamicallyCRM
Jan 19 '17 at 17:09
1
1
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
This is set up in a way to ensure that no exceptions can be thrown.
– Stefan William-Worrall
Jan 19 '17 at 17:17
|
show 2 more comments
If you reference the DLaB.Xrm from Nuget, you could write this like this:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
If you wanted to actually get the actual values, you could do this.
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
add a comment
|
If you reference the DLaB.Xrm from Nuget, you could write this like this:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
If you wanted to actually get the actual values, you could do this.
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
add a comment
|
If you reference the DLaB.Xrm from Nuget, you could write this like this:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
If you wanted to actually get the actual values, you could do this.
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
If you reference the DLaB.Xrm from Nuget, you could write this like this:
bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;
If you wanted to actually get the actual values, you could do this.
var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;
if (entity != null)
var account = entity as Account; // will be null if the Guid was for a contact
var contact = entity as Contact; // will be null if the Guid was for an account
answered Jan 19 '17 at 18:27
DarylDaryl
14.7k5 gold badges55 silver badges117 bronze badges
14.7k5 gold badges55 silver badges117 bronze badges
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
add a comment
|
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
1
1
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
This seems like a very nice approach. It's very failsafe.
– Zeliax
Mar 28 at 11:37
add a comment
|
If you only need to distinguish between with Account and Contact and want to make sure the record really still exists, you could also use their CustomerAddress, LEFT OUTER JOIN
ing both, Account and Contact:
var query = new QueryExpression
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
Conditions =
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
,
,
LinkEntities =
new LinkEntity
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
,
new LinkEntity
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
,
,
;
... will allow you to retrieve whichever, Account or Contact in one go:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
... but require to access their fields through AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
... which can make reading a lot of attributes a little messy.
add a comment
|
If you only need to distinguish between with Account and Contact and want to make sure the record really still exists, you could also use their CustomerAddress, LEFT OUTER JOIN
ing both, Account and Contact:
var query = new QueryExpression
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
Conditions =
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
,
,
LinkEntities =
new LinkEntity
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
,
new LinkEntity
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
,
,
;
... will allow you to retrieve whichever, Account or Contact in one go:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
... but require to access their fields through AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
... which can make reading a lot of attributes a little messy.
add a comment
|
If you only need to distinguish between with Account and Contact and want to make sure the record really still exists, you could also use their CustomerAddress, LEFT OUTER JOIN
ing both, Account and Contact:
var query = new QueryExpression
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
Conditions =
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
,
,
LinkEntities =
new LinkEntity
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
,
new LinkEntity
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
,
,
;
... will allow you to retrieve whichever, Account or Contact in one go:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
... but require to access their fields through AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
... which can make reading a lot of attributes a little messy.
If you only need to distinguish between with Account and Contact and want to make sure the record really still exists, you could also use their CustomerAddress, LEFT OUTER JOIN
ing both, Account and Contact:
var query = new QueryExpression
EntityName = "customeraddress",
ColumnSet = new ColumnSet("customeraddressid"),
TopCount = 1,
Criteria = new FilterExpression
Conditions =
// limit to Address 1
new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
// filter by "anonymous" GUID
new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
,
,
LinkEntities =
new LinkEntity
EntityAlias = "acc",
Columns = new ColumnSet("name"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "accountid",
LinkToEntityName = "account",
JoinOperator = JoinOperator.LeftOuter
,
new LinkEntity
EntityAlias = "con",
Columns = new ColumnSet("fullname"),
LinkFromEntityName = "customeraddress",
LinkFromAttributeName = "parentid",
LinkToAttributeName = "contactid",
LinkToEntityName = "contact",
JoinOperator = JoinOperator.LeftOuter
,
,
;
... will allow you to retrieve whichever, Account or Contact in one go:
var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();
... but require to access their fields through AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
... which can make reading a lot of attributes a little messy.
answered Jan 26 '17 at 22:29
FilburtFilburt
14k11 gold badges53 silver badges97 bronze badges
14k11 gold badges53 silver badges97 bronze badges
add a comment
|
add a comment
|
I know this is an old question, but I thought I would add something in case someone stumples upon this in the future with a problem similar to mine. In this case it might not be particularly the same as OP has requested.
I had to identify whether an entity was one or the other type, so I could use one method instead of having to write standalone methods for each entity. Here goes:
var reference = new EntityReference
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
And by passing it into the following method the type can be identified.
public void IdentifyType(EntityReference reference)
switch(reference.LogicalName)
case Account.EntityLogicalName:
//Do something if it's an account.
Console.WriteLine($"The entity is of type nameof(Account.EntityLogicalName)."
case Contact.EntityLogicalName:
//Do something if it's a contact.
Console.WriteLine($"The entity is of type nameof(Contact.EntityLogicalName)."
default:
//Do something if neither of above returns true.
Console.WriteLine($"The entity is not of type nameof(Account.EntityLogicalName) or nameof(Contact.EntityLogicalName)."
add a comment
|
I know this is an old question, but I thought I would add something in case someone stumples upon this in the future with a problem similar to mine. In this case it might not be particularly the same as OP has requested.
I had to identify whether an entity was one or the other type, so I could use one method instead of having to write standalone methods for each entity. Here goes:
var reference = new EntityReference
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
And by passing it into the following method the type can be identified.
public void IdentifyType(EntityReference reference)
switch(reference.LogicalName)
case Account.EntityLogicalName:
//Do something if it's an account.
Console.WriteLine($"The entity is of type nameof(Account.EntityLogicalName)."
case Contact.EntityLogicalName:
//Do something if it's a contact.
Console.WriteLine($"The entity is of type nameof(Contact.EntityLogicalName)."
default:
//Do something if neither of above returns true.
Console.WriteLine($"The entity is not of type nameof(Account.EntityLogicalName) or nameof(Contact.EntityLogicalName)."
add a comment
|
I know this is an old question, but I thought I would add something in case someone stumples upon this in the future with a problem similar to mine. In this case it might not be particularly the same as OP has requested.
I had to identify whether an entity was one or the other type, so I could use one method instead of having to write standalone methods for each entity. Here goes:
var reference = new EntityReference
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
And by passing it into the following method the type can be identified.
public void IdentifyType(EntityReference reference)
switch(reference.LogicalName)
case Account.EntityLogicalName:
//Do something if it's an account.
Console.WriteLine($"The entity is of type nameof(Account.EntityLogicalName)."
case Contact.EntityLogicalName:
//Do something if it's a contact.
Console.WriteLine($"The entity is of type nameof(Contact.EntityLogicalName)."
default:
//Do something if neither of above returns true.
Console.WriteLine($"The entity is not of type nameof(Account.EntityLogicalName) or nameof(Contact.EntityLogicalName)."
I know this is an old question, but I thought I would add something in case someone stumples upon this in the future with a problem similar to mine. In this case it might not be particularly the same as OP has requested.
I had to identify whether an entity was one or the other type, so I could use one method instead of having to write standalone methods for each entity. Here goes:
var reference = new EntityReference
Id = Guid.NewGuid(),
LogicalName = "account" //Or some other logical name - "contact" or so.
And by passing it into the following method the type can be identified.
public void IdentifyType(EntityReference reference)
switch(reference.LogicalName)
case Account.EntityLogicalName:
//Do something if it's an account.
Console.WriteLine($"The entity is of type nameof(Account.EntityLogicalName)."
case Contact.EntityLogicalName:
//Do something if it's a contact.
Console.WriteLine($"The entity is of type nameof(Contact.EntityLogicalName)."
default:
//Do something if neither of above returns true.
Console.WriteLine($"The entity is not of type nameof(Account.EntityLogicalName) or nameof(Contact.EntityLogicalName)."
answered Mar 28 at 12:18
ZeliaxZeliax
1,7413 gold badges29 silver badges52 bronze badges
1,7413 gold badges29 silver badges52 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41746737%2fhow-to-identify-the-type-of-entity%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I have a Guid, I don't know whether the guid pertains to an Account entity or a Contact entity
– l--''''''---------''''''''''''
Jan 19 '17 at 16:24
Very similar to having a GUID and trying to query a database with 50 tables, either you query each one of them to see if a record with the GUID exists or you get the name of the table before hand and narrow down your search.
– dynamicallyCRM
Jan 19 '17 at 16:47
that is a fair point
– l--''''''---------''''''''''''
Jan 19 '17 at 16:47