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

Question Status

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

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"] = fromRef.name.ToString();                            
                            entty.Attributes["regardingobjectid"] = fromRef;
                            
                            service.Update(entty);                            
                        }                        
                        context.InputParameters["Target"] = entity;                           


Reply
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.


Reply
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.

Reply
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 = q.id,
LogicalName = q.LogicalName,
//your desired mapped attributes
}).ToList<Incident>();
}


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.


 

Reply
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

cont.GetAttributeValue<Guid>("parentcustomerid")

cont.GetAttributeValue<Guid>("parentcustomerid").ID

and

cont.Attributes["parentcustomerid"].Id.

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

Reply
Anthony A responded on 6 Oct 2017 9:34 PM

Found it!

cont.GetAttributeValue<EntityReference>("parentcustomerid").Id

thanks for your help!

Reply
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!!

Reply
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!

Reply