The post CRM 2011 Javascript Basics appeared first on CRM Kitchen.
]]>Why we use Javascript in CRM ?
Get the value from field
Xrm.Page.getAttribute("fieldname").getValue();
Set the value of a field
Xrm.Page.getAttribute("fieldname").setValue(value);
Set the value of a disabled field (Read-Only)
Xrm.Page.getAttribute("fieldname").setSubmitMode("always"); Xrm.Page.getAttribute("fieldname").setValue(value);
Set the Requirement level
Xrm.Page.getAttribute("fieldname").setRequiredLevel("none"); Xrm.Page.getAttribute("fieldname").setRequiredLevel("required"); Xrm.Page.getAttribute("fieldname").setRequiredLevel("recommended");
Get Form Type
//Create : 1, Update : 2, Read-Only : 3, //Disabled : 4, QuickCreate : 5, BulkEdit : 6 var formType = Xrm.Page.ui.getFormType();
Set the value of a lookup field
function setLookupValue (fieldname, id, name, entityType){ var lookupValue = new Array(); lookupValue[0] = new Object(); lookupValue[0].name = name; lookupValue[0].entityType = entityType; lookupValue[0].id = id; Xrm.Page.getAttribute("fieldname").setValue(lookupValue); }
Hide / Show a field
Xrm.Page.getControl("fieldname").setVisible(false);
Hide / Show a Tab
Xrm.Page.ui.tabs.get(tabName).setVisible(visible);
Save the form & Save and Close the from
Xrm.Page.data.entity.save() Xrm.Page.data.entity.save("saveandclose")
Determine whick fields on the form are dirty
var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes){ var attribute = attributes[i]; if(attribute.getIsDirty()) alert(attribute.getName() + " is dirty"); }
Get ID / Guid of the current record
Xrm.Page.data.entity.getId();
Get ID / Guid of the current user
Xrm.Page.context.getUserId();
Get the Security Roles of the current user
var roles = Xrm.Page.context.getUserRoles();
Get the CRM Server URL
Xrm.Page.context.getServerUrl();
Refresh a SubGrid
Xrm.Page.ui.controls.get("target").refresh();
Change the default entity in the lookup window
function changeDefaultLookup(){ document.getElementById("fieldname").setAttribute("defaulttype","2"); var ViewId = "BBD2S5C5-53E3-4C69-ADDD-802327E67A0D"; Xrm.Page.getControl("fieldname").setDefaultView(viewId); }
Popup an existing CRM Record
Xrm.Utility.openEntityForm("entityname",recordId); //or //Set features for how the window will appear var feature = "location=no,menubar=no,status=no,toolbar=no"; // Get the CRM Server URL window.open(serverUrl + '/main.aspx?etnENTITYNAME&pagetype=entityrecord&id= ' + encodeURIComponent(ENTITYID), '_blank', features, false);
Popup a new CRM form ( blank form)
Xrm.Utility.openEntityForm("entityname")
Popup a new CRM form with default values
var parameters = { new_name : "Customer Name", customerid : contactId, customeridname : contactName, customeridtype : "contact" } Xrm.Utility.openEntityForm("entityname",null,parameters);
Popup / Trigger the lookup window associated to a lookup field
window.document.getElementById('fieldname').click()
Popup an OK / Cancel Dialog ( Alert )
if (confirm("Are you sure ? ")){ // OK is selected } else{ // Cancel is selected }
Refresh the Ribbon
Xrm.Page.ui.refreshRibbon()
Disable the all fields in the form
Xrm.Page.ui.controls.forEach (function (control, index){ control.setDisabled(true); }
Expand / Collapse a Tab
Xrm.Page.ui.tabs.get("tabname").setDisplayState(true)
Get all Required Fields in CRM form
function getRequiredFields(){ var arr = []; Xrm.Page.data.entity.attributes.forEach (function (attribute, index){ if (attribute.getRequiredLevel() == "required"){ arr.push(attribute); } }); return arr; }
Set / Change Label of field in CRM form
Xrm.Page.ui.controls.get("fieldname").setLabel("example");
Reload / Refresh a form in CRM
window.location.reload(true);
Convert to Upper String Value
value.toUpperCase()
Change CRM Field CSS Style (font, color etc.)
document.getElementById(fieldname + "_c").style.font = fontstyle; document.getElementById(fieldname + "_c").style.color = colorname;
Set as Read Only
Xrm.Page.ui.controls.get("fieldname").setDisabled(true);
The post CRM 2011 Javascript Basics appeared first on CRM Kitchen.
]]>