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
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!
Apologies if I had some code errors. But I am glad you were able to figure it out!!
Found it!
cont.GetAttributeValue<EntityReference>("parentcustomerid").Id
thanks for your help!
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? :(
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.
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.
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156