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);


 



 
 

CRM 2011 - Checkbox onchange event not trigerring

I have faced a strange scenario that the onchange event for the check box in CRM 2011 is not working on my colleagues laptop but the same is working in my laptop as expected. Got shocked due to this behavior.

Later on searching in the net came to know that there are some more people in this shock and they have managed to come out of this with a solution.

Basically the problem was with the IE version which we both were using. I was using IE 10 and it worked properly. My colleague was using IE 8 in which it was not working.

In IE 8 the event will work only when the focus is taken out of the control. This is a kind of work around, but the problem is that this will not be accepted by some of the clients. :)

For that there is another work around. Please follow this blog for the same.

Also you can refer to the blog which I was referring to here.

Wednesday, June 12, 2013

CRM 2011 - Saving a record using javascript

Using JavaScript we can initiate the save operation in CRM 2011.
 
The Xrm.Page.data.entity attribute collection has inbuilt method Save() to achieve this particular action. With respect to the parameter passed via this method we can achieve save the form action, save and close the form, save and open a new form actions easily.
 
Below are the examples to achieve this,
 
function Save()

{
// Saves the record.
Xrm.Page.data.entity.save();
}
function SaveAndClose()

{
// Saves the record and closed the window.
Xrm.Page.data.entity.save("saveandclose");
 
 
}
function SaveAndNew()
 
{
// Saves the record and open a new window.
Xrm.Page.data.entity.save("saveandnew");
 
 
}

CRM 2011 - Cancelling the save operation

Whenever we are saving a record in CRM 2011 there can be a lot of scenarios where we have to do some validation with respect to business flow for the forms. Such a case is checking for a required field or a business validation.
 
Required field validation can be implemented by marking the field are required. But this will help while saving the record.
 
Let's look at an example for lead entity. As per my requirement while saving a lead record, its not mandatory to have a particular field. But when the lead is getting qualified that field is required. Such cases can be implemented by using two functions in the client side itself.
 
Use the savemode() method to identify whether the save event was initiated as a result of click of qualify button for the lead. 
 
If that is the case, check for the field and make sure the field is having a value.
 
If the field is not having an entry prevent the save operation using the
 
ExecutionObj.getEventArgs().preventDefault()
 
Execution object will be available by checking the check box to pass the execution context as the first parameter to the method. Please check my previous blog for more details. 
 
Below is an example for the above scenario.
 
function qualify_lead_onclick(context)
 
{
var saveMode = context.getEventArgs().getSaveMode();
//Lead qualifies.
if (saveMode == 16)
 
{
if (Xrm.Page.getAttribute('fieldname').getValue() == null)
 
{
context.getEventArgs().preventDefault();
alert('Field Name cannot be empty while qualifying a lead.');
 
}
}
}
 

Tuesday, June 11, 2013

CRM 2011 - How Save event is Initiated using Javascript?

I have recently encountered a scenario where I wanted to track the save event initiated while qualifying a lead to make sure some validations has to be implemented.

Using JavaScript we can identify each of the save event mode from CRM 2011 application using the below method.

executionobj.getEventArgs().getSaveMode();

This method returns a number from which the save initiated entity and the save mode can be identified.

Below are the supported return values for CRM 2011.
  • Applicable to all the entities
    • 1 - Save
    • 2 - Save and Close
    • 59 - Save and new
    • 5 - Deactivate
    • 6 - Reactivate
  • Activities entity
    • 58 - Save as completed
  • User or Teams owned entities
    • 47 - Assign
  • Email
    • 7 - Send
  • Lead
    • 16 - Qualify
    • 15 - Disqualify
There are many unsupported return values which can also be used. But that has to be on our own risk to use them.

When this method is added to the form make sure to enable the check box "Pass execution context as first parameter". This will get us the executionobj mentioned above. Below is an example of the sample method.

function getSaveMode(executionobj)



{
 
    return executionobj.getEventArgs().getSaveMode();


  
} 

CRM 2011 - Form Type using JavaScript

In our real life scenarios we have several occasion where we have to identify the state of the action on the page, whether its a Creation of a record, updating a record and so on. In such case, using java Script we can identify the state and do our next step accordingly.

Xrm.Page.ui contains different methods to retrieve information about the user interfaces and its sub components in the form. Within that scope we have a method using which we can achieve this requirement.

Xrm.Page.ui.getFormType()

This method indicates the form context for the record. Indication of the form type is through an integer value from which we can identify the state of the record. Below are the list of possible numbers which can be returned via this method.

  • 0 - Undefined
  • 1 - Create
  • 2 - Update
  • 3 - Read Only
  • 4 - Disabled
  • 5 - Quick Create (Deprecated but still the same is supported)
  • 6 - Bulk Edit
  • 11 - Read optimized
Example:

var formType = Xrm.Page.ui.getFormType();

switch (formType)



{
 
case 0: //Logic for undefined.

break;

case 1: //Logic for Create.

break;

case 2: //Logic for Update.

break;

case 3: //Logic for Read Only.

break;

case 4: //Logic for Disabled.

break;

case 5: //Logic for Quick Create.

break;

case 6: //Logic for Bulk Edit.

break;

case 11: //Logic for Read optimized.

break;


}

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...