{Dynamics CRM} Not getting latest Text field value on click of custom ribbon button in CRM 2013

Recently in my project, I was faced with the above problem. The exact scenario is explained below.

I have a text field for entering the Device Id in CRM. The user enter a value in the textbox and clicks on the the ‘Determine 54-90’ custom ribbon button without tabbing out from the textbox as shown in the screenshot below.

Capture101

The problem is inside the function that is called from ribbon button click, the value obtained using Xrm APIs for this Device ID field does not reflect the latest value entered. This is because the user has not tabbed out from the field before clicking the ribbon button.

In CRM 2011, the following code during onload of the form would resolve the issue.

document.getElementById(“<controlid>”).onblur = function()
{
var latestValue = document.getElementById(“<controlid>”).value;
}

However the same code might not work in CRM 2013 because in CRM 2013, the rendering model has changed completely. On load of the form all the controls are loaded as div and the value of the control is shown inside the span of the Div.

The moment the user clicks on the text field, it becomes an input field. So the scenario would be:

1. if the user don’t tab out of the textfield, check for the latest field value using a jquery

2. If the user has tabbed out, get the value of the textfield using the XRM API

the code below achieves the same.

if ($(“input[attrname='[field_logicalname]'”).length > 0) {
           Xrm.Page.getAttribute(“[field_logicalname]”).setValue($(“input[attrname='[field_logicalname]'”).val());
}

 

Hope this helps!