web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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 Profile Picture
    230 on at

    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
    on at

    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
    230 on at

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

  • Anthony A Profile Picture
    230 on at

    Found it!

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

    thanks for your help!

  • Community Member Profile Picture
    on at

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

  • Anthony A Profile Picture
    230 on at

    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!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans