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.
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!
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Debajit Dutta,
Thanks for the post.. It helped me a lot..
Hi Chandrasekar,
Welcome. Always great to hear such kind of comments.
Thanks Debajit.
Have you heard anything from Microsoft or read in any Microsoft blog that they will fix it in next rollups?
Hi Ravi,
Thanks for reading my blog post.
I am not sure if that is fixed. I have CRM 2013 SP1 installed on my machine and still I can reproduce this.
I feel its more of because of the way framework is designed. CRM only takes the fields which are dirty. When you click the ribbon button immediately, the onchange of the field does not fire. Hence the field is not dirty and hence you get the old value.