Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

Xrm.Page.getAttribute throws an exception "TypeError: this.$2_2.getKey is not a function"

Posted on by 35

Hi all,

I seem to be having a problem where my CRM script is causing an error. I am using Microsoft Dynamics CRM 2016 not 365.

The error is inconsistent, but by using the debugger it seems to be occurring deep in CRM's own code.

Has anyone seen a similar problem to this and know of a solution? I have attached a screenshot and code snippet below.

$.ajax({
  type: 'get',
  contentType: 'application/json; charset=utf-8',
  datatype: 'json',
  url: serverUrl + ODATA_ENDPOINT + '/' + odataSetName + "(guid'" + guid + "')" + select,
  beforeSend: function(XMLHttpRequest) {
    //Specifying this header ensures that the results will be returned as JSON.
    XMLHttpRequest.setRequestHeader('Accept', 'application/json');
  },
  success: function(data, textStatus, XmlHttpRequest) {
    myglobaloutofcourtsettlement = parseFloat(data.d.rsl_prostype_moneyoocsetoffered);
    myglobalfromat = data.d.rsl_prostype_fromat_jurisdictioncourt.Value;
    Xrm.Page.getAttribute('rsl_pros_lawtype').setValue(data.d.rsl_prostype_lawtype.Value);
  },
  error: function(XmlHttpRequest, textStatus, errorThrown) {
    //alert("Error " + errorThrown)
    alert('Error while retrieving Prosecution Type details; Message: ' + $.parseJSON(XmlHttpRequest.responseText).error.message.value);
  }
});

pastedimage1591083677901v1.png

Any help would be greatly appreciated! I'm also happy to answer any questions which may help me solve this issue.

  • JamesNWarner Profile Picture
    JamesNWarner 35 on at
    RE: Xrm.Page.getAttribute throws an exception "TypeError: this.$2_2.getKey is not a function"

    Hi Emily,

    Thanks for getting back to me.

    I'm afraid I'm still using CRM Dynamics 2016 and not CRM Dynamics 365.

    It's all quite complicated, but the exact version of CRM Dynamics which we run has not yet been upgraded to 365.

    I just tried using formContext by calling `executionContext.getFormContext();` but my code now errors with "TypeError: executionContext.getFormContext is not a function" so I don't believe that method is support in CRM Dynamics 2016.

    Thanks for the response though!

  • Emily Zhang Profile Picture
    Emily Zhang on at
    RE: Xrm.Page.getAttribute throws an exception "TypeError: this.$2_2.getKey is not a function"

    Hi James,

    Xrm.Page is retired. You should use new API now. You should use formcontext now.

    https://docs.microsoft.com/en-us/power-platform/important-changes-coming

    https://alphabold.com/xrm-page-is-deprecated-how-do-i-use-formcontext-in-dynamics-365/

    pastedimage1591089097842v1.png 

  • JamesNWarner Profile Picture
    JamesNWarner 35 on at
    RE: Xrm.Page.getAttribute throws an exception "TypeError: this.$2_2.getKey is not a function"

    Hi David,

    Thanks for the quick reply and the suggestion.

    I'm pretty certain it's getAttribute too. It appears to be solely an issue with this attribute, as we are calling getAttribute in a lot of places.

    The attribute may or may not be on the form, as we are checking in the onload event.

    However, usually, when we want to check if a field exists on the form we would do something like `Xrm.Page.getAttribute(attributeName)` and if that returns null then the field does not exist.

    Do you know of another way to safeguard against the field/attribute not existing?

    Many thanks!

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Xrm.Page.getAttribute throws an exception "TypeError: this.$2_2.getKey is not a function"

    I'd suggest you split out the code to check which part of this is throwing the error - you're accessing 3 functions in the 1 line (getAttribute, setValue and Value), and if you split this onto 3 separate lines you can be sure which has the problem (based on the error and the debug it looks like getAttribute, but it would be best to be sure)

    Then, is the issue solely with this attribute, or does it occur with others ?

    Is the attribute on the form, and if so, how many times, and where (main form, header, BPF) ?

    When does the code run ? If it's in the onload event, then it may be that not all script has loaded yet

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans