SBX - Search With Button

SBX - Forum Post Title

Get company name from "from" Contact in Email...

Microsoft Dynamics CRM Forum

Anthony A asked a question on 6 Oct 2017 10:24 AM

Question Status


Hi Guys, 

When an email comes into our CRM, the "from" is associated to a contact. That contact is associated to a parentcustumerid.

I need the retrieve that id so i can do a query on a the incident entity and see if a case already exists with same customer and subject.

i figured out how to get read entity reference from the email, but i am not sure how to retrieve its information (i could do a query but i'm sure there is a more efficient way) - i guess i need the guid or the name of the account to query the incident entity.

                        EntityCollection fromCollection = (EntityCollection)entity["from"];
                        if (fromCollection != null && fromCollection.Entities.Count > 0)
                            Entity fromParty = fromCollection[0]; //activityparty entity
                            EntityReference fromRef = (EntityReference)fromParty["partyid"];                             
                            ColumnSet attributes = new ColumnSet(new string[] { "new_quicknote", "regardingobjectid", "subject" });                           
                            Entity entty = service.Retrieve(entity.LogicalName, entity.Id, attributes);                            
                            //entty.Attributes["new_quicknote"] =;                            
                            entty.Attributes["regardingobjectid"] = fromRef;
                        context.InputParameters["Target"] = entity;                           

aworden7 responded on 6 Oct 2017 10:57 AM

How I understand your question is the you want the incidents related to the entity reference you already have? If this is the case then the name or id, or some relationship is needed to filter the results.

We use LINQ queries to set up our data access of entities instead of building  query expressions.  Code is much more compact.

Anthony A responded on 6 Oct 2017 1:40 PM

Basically I get an email with a valid CRM contact in the "partylist"

I need to retrieve the parentcustomerID  from that contact.

I figured out how to get the "contact" from the party list but not sure how retrieve attributes from it.

I could use a QueryExpression but i'm sure there is a better way.

aworden7 responded on 6 Oct 2017 2:49 PM

Since you have the entityreference of the contact (guid and name) I am guessing (sorry not sure what variable in your code is the contact), I think the simplest way is to do another Retrieve service call.

ColumnSet contactAttributes = new ColumnSet(new string[] {"parentcustomerid" });
var retrievedContact = service.Retrieve("contact", guid, contactAttributes)
var parentReference = retrievedContact.ParentCustomerId

var myIncident = GetIncidentByAccountId(parentReference.Id);

From here you would have the EntityReference to the parent account and then perform a  query (LINQ or expression) to get incidents associated by guid to the parent account. (Sorry my Linq may not be 10% correct)

 public List<Incident> GetIncidentByAccountId(Guid id)
            return _context.IncidentSet.Where(I => i.AccountId.Value == id).Select(q => new Incident()
Id =,
LogicalName = q.LogicalName,
//your desired mapped attributes

I hope this helps, I am not sure about this being the most efficient way, but this is how I would probably go about doing it.


Anthony A responded on 6 Oct 2017 9:11 PM

I figured out how to read contact entity! (same as what you wrote)

but i can't seem to get to the guid of the "parentcustomerid"

and what you are suggesting does not seem to be recognized on my end? (VS wont let me compile)

var parentReference = retrievedContact.ParentCustomerId

var myIncident = GetIncidentByAccountId(parentReference.Id);

i tried





the last one is the closest i get, but it returns "Microsoft.Xrm.Sdk.EntityReference" instead of a guid?  :(

Anthony A responded on 6 Oct 2017 9:34 PM

Found it!


thanks for your help!

aworden7 responded on 7 Oct 2017 4:34 PM

Apologies if I had some code errors. But I am glad you were able to figure it out!!

Anthony A responded on 8 Oct 2017 8:18 AM

Might not be your code by what context i'm using it in.

I'm very new to C# and CRM plugins. and have not had a chance to buy a simple "how to" guide.(need to find one) most information online are references.

anyhow my 3rd plugin works now! so i'm happy with the results!

Thanks for your help!


SBX - Two Col Forum

SBX - Migrated JS