web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM 365: JavaScript - Error while retrieving parent entity data

(0) ShareShare
ReportReport
Posted on by

Hi

In a CRM Case record I am trying to retrieve information from Account entity. Below is the code I have written but it throws the below error when I debug.

Can anyone help me please?

SCRIPT1014: Invalid character  at JSON.Parse(this.response)

var oAccount = Xrm.Page.data.entity.attributes.get('dcit_accountid');

if(oAccount.getValue()[0] != null)
{
oAccountId = oAccount.getValue()[0].id.replace(/\{|\}/gi,""); //To remove the braces at start and end of GUID
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
var query = "/api/data/v8.2/accounts?$filter= accountid eq " +""+ oAccountId;
//var query = "/api/data/v8.2/accounts?$filter= AccountId eq guid'"+oAccount.getValue()[0].id+"'";
req.open("GET", encodeURI(clientURL + query), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200)
{
JSON.parse(this.response);
}
else {
var error = JSON.parse(this.response).error;
alert("Error retrieving Record – " + error.message);
}
}
};
req.send();
}

*This post is locked for comments

  • ashlega Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Hi,

      your code seems to work.. Try adding "debugger;" right before each of those JSON.parse calls.

      Then open the records, open dev tools(F12), use F5 to reload the form - the code will stop at the debugger line, and you will be able to inspect response/other properties (so you might be able to see what's happening there)

      

      

  • Community Member Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Hi Alex

    I have tried to debug it doesn't hit the breakpoint it straight away throws this error in the debugger window. So it is very difficult to analyse why it is failing.

    Any thoughts please?

    Regards

    B

  • ashlega Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Did you add those "debugger" lines(and did you publish those changes/refreshed the browser)? Could you post the code with debugger lines added?

  • Aric Levin - MVP Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    You can also open the debugger (as Alex mentioned), and add your JavaScript library to the debugger. Once there, put a breakpoint in the line of code that you want to stop.

    If you need to call the function or code from the form load event, refresh the CRM form after you put a breakpoint. This will stop the debugger on your line of code.

  • Community Member Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Hi Alex and Aric

    When I put a debugger and add a value to lookup (it is an onChange JS), it straight away goes to the 3rd breakpoint and throws this error.

    1832.JS-error.PNG

    Regards

    A

  • Aric Levin - MVP Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Can you put the this.response in the Watch window and copy the value?

    This might tell us why you are getting and Invalid character in the response.

  • Community Member Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Hi Aric

    I have attached the screenshot below of the value that I have put in watch window on the right, which shows the value 'undefined'

    watch.PNG

  • Aric Levin - MVP Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    One more thing:

    Can you please Add a Watch for the encodeURI(clientURL + query)?

    Maybe something in the web api call is causing this. You can try pasting the Web api call in a browser window and see what results you are getting. Don't think this is the case, but worth a try.

  • Verified answer
    ashlega Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Also, could you expand "this" on the right side?

    I'm wondering if you'll see responseText (not response) with the correct json there..

    It seems responseText is what we should be using:

    https://www.w3schools.com/xml/ajax_intro.asp

    Now I'm wondering if some browsers support both properties..

  • Community Member Profile Picture
    on at
    RE: CRM 365: JavaScript - Error while retrieving parent entity data

    Hi Alex

    It is very strange. I have attached the screenshots below.

    this.responseText is "undefined" as well.

    In screenshot 2, you can see there are no responses under "this".

    Is this same for you as well?

    0624.error2.PNG

    0624.error2.PNG

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans