Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, March 6, 2009

Dubugging Javascript in CRM Forms is Easy

Its easy to debug custom JavaScript in CRM forms. Save your JS code in the appropriate event and follow the instruction below. Debugging can be performed in IE or Visual Studio.

Scripting Debugging in Internet Explorer

entering the word "debugger" anywhere in your JS code will prompt IE to break at that point and load the script debugger.

Sunday, February 8, 2009

Reference External Javascript File in CRM Form Events Asynchronously

A few months ago I posted a method of importing external javascript files into CRM forms. See 'Reference External Javascript File in CRM Form Events'.

Some of you may have noticed an issue with this method when including multiple exeternal files. This is due to the fact that the process runs asynchronously while the rest of the code continues to execute.

A friend of mine Sam Manins managed to come up with a full work around for this. Thanks to Sam, the function below can be pasted into your OnLoad event for a more reliable method to import Javascript files into your CRM forms.

The idea is to invoke the command to add your external file, then wait for confirmation that the file was imported. Only then should we allow the code to continue. See comments within the code.

//Paste this function somewhere into your form "OnLoad" event
function includeExternalJSFiles(sec)
{
var errFound = 0;
// load the Ascentium js file into a Script element - do not do this on recursive calls to this function
if (sec == 1)
{
//Include the CRM js file
var Ascentium_Script = document.createElement("script");
Ascentium_Script.language = "javascript";
Ascentium_Script.src = "MyJSFunctions1.js"
document.getElementsByTagName("head")[0].appendChild(Ascentium_Script);
//Include another js file
var Ascentium_Script = document.createElement("script");
Ascentium_Script.language = "javascript";
Ascentium_Script.src = "MyJSFunctions2.js"
document.getElementsByTagName("head")[0].appendChild(Ascentium_Script);
//Add any more JS files you need
}
try
{
//Each file needs to have a simple function(with a unique name) that does nothing that can be called.
//th
en you can call this finction to see if the file has been imported yet.
MyJSFunction1_STUB();//this function is contained in the external JS file
MyJSFunction2_STUB();//this function is contained in the external JS file
}
catch(err)
{
//error found(probably could not find the function
errFound = 1;
}
if (sec < 5 && errFound == 1)
{
sec++;
setTimeout("includeExternalJSFiles(" + sec + ")", 100); //waits 0.1 second before trying the stubs again.
}
else
{
if (errFound == 1)
//Did not work after 5 attempts (0.5 seconds)
//Try a longer time period or output an error message for the user
//alert("Did not work: " + sec);
else
//All functions were found (all files imported)
//No need to do anything. Just exit functions and continue code
//alert("Success after " + sec + " attempts. ");
}
}

Remember to have a unique stub function to run in each external file. the function needs no do anything. Just needs to be there(See sample below).

//this function would be found in the file MyJSFunction1.js
function MyJSFunction1_STUB()
{
//stub function (does nothing)
var a;
}

Finally you need to call the function to import your Javascript files:

includeExternalJSFiles(0);

The above example makes the call to add the files to the page header, then continues to try to run a functions in each file every 0.1 seconds until successful or until 5 failed attempts. Different systems under different workloads may perform the job at different speeds. Feel free to modify the code to suit your needs.

And improvement on this function could be to also pass in the name of each Javascript file in an array, making it more generic.

A big thank you goes to Sam Manins for the solution.

Sunday, September 7, 2008

Microsoft Dynamics CRM Javascript Access to Form Elements

Here's how to access each control on your forms using Javascript. Using this information you can add very useful functionality to CRM's front end.

There are a couple of ways to do this, you can choose which ever suits your situation best...

Currently displayed entitie's GUID:
        crmForm.ObjectId

To access a control on the form:
    crmForm.all.[attributename]
    // OR
    document.getElementById("[attributename]")

Hide/show attribute(set to visible or invisible):
    //Hide it
    crmForm.all.[attributename].style.display = "none";
    //Show it
    crmForm.all.[attributename].style.display = "inline";

Enable/disable attribute:
   //Disable
   crmForm.all.[attributename].Disabled = true;
   //Enable
   crmForm.all.[attributename].Disabled = false;

Does control/attribute contain any data?
   if (crmForm.all.[attributename] != null)
       //Contains some data

Control's value(this is the current on screen value. ie, may not have been saved yet):
   crmForm.all.[attributename].DataValue
   // OR
   crmForm.all.[attributename].value
   // OR if its a checkbox
   crmForm.all.[attributename].Checked = [true false=""]
   // OR if its a lookup
     //Descriptive name for the selected item
     crmForm.all.[attributename].DataValue.name
     //GUID value of the selected item
     crmForm.all.[attributename].DataValue.id
     //Type of entity selected in the lookup(1=Account, 2=Contact...)
     crmForm.all.[attributename].DataValue.type
     //Descriptive name for the selected entity type
     crmForm.all.[attributename].DataValue.typename


Reference External Javascript file in CRM form events

Some times you may have a great deal of Javascript code to insert into your CRM entity's form events, be it OnLoad, OnSave or OnChange of a control.

It is possible to encapsulate the functionality or procedures into separate JS files and reference them from the form. I often do this and have a list of standard JS files with specific functions as I need them. You can insert the file into your event by ading this block of code.

//Insert JS Functions to perform whatever functionality:

var script = document.createElement("script");

script.language = "javascript";

script.src = "http://crmserver:5555/ISV/JSFiles/CRMControlFunctions.js";

document.getElementsByTagName("head")[0].appendChild(script);


Don't forget to substitute your own server, port and filename. Ensure that it's hosted in a website or virtual directory and reachable from the CRMWeb website application.


Saturday, September 6, 2008

Access MS CRM data through the Web Service via Javascript SDK

So I was madly typing away and badly mixing my languages up as I tried to remember how to access the CRM Web Service using Javascript from the front end when I finally succumbed to calling on a Google search.

I don't usually post something that I've already been able to find on the net, but it took me a while to find this and when I did... Oh man it was like a gift from heaven.

A fantastic post by Jason Hunt at Ascentium:
A Microsoft Dynamics CRM JavaScript SDK
http://www.ascentium.com/blog/crm/Post129.aspx