Showing posts with label Relationships. Show all posts
Showing posts with label Relationships. Show all posts

Wednesday, June 26, 2013

RETRIEVING DATA FOR A PARTICULAR RELATIONSHIP

We have faced a situation where the entity was having a relationship but the relationship was not visible in the form. For example, a contact was having a category related. But in the form while creating a category for a contact there were no option for the user to select the contact for the category.
We were creating the record using the plugin and then associating the category to the contact using the AssociateRequest. Please find that blog here.


Before creating a category under a contact we wanted to check whether the same category is already associated with the contact for restricting the duplicates. This was done using the RelatedEntityCollection.

Below is the code snippet for the same.

QueryExpression query = new QueryExpression();
query.EntityName = "new_accountindustry";
query.ColumnSet = new ColumnSet("new_industry", "new_name");

Relationship relationship = new Relationship();

relationship.SchemaName = relationshipName;
RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();

relatedEntity.Add(relationship, query);
RetrieveRequest request = new RetrieveRequest();

request.RelatedEntitiesQuery = relatedEntity;
request.ColumnSet = new ColumnSet("fullname");

request.Target = new EntityReference
{

Id = contactId,
LogicalName = "contact"
};
RetrieveResponse response = (RetrieveResponse)orgService.Execute(request);

RelatedEntityCollection relatedEntityCollection = response.Entity.RelatedEntities;

relatedEntityCollection contains all the categories associated with the contactId passed as the parameter.
From the relatedEntityCollection if we have to get the data in EntityCollection we can use the following line of code.
     
EntityCollection entityCollection = (EntityCollection)relatedEntityCollection.Values.ElementAt(0);


Now we have the entityCollection handy with all the data available. Now I assume you know how to get the data from the entityCollection.


CREATING RECORDS WITH A RELATIONSHIP

We had to create a record with a relationship in CRM 2011. The trick was that in the form there were no option for the user to associate the two entities but still the relationship was present. For example, a contact was having categories associated, but in the form there were no option for the user to select the contact when the category was created.
We were trying to create the category via a plugin. We were able to achieve the same by creating the category first and then associating the category to the contact using AssociateRequest. Below is the code snippet for the same.

Entity category = new Entity("new_category");
//Create the reference entity object for the category.
EntityReference categoryReference = new EntityReference("category", categoryId);
//Add that attribute to the entity.
category.Attributes.Add("category", categoryReference);
//Create the category and get the GUID for the new one.
Guid categoryCreated = service.Create(category);
//Associate the created category with the contact.
AssociateRequest associateRequest = new AssociateRequest
{
Target=new EntityReference("contact",contactId),
RelatedEntities=new EntityReferenceCollection
{
    new EntityReference("category",categoryCreated)
},
Relationship=new Relationship(relationshipName)
};
service.Execute(associateRequest);


 



 
 

Configuration for CRM Plugins

CRM plugins are always great feature where we can automate many functionality which we cannot automate from user interface. But almost eve...