Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 230

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;                           


*This post is locked for comments

  • Anthony A Profile Picture
    Anthony A 230 on at
    RE: Get company name from "from" Contact in Email...

    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!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get company name from "from" Contact in Email...

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

  • Anthony A Profile Picture
    Anthony A 230 on at
    RE: Get company name from "from" Contact in Email...

    Found it!

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

    thanks for your help!

  • Anthony A Profile Picture
    Anthony A 230 on at
    RE: Get company name from "from" Contact in Email...

    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?  :(

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get company name from "from" Contact in Email...

    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.


     

  • Anthony A Profile Picture
    Anthony A 230 on at
    RE: Get company name from "from" Contact in Email...

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get company name from "from" Contact in Email...

    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.


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans