Retrieve Customer Entity in Dynamics CRM 2016 - dynamics-crm

As we know Dynamics CRM has a specific attribute value: Customer. This value combines the Client and Account entity, but I'm blind or MSDN doesn't have specification about retrieving this field in query.
For example:
QueryByAttribute query = new QueryByAttribute(entName);
query.ColumnSet = new ColumnSet(new String[] { searchAttr });
query.Attributes.Add(searchAttr);
query.Values.Add(searchValue);
EntityCollection retrived = service.RetrieveMultiple(query);
This code accepts entity name and searches the attribute's name and value, but when I run it I don't know which type of entity I get from my DataSouce: Client or Account.
So the question is: is it possible to retrieve Customer entity in one query?

No, you must first know which entity you are trying to retrieve.
Get the value held within the Customer field as an EntityReference:
var customer = entity.GetAttributeValue<EntityReference>("customerid");
Get the LogicalName of the EntityReference:
var customerEntity = customer.LogicalName;

Related

How to pass an EntityReference to add attribute value on a lookup field in Microsoft Dynamics 365 CRM

Entity contact = new Entity("contact");
contact.Attributes.Add("fullname", "h api test");
contact.Attributes.Add("emailaddress1", "hh#devenv1.local");
contact.Attributes.Add("telephone1", "1");
contact.Attributes["parentcusotmerid"] = new EntityReference("Organization", );
Guid contactId = m_OrgServ.Create(contact);
Console.WriteLine(contactId);
The lookup field I want to set
The logicalname of the lookup field is parentcusotmerid, and
m_OrgSerc.create
is basically
Service.create
I am setting attribute values for the fields, it works fine for normal text boxes where I am entering values, however for lookup values it doesn't work. I know lookup fields are of type EntityReference, so I need to know the LogicalName of the entity the lookup is pointing and the Id of the record.
I have tried it but its asking for the GUID of the Organization field now, so I'm not sure if I am going about it the right way?
You cannot set "parentcustomerid" to organization. It's special reference field that takes either Account or Contact entity reference as parameter.
If you want to set it you go like this
contact.Attributes["parentcusotmerid"] = new EntityReference("account", Guid.NewGuid());
or
contact.Attributes["parentcusotmerid"] = new EntityReference("contact", Guid.NewGuid());
where Guid.NewGuid() is Guid of your Account or Contact that you want to reference

What is entity reference and Query Expression ? Please give some simple examples

What is EntityReference and QueryExpression? Please give me some simple examples.
EntityReference
It is used for lookup fields in
365 – e.g. to link records via a 1 to many relationship. The lookup
field is shown on the ‘child’. You need to specify the ‘parent’ entity
type and record id.
For example if you are creating a accountand want to set the primary contact.
Entity account = new Entity("account");
account["name"] = "James Account";
account["primarycontactid"] = new EntityReference("contact", contactId);
service.Create(account);
QueryExpression
QueryExpression provides an object model to construct a query. Queries
can also be created using FetchXML, a proprietary XML based query
language.
For example of you wanted to retrieve all contacts full name and telephone number.
QueryExpression query = new QueryExpression()
{
Distinct = false,
EntityName = Contact.EntityLogicalName,
ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
};
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;

Unable to get name from EntityReference

I'm using CRM 2015 SDK for my plugin. I want the attributes of entity reference in my code. I'm able to get the Guid and Logical Name. But the name returns null for all the entity reference fields. Here is my code:
EntityReference centre= ((EntityReference)quoteEntity.Attributes["mc_centre"]);
Guid centreGuid = centre.Id; //returns Guid
string centreName = centre.Name; //returns null
I have checked the Referenced Entity, "Centre" which uses the 'name' field and has valid value. Has anyone faced the same issue? Am I doing something wrong in my code? I don't want another service call to get the name btw.
the Name property of an EntityReference is not always populated when you cast it from an attribute.
You need to do an additional retrieve if you want to get the name.
This is the relevant MSDN article: EntityReference.Name Property
This property can contain a value or null. This property is not
automatically populated unless the EntityReference object has been
retrieved from the server.
Entity member = service.Retrieve("new_vendor", ((EntityReference)entity["new_vendorname"]).Id, new ColumnSet(true));
or
Entity member = service.Retrieve("new_vendor", Vendor.Id, new ColumnSet(true));
String VendorName = member.Attributes["new_name"].ToString();

Dynamics crm 2011 addlink with relationship campaignlist_association

Relationship relation = new Relationship("campaignlist_association");
Entity campaign = (from c in orgServiceContext.CreateQuery("campaign")
select c).FirstOrDefault<Entity>();
foreach (Guid id in listsMarketingGuid)
{
Entity list = (from l in orgServiceContext.CreateQuery("list")
where l["listid"].Equals(id)
select l).FirstOrDefault<Entity>();
orgServiceContext.AddLink(campaign, relation, list);
orgServiceContext.AddLink(list, relation, campaign);
}
orgServiceContext.SaveChanges();
I would like to add a relation between a marketing list and a campaign but when the SaveChanges statment is executed I got an error "Associate is not supported for CampaignItem".
Do you any idea ?
Thanks
use Associate method as for building relationship:
_service.Associate(EntityLogicalName,EntityId,relationship,relatedEntities);
where EntityLogicalName is name of entity
EntityId is id of entity
relationship:wat kind of relationship
relatedentities:to which entiites u want to build relation of above entity.
it needs to call method AddItemCampaignRequest
I got the error
"Associate is not supported for CampaignItem"
when trying to associate product to campaign. This worked for me:
var request = new AddItemCampaignRequest
{
CampaignId = yourCampaign.Id,
EntityId = productToAssociate.Id,
EntityName = ProductEntity.EntityLogicalName,
};
_serviceProxy.Execute(request);
Creds to Mitch Milam
Hope this will help someone.

Auto-populate fields in email form in Dynamics CRM 2011

Imagine, you want to add an email to a case. The email form opens and the "To" field is auto-populated with the case's customer account.
I want to change the behavior in order to auto-populate the content of "To" with a custom property of the related case.
My first approach was to register a JavaScript for the OnLoad event of the form and let the script change the field. That would work, but I am wondering if there is a smarter way to achieve this. There is already some logic, which fills the "To" field. Is it possible to configure this existing feature?
Any hints are appreciated.
I do not believe that this specific scenario can be done more effectively than how you've already worked it out. I would've suggested looking at the data mappings (left-nav item when you pop open the relationship in the entity's customizations, same concept as discussed in this Dynamics CRM 4.0 article http://www.dynamicscare.com/blog/index.php/modifying-mapping-behavior-between-parent-child-records-in-microsoft-dynamics-crm/), but it does not appear to be applicable to this relationship.
This might help you:
DataService.EntityReference toEntityRef = new DataService.EntityReference();
toEntityRef.LogicalName = "contact";
toEntityRef.Id = Guid.Parse(to_id);
Entity toParty = new Entity();
toParty["partyid"] = toEntityRef;
toParty.LogicalName = "activityparty";
Entity emailEntity = new Entity();
emailEntity.LogicalName = "email";
EntityCollection toCollection = new EntityCollection();
toCollection.Entities = new ObservableCollection<Entity>();
toCollection.Entities.Add(toParty);
emailEntity["to"] = toCollection;
IOrganizationService soapService = ServerUtility.GetSoapService();
IAsyncResult res = soapService.BeginCreate(emailEntity, OnCreateComplete, soapService);
Call Back method:
private void OnCreateComplete(IAsyncResult result)
{
Guid emailId = (IOrganizationService)result.AsyncState).EndCreate(result);
}
Another approach would be to replace the Add Email buttons in the ribbon in order to call a custom JavaScript function. This function could open the mail window with window.open and initialize the To: field by setting the extraqs parameter to configure an ActivityParty for the email about to create. This can be done by setting:
partyid to the id of an allowed entity's instance
partyname to the name of the entity instance
partytype to 2 (for the recipent field, see here for details)
But the extraqs parameter is limited: You can set only one receiver and no other field (from, cc, bcc, ...). Moreover, replacing the buttons would bypass built-in functionality, which may change in future versions,
So I prefer the handling of the OnLoad event of the form.

Resources