ttajic
5/5/2016 - 11:18 AM

Generali Form Javascript Functions

Generali Form Javascript Functions

function adInitScript()
{
  return;
}

adRefreshAssociatedViewIFrame=function(iFrameName, gridName){
    //if in insert mode, return
	try{
	if (Xrm && Xrm.Page && Xrm.Page.ui && Xrm.Page.ui.getFormType() != 0)
	{
		if (Xrm.Page.ui.getFormType() == 1){
			return;
		}
	}
	}
	catch(ex)
	{}
	
    //reference to the iframe
    objIFrame = document.all[iFrameName];
    if (objIFrame != null) {
      var grid =  objIFrame.contentWindow.document.getElementById(gridName);
      if (grid != null)
      {
        grid.Refresh();
      }
    }
	else
	{
	  objIFrame = parent.document.all[iFrameName];
	  if (objIFrame != null) {
	    var grid =  objIFrame.contentWindow.document.getElementById(gridName);
	    if (grid != null)
	    {
		  grid.Refresh();
	    }
	  }
	  else
	  {
	    var grid =  document.getElementById(gridName);
	    if (grid != null)
	    {
	  	  grid.Refresh();
	    } 
	  }
	}
}

adGetAssociatedGrid = function(iFrameName, gridName){
    //if in insert mode, return
    if (Xrm.Page.ui.getFormType() == 1){
        return;
    }

    //reference to the iframe
    objIFrame = document.all[iFrameName];
    if (objIFrame != null) {
      var grid =  objIFrame.contentWindow.document.getElementById(gridName);
      if (grid != null)
      {
        grid.Refresh();
      }
    }
}

adOpenNavItem = function(navItemName, iFrameName, gridName)
{
    //open asociated view and refresh grid
	try
	{
	  Xrm.Page.ui.navigation.items.get(navItemName).setFocus();
	}
	catch(ex)
	{
		try
		{
		  parent.Xrm.Page.ui.navigation.items.get(navItemName).setFocus();
		}
		catch(ex1)
		{

		}
    }
	try
	{ 
	  adRefreshAssociatedViewIFrame(iFrameName,gridName);		
	}
	catch(ex1){}
}

////////////////////////////////////// General functions
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READONLY = 3;
var CRM_FORM_TYPE_DISABLED = 4;
var CRM_FORM_TYPE_BULKEDIT = 6;
var CRM_FORM_TYPE_UNDEFINED = 0;

function getLookupFieldTextValue(fieldName) {
    try
    {
        var lookupFieldText = Xrm.Page.data.entity.attributes.get(fieldName).getValue()[0].name;
        return lookupFieldText;
    }
    catch(e)
    {
        return "";
    }
}

function getLookupValueId(fieldName) {
    try{
        if (Xrm.Page.data.entity.attributes.get(fieldName).getValue().length>0)
        {
            var primaryContactGUID = Xrm.Page.data.entity.attributes.get(fieldName).getValue()[0].id;
            return primaryContactGUID
        }
        return null;
    }
    catch(e){
        return null;
    }
}

function getLookupValueType(fieldName) {
    try{
        if (Xrm.Page.data.entity.attributes.get(fieldName).getValue().length>0)
        {
            var valueType = Xrm.Page.data.entity.attributes.get(fieldName).getValue()[0].type;
            return valueType
        }
        return null;
    }
    catch(e){
        return null;
    }
}

function getTextFieldValue(fieldName)
{
    var textValue = Xrm.Page.data.entity.attributes.get(fieldName);
    return textValue.getValue();
}
function setTextFieldValue(fieldName, fieldValue)
{
    var field = Xrm.Page.data.entity.attributes.get(fieldName);
    field.setValue(fieldValue);
    ForceFieldSubmit(fieldName);
}

function getNumericFieldValue(fieldName)
{
    var fieldValue = Xrm.Page.data.entity.attributes.get(fieldName);
    return fieldValue.getValue();
}
function setNumericFieldValue(fieldName, fieldValue)
{
    var field = Xrm.Page.data.entity.attributes.get(fieldName);
    field.setValue(fieldValue);
    ForceFieldSubmit(fieldName);
}

function setDateField(fieldName, fieldValue) 
{
    Xrm.Page.data.entity.attributes.get(fieldName).setValue(fieldValue);
    ForceFieldSubmit(fieldName);
}

function getDateValue(fieldname) {
    try
    {
        return Xrm.Page.data.entity.attributes.get(fieldname).getValue();
    }
    catch(e)
    {
        return "";
    }
}

function getDateValueString(fieldname) {
    try{
        var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
        if (d != null) {
            var curr_date = d.getDate();
            var curr_month = d.getMonth();
            curr_month++;  // getMonth() considers Jan month 0, need to add 1
            var curr_year = d.getFullYear();
            return curr_date + "." + curr_month + "." + curr_year;
        }
        else {
            return "";
        }
    }
    catch(e)
    {
        return "";
    }
}

function getOptionSetValue(fieldName)
{
    var field = Xrm.Page.data.entity.attributes.get(fieldName);
    var fieldValue = field.getValue();
    return fieldValue;
}
function getOptionSetTextValue(fieldname) {
    try
    {
        var field = Xrm.Page.data.entity.attributes.get(fieldname);
        fieldValue = field.getSelectedOption().text;
        if (fieldValue != null) {
            return fieldValue;
        }
        return "";
    }
    catch(e)
    {
        return null;
    }
}


function getBoolFieldValue(fieldName)
{
    return Xrm.Page.data.entity.attributes.get(fieldName).getValue();
}
function setBoolFieldValue(fieldName)
{
    var field = Xrm.Page.data.entity.attributes.get(fieldName);
    field.setValue(fieldValue);
    ForceFieldSubmit(fieldName);
}

function getFirstActivityParty(fieldName)
{

}

function setLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
        ForceFieldSubmit(fieldName);
    }
}

function hideField(fieldName) {
    var field = Xrm.Page.ui.controls.get(fieldName);
    field.setVisible(false);
}

function showField(fieldName) {
    var field = Xrm.Page.ui.controls.get(fieldName);
    field.setVisible(true);
}

function clearField(fieldName) {
    var field = Xrm.Page.getAttribute(fieldName);
    field.setValue(null);
}


/*
 Valid levels:
 none
 required
 recommended
 * */
function SetRequirementLevel(fieldName, level) {
    var field = Xrm.Page.data.entity.attributes.get(fieldName);
    field.setRequiredLevel(level);
}

function SetDisabledState(fieldName, enabled) {
    try{
        var field = Xrm.Page.ui.controls.get(fieldName);
        field.setDisabled(enabled);
    }
    catch(e){}
}

function refreshGrid(gridName)
{
    try{
        var targetgird = Xrm.Page.ui.controls.get(gridName);
        targetgird.refresh();
    }
    catch(e)
    {}
}

function ForceFieldSubmit(fieldName)
{
    Xrm.Page.getAttribute(fieldName).setSubmitMode("always");
}

function hideNavActivities() {
    Xrm.Page.ui.navigation.items.forEach(function (item, index) {

        var itemLabel = item.getLabel();

        if (itemLabel == "Activities" || itemLabel == "Closed Activities") {
            item.setVisible(false);
        }
    });
}


function SetLookupNull(lookupAttribute)
{
    var lookupObject = Xrm.Page.getAttribute(lookupAttribute);
    if (lookupObject != null)
    {
        Xrm.Page.getAttribute(lookupAttribute).setValue(null);
    }
}