Softchief Solutions

Technologies | Life Tips | Socials & Everything

Javascript Snippets

You can visit the page which explains the client script elaborately here.

Remove Option set Value in Java-script

Xrm.Page.getControl('field name').removeOption(value);

Disable/Enable a Field on the Form

Xrm.Page.getControl('field name').setDisabled(true);

Disable/Enable all Fields of a section

function DisableEnableAllFieldsofSection (sectionname, disablestatus)
{
   var ctrlName = Xrm.Page.ui.controls.get();

   for(var i in ctrlName)
   {
     var ctrl = ctrlName[i];
     var ctrlSection = ctrl.getParent().getName();
     if (ctrlSection == sectionname)
     {
       ctrl.setDisabled(disablestatus);
     }
   }
}

Disable/Enable Tab

 function EnableDisableTAB(tabname, disablestatus)
 {
   var tab = Xrm.Page.ui.tabs.get(tabname);
   if (tab == null)
   {
     alert("Error: The tab: " + tabname + " is not available");
   }
   else
   {
     var tabsections =  tab.sections.get();
     for (var i in tabsections)
     {
       var secname = tabsections[i].getName();
       DisableEnableAllFieldsofSection (secname, disablestatus);
     }
   }
}

Disable/Enable Form

function DisableEnableForm(disablestatus)
{
  var allAttributes = Xrm.Page.data.entity.attributes.get();
  for(var i in allAttributes)
  {
   var myattribute =
      Xrm.Page.data.entity.attributes.get(allAttributes[i].getName());
   var myname = myattribute.getName();
   Xrm.Page.getControl(<controlname>).setDisabled(disablestatus);
  }
}
Disable/Enable All fields of Form including iFrame,Subgrid and Web Resources 
function disableFormFields(onOff)
{
  Xrm.Page.ui.controls.forEach(function (control, index){
    if(ifControlHaveAttribute(control))
    {
       control.setDisabled(onOff);
    }
  });
}

function ifControlHaveAttribute(control)
{
  var controlType = control.getControlType();
  return controlType != "iframe" && controlType != "webresource"
&& controlType != "subgrid";
}

Reset OptionSet Values  CRM

function RefreshOptionsets()
{
   var CustomerTypeItem =
        Xrm.Page.getControl("customertype");
   var AddOption = CustomerTypeItem.getAttribute().getOptions();
   CustomerTypeItem.clearOptions();
   for (var i = 0; i < AddOption.length; i++)
   {
      var option = new Option();
      option.text = AddOption[i].text;
      option.value = AddOption[i].value;
   }
   CustomerTypeItem.addOption(option);
}

Remove OptionSet

Xrm.Page.ui.controls.get("customertype").removeOption();

Add OptionSet

var option = new Option();
option.text = 'text item';
option.value = 1000;
Xrm.Page.getControl("customertype").addOption(option);

 

Prevent Auto Save

 function preventAutoSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {
        eventArgs.preventDefault();
    }
}
%d bloggers like this: