Saturday, 7 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 window.open functionality in Outlook?

When you use window.open 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 () {
        crmForm.all.ink_applyvat.FireOnChange();
    };
}
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) {
            clearInterval(interval);
        }
    }
}
 
function ReLoadForm() {
    window.location.reload(true);
}

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.

2 comments: