Advertisements

{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!

Advertisements

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

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

  1. Chandrasekar M says:

    Debajit Dutta,

    Thanks for the post.. It helped me a lot..

  2. Ravi says:

    Thanks Debajit.

    Have you heard anything from Microsoft or read in any Microsoft blog that they will fix it in next rollups?

    • Debajit says:

      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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: