JavaScript Helper Functions



//Get the Field Value
function GetFieldValue(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null) {
return Xrm.Page.getAttribute(fieldName).getValue();
} else {
return null;
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Get the OptionSet Value
function GetOptionSetValue(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null) {
return Xrm.Page.getAttribute(fieldName).getValue();
} else {
return null;
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Get the Optionset field text
function GetOptionSetText(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null) {
return Xrm.Page.getAttribute(fieldName).getText();
} else {
return null;
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Gets the Lookup field value
function GetLookupId(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null) {
var value = Xrm.Page.getAttribute(fieldName).getValue();
return value[0].id;
} else {
return null;
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Gets the Lookup field Name
function GetLookupName(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null) {
var value = Xrm.Page.getAttribute(fieldName).getValue();
return value[0].name;
} else {
return null;
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the Field Value
function SetFieldValue(fieldName, value) {
try {
Xrm.Page.getAttribute(fieldName).setValue(value);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the value of the optionset.
function SetOptionSetFieldValue(fieldName, option) {
try {
Xrm.Page.getAttribute(fieldName).setValue(option);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the value of the optionset based on option Text
function SetOptionsetFieldText(fieldName, optionsetText) {
try {
var options = Xrm.Page.getAttribute(fieldName).getOptions();
for (i = 0; i < options.length; i++) {
if (options[i].text == optionsetText)
Xrm.Page.getAttribute(fieldName).setValue(options[i].value);
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the Lookup field
function SetLookupField(guid, name, entitytype, fieldname) {
try {
var value = new Array();
value[0] = new Object();
value[0].id = guid;
value[0].name = name;
value[0].entityType = entitytype;
Xrm.Page.getAttribute(fieldname).setValue(value);
} catch (ex) {
Xrm.Utility.alertDialog(ex.message, null);
}
}

//Show or Hide the Field
function ShowHideField(fieldName, visible) {
try {
var field = Xrm.Page.ui.controls.get(fieldName);
field.setVisible(visible);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Show or Hide the Section
function ShowHideSection(tabName, sectionName, visible) {
try {
var tab = Xrm.Page.ui.tabs.get(tabName);
if (tab != null) {
tab.sections.get(sectionName).setVisible(visible);
} else {
Xrm.Utility.alertDialog("No Tab exists with name: " & tabName, null);
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Show or Hide the Tab
function ShowHideTab(tabName, visible) {
try {
Xrm.Page.ui.tabs.get(tabName).setVisible(visible);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Enable or Disable the Field
function DisableField(fieldName, enabled) {
try {
var field = Xrm.Page.ui.controls.get(fieldName);
field.setDisabled(enabled);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Enable or Disable the Control
function DisableControl(controlName, enabled) {
try {
var control = Xrm.Page.ui.controls.get(controlName);
control.setDisabled(enabled);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the field require level to Required or Not Required
function SetFieldRequired(fieldName, required) {
try {
if (required === true) {
Xrm.Page.data.entity.attributes.get(fieldName).setRequiredLevel("required");
} else {
Xrm.Page.data.entity.attributes.get(fieldName).setRequiredLevel("none");
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the field require level to Recommended or None
function SetFieldRecommended(fieldName, recommended) {
try {
if (recommended === true) {
Xrm.Page.data.entity.attributes.get(fieldName).setRequiredLevel("recommended");
} else {
Xrm.Page.data.entity.attributes.get(fieldName).setRequiredLevel("none");
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the Focus to Control
function SetFocusToControl(fieldName) {
try {
var control = Xrm.Page.ui.controls.get(fieldName);
control.setFocus(true);
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Get the Form Type as Text
function GetFormTypeText() {
try {
var formTypeValue = Xrm.Page.ui.getFormType();
var formType = "";
//0 - undefined
//1 - Create
//2 - Update
//3 - Read Only
//4 - Disabled
//5 - Quick Create (Deprecated)
//6 - Bulk Edit
//11 - Read Optimized (Deprecated)
switch (formTypeValue) {
case 1:
formType = "Create";
break;
case 2:
formType = "Update";
break;
case 3:
formType = "Read Only";
break;
case 4:
formType = "Disabled";
break;
case 5:
formType = "Quick Create";
break;
case 6:
formType = "Bulk Edit";
break;
case 11:
formType = "Read Optimized";
break;
case 0:
formType = "Undefined";
}
return formType;
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

//Set the value to Upper Case
function SetToUpperCase(fieldName) {
try {
if (Xrm.Page.getAttribute(fieldName).getValue() != null && Xrm.Page.getAttribute(fieldName).getValue() != "undefined") {
var value = Xrm.Page.getAttribute(fieldName).getValue();
Xrm.Page.getAttribute(fieldName).setValue(value.toUpperCase());
}
}
catch (ex) {
Xrm.Utility.alertDialog(ex.description, null);
}
}

Advertisements

About Kishore Dhanekula

MS Dynamics CRM and .NET Architect
This entry was posted in Ms Dynamics CRM 2013, MS Dynamics CRM 2015 and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s