Thursday, November 24, 2011

Several basic Javascript commands CRM 2011

Case switch:
var valueToSwitch = Xrm.Page.getAttribute("yourField").getValue();

switch(valueToSwitch)
{
case 0:
//what to do if value = 0
break;
case 1:
//what to do if value = 1
break;
case 2:
//what to do if value = 2
break;
}
 Catch error:
try {
//Code
}
catch (ex) {
var txt="You got the following error\n\n" + ex;
alert(txt);
}
Force submit (fields that are marked read only - make scripts able to write to them)
Xrm.Page.getAttribute("youField").setSubmitMode("always"); 
 Forminfo check:
function forminfo()
{
var items = Xrm.Page.ui.formSelector.items.get();
if (items.length > 1)
{
alert("Formtype: " + Xrm.Page.ui.getFormType());
alert("Form ID: " + Xrm.Page.ui.formSelector.getCurrentItem().getId());
alert("Formname: " + Xrm.Page.ui.formSelector.getCurrentItem().getLabel());

else
{
alert("There is only one form item currently available.");
}
}
 Maximize window
window.top.moveTo(0,0);
window.top.resizeTo(screen.width, screen.height);
 Get name of selected picklist value
Xrm.Page.data.entity.attributes.get("crayon_fagomrde").getText();
 Start Dialog from Form (note tings to change)
var dialogId = "a13ad982-d812-40a0-ab67-f314cdabbd2b"; // This must be your Dialog ID
var returnValue = showModalDialog("/" + Xrm.Page.context.getOrgUniqueName() + "/cs/dialog/rundialog.aspx?DialogId=%7b" + dialogId + "%7d&EntityName=account&ObjectId=" + Xrm.Page.data.entity.getId());

//Optional:To save and close entity form as soon as the dialog is closed.
//Xrm.Page.data.entity.save("saveandclose"); 
 Set URL to Iframe
var url = http://www.crayon.com
Xrm.Page.getControl("IFRAME_iFrameName").setSrc(url); //("about:blank") if black
 This was some of the basic javascripts..

Hope it's usefull for you!