Friday 6 July 2012

Dynamics CRM 2011– Some JavaScript Tips

How to check form mode using Javascript?

Xrm.Page.ui.getFormType() function returns the form type or form mode. Using this function you can make out if the form is currently opened in edit mode or create mode
//Create mode
if (Xrm.Page.ui.getFormType() == 1) {
    //Do this
//Edit Mode
if (Xrm.Page.ui.getFormType() == 2) {
    // Do this

How to get Parent Entity Id when a form is in create mode?

When you add a new record from subgrid of an entity and you need the Parent Entity Id which is the Id of the record from which the child form is created ,you can get it from the query string.

//Create mode
if (Xrm.Page.ui.getFormType() == 1) {
    var qs = new Querystring();
    var Id = qs.get("_CreateFromId", "");

How to set Lookups attributes?

//Using the code in below line you can set the lookup for entity of type 2 i.e. Contacts only
document.getElementById("to").setAttribute("lookuptypes", "2");
//Using the code below you can set the default lookup type to Contacts
document.getElementById("to").setAttribute("defaulttype", "2");

The difference in the above two is that the first would restrict the lookup to Contacts only whereas the second would set the default lookup type as Contacts but would not restrict the lookup to Contacts.

//Using the code below you can restrict the user for single value selection
document.getElementById("to").setAttribute("lookupstyle", "single");
//You can set the icons for selected lookup value using the code below
document.getElementById("to").setAttribute("lookuptypeIcons", "/_imgs/ico_16_2.gif");

How to implement functionality in Outlook?

When you use to popup a new window programmatically, it works fine with web application of CRM. But when you execute it in Outlook the new window will open as an IE window and a Sign In page is displayed. So to open it in Outlook like any other outlook window you can use the function “openStdWin”.

openStdWin("Url", "_blank", width, height, features);

How to implement Oncheck event of checkbox?

When you click on a checkbox the onclick/oncheck event is not fired until the checkbox loses focus. To solve this issue and implement oncheck event immediately after the checkbox is checked or unchecked attach the following function on the onload event of the form and this will fire the onclick event.

// fire Oncheck event of checkbox
function CheckboxOnLoad() {
    crmForm.all.ink_applyvat.onclick = function () {
How to reload a parent form OnRefresh of a subgrid?

To reload a form OnRefresh event of its subgrid attach the following function to the onLoad event of the form

//Reload parent form to reflect changes from related subgrid entity
var interval = null;
function OrderLoad() {
    interval = setInterval("SubGridRefresh();", 15000);
function SubGridRefresh() {
    var grid = document.getElementById("OrderProducts");
    if (grid) {
        grid.attachEvent("onrefresh", ReLoadForm);
        if (interval != null) {
function ReLoadForm() {

The function SubGridRefresh attaches the function ReLoadForm with the OnRefresh event of Subgrid. SubGridRefresh is called after some delay because at the time of OnLoad event the subgrid may or may not have loaded so the event handler cannot be attached in case it is not loaded. So the setInterval function will keep on calling the SubGridRefresh function till the Subgrid is loaded and the event handler is attached.


  1. The first couple of examples, using Xrm.Page… are fine, using the new CRM 2011 JScript syntax.

    The rest of the examples (anywhere you see “getElementbyId”) may work now, but are in the realm of unsupported customizations: buyer beware… Particularly if you're thinking of supporting the new browsers, you need to be careful to use only SDK-supported customizations.

    For CRM Customizations, if it's not in the SDK, it's not OK...

  2. Hello,

    Thanks for you comment.

    However,CRM Anywhere not here yet, Q2 2012 Service Update functionality delayed until Q4 2012

  3. Nice examples.
    Waiting to new posts ))


  4. thanks for examples.

  5. Hello,
    There is unsupported js samples
    Carreful reusers !

    Thank you for your post