0

I have a html page with this on it.

var accountid = getParameterByName("AccountId");
var account = null;

if (accountid != null)
{
    account = GetEntity("Account", accountid, "Name, piv_BusinessUnit, AccountId");
}

At the bottom of that same page is this

<script src="js/datasource.CRM.js"></script>

Within that file is this

function GetEntity(logicalName, id, columnSet)
{
    return RunQuery(logicalName + "Set?&$filter="+logicalName+"Id eq guid'{" + id + "}'" + "&$select="+columnSet);
}

When running the page I get this error

Uncaught ReferenceError: GetEntity is not defined

Does anyone know any reason why a Javascript function is not found when it is there???

Tom Hanson
  • 873
  • 10
  • 39
  • It's probably out of scope, but how and why is impossible to answer without seeing everything – adeneo Sep 19 '17 at 00:18
  • 1
    Uhm, wait, are you trying to run the function before the script tag is included? If so, move the script tag above the code that executes the function – adeneo Sep 19 '17 at 00:19
  • 1
    _"At the bottom of that same page is this"_ The script appears to be called before the function is defined? – guest271314 Sep 19 '17 at 00:19
  • Unfortunately I can not show all the code due to company policy. I can verify that GetEntity is not inside anything. Its at the same level as other methods in that file which Products.html can find – Tom Hanson Sep 19 '17 at 00:21
  • All the other methods within that file work. i will however attempt that – Tom Hanson Sep 19 '17 at 00:22
  • Have you tried placing `` before call to `GetEntity`? – guest271314 Sep 19 '17 at 00:24
  • If you can't show the code, put together a [mcve] that demonstrates the problem with examples. But it does sound like your call to `getEntity` is being encountered and executed before the function definition is loaded. – Stephen P Sep 19 '17 at 00:24
  • That worked.I guess all the other methods worked because they are called late on in the page lifecycle. What I am doing is trying to execute during the page loading. Thanks guys @adeneo since you were the first, can you add it as an answer and I will tick it – Tom Hanson Sep 19 '17 at 00:25

1 Answers1

1

When including script tags that loads external scripts, they are only parsed as encountered in the DOM, and as such hoisting won't work across script tags.

In other words, you have to include the script, before actually trying to use it.

Here's the classis example, using jQuery before it's included, and failing

<script type="text/javascript">
  $('#epic fail').addClass('wont_work'); // $ is not defined error
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
adeneo
  • 312,895
  • 29
  • 395
  • 388