Thursday, August 21, 2008

Referencing an External JS File From a CRM Form




Benefits:

1. Full VS intellisense.
2. Ability to set break points (F9). You can’t do that if your code resides in the
onload/onsave events.
3. Quick development cycle as oppose to the edit -> save -> publish routine.
4. Don't forget to paste the code back in the onload event if you want this to work offline.


/* This goes in the entity onload event */

//create a new script Element
var script2Load = document.createElement("SCRIPT");
script2Load.language = "javascript";
/*
set the src (url) Property with a random number to avoid caching
if you don't set the random number ie will cach your file and you might need to ctrl + f5 to see the changes you make while developing.
*/
script2Load.src = "/ISV/Entity/account.js?nocach=" + Math.random(); //Just an example
//append the element to the head tag (global scope)
document.getElementsByTagName("HEAD")[0].appendChild(script2Load);
//or document.documentElement.childNodes[0].appendChild(script2Load);

/* this goes in the account.js file */

function OnCrmPageLoad()
{

alert('doing stuff');
}

function CallMeFromAnywhare()
{
alert();
}

/*
expose the function to the window scope.
This is important if your going to paste the file back into the entity onload event to support offline mode or a redeployment becouse the script is loaded into the head tag (global scope) and the onload event is a third level function which makes the functions you write unavailable to external calls.
*/
window.CallMeFromAnywhare = CallMeFromAnywhare;

/* last Line */
OnCrmPageLoad();

11 comments:

Stephen Maij said...

Nice post.
Well, if you need to define functions with uses functions from within the javascript file, I prefer this script:

http://www.maijspace.nl/crm-40-loading-custom-javascript-files-prototype

Anonymous said...

I am having strange issues with this.

First, let me say the method works for 99% of our users. We have a single user who gets an error when we use this specific javascript code, (or any other external javascript link code for that matter, we've tried many different ways)...

Anyways, it becomes very strange. If you debug it, it seems that it is loading an error page instead of the javascript file itself. The error page lists an error about the user being disabled or not having a business unit. The user IS enabled and HAS a business unit.

It seems to be some sort of file permission error, I would guess, but have no clue how to fix. The javascript is actually in an aspx file. Consultants set this up for us, I just realized they did it so they could use the ASP include directive to insert more javascript inside a javascript file. So I can't easily rename it to .js and be done with it.

Mainly, I guess I'm confused that its limited to one user and that a user error is being generated. I cannot find a fix anywhere.

Was wondering if you would have any guesses.

Thanks.

Adi Katz said...

Hi,

I would check that the user's CRM groups are defined correctly in the AD.

And try to add the user to the CRM administrator role; do you still get an error?

Adi

Anonymous said...

The user appears to be set up correctly in AD. He appears to be in the same groups as all the other users, the UserGroup and the ReportingGroup. I am somewhat weak in my knowledge of AD though, especially as it relates to CRM.

We have tried giving the user CRM admin role, he still gets the same error.

Thanks.

Anonymous said...

I figured it out partially. First, the problem does not really relate to this script. The problem is more related to asp file and virtual directories, etc. We encountered the problem because we stored our javascript in an apsx file instead of an js file. I later figured out that we encountered this error on any custom aspx page.

Second we are running a multi-tenant environment. We have the same user set up on two different CRM tenants. On the first tenant (the primary one), the user is disabled. So obviously there is some confusion on which tenant is the correct one. I was able to temporarily fix by enabling the user in both tenants.

On the second tenant, the user is enabled. So, maybe I don't have the virtual directory set up correctly, although I also tried just placing the file in the ISV folder with no virtual directory and still go the disabled error.

Adi Katz said...

You need to prepend the organization name to the script file if’s an aspx file.
e.g. script2Load.src = prependOrName( “/isv/myscript.aspx”);
The result would be “/ORGNAME/isv/myscript.aspx”

The prependOrgName function was introduced in CRM 4.0 to support multi-tenancy.

Adi

Harsa said...

You made it like a Breeze. Thanks a ton.

Joe.com said...

Hi Adi

Thanbks for this post.

I'm getting an 'Object Expected' error message on the line 'OnCrmPageLoad();'... It loads the file, and i've cleared the cache, any ideas?

Thanks

Joe

Adi Katz said...

The OnCrmPageLoad(); should also be inside the external file.

If you put it in the onload event handler the call will be made before the file is completly loaded.

Anonymous said...

Sweeeeet, very nice post. Only setback to this awesome approach is by calling the function from inside the js file itself makes it non reusable from other pages, if entry point is a different function.
Is there a way to force the loading of js file before calling the external js file function from CRM onLoad function.
Many thanks.

Anonymous said...

I want to know if there is any configuration to link JS file in Virtual Directory? I am seeing other developer's code which calls just a js function name in Onload event and i couldn't figure out where the link to JS file is set. Any idea. Thanks !!