Wednesday, March 28, 2012

CRM Filter Subgrid Control Based On Lookup Control

I Modify the code that exist in this blog and make it works based on Lookup Control

http://mscrmblogger.com/2011/02/23/crm2011-parent-child-grids-jscript/



function GridFilter(filterControl, sgrid, sfield) {
//var ids = Xrm.Page.getControl(filterControl)._control.get_innerControl().get_selectedIds();
//var ids = document.getElementById(filterControl).control.get_selectedIds();
debugger;
var id = "";
var ids = Xrm.Page.getAttribute(filterControl).getValue();
if (ids.length > 0) {
id = ids[0].id;
}

var updateFetchXml = function (fetchxml, field, id) {
var offset = 19;
var fi = fetchxml.indexOf('= 0) {
nfetchxml = fetchxml.substring(0, fi);
offset = fetchxml.indexOf('/>', fi) - fi + 2;
} else {
fi = fetchxml.indexOf('');
if (fi < 0) {
offset = 0;
fi = fetchxml.indexOf('', fi) + 2;
closings = '
';
}
nfetchxml = fetchxml.substring(0, fi);
nfetchxml += '';
}

nfetchxml += '';
nfetchxml += closings;
nfetchxml += fetchxml.substring(fi + offset);
return nfetchxml;
};

var updateXml = function (grid, xmlfield, subgridfield) {
try {
//var g = Xrm.Page.getControl(grid)._control.get_innerControl();
var g = document.getElementById(grid).control;
var a = g.getParameter(xmlfield);
var b = updateFetchXml(a, subgridfield, id);
g.setParameter(xmlfield, b);
} catch (e) { }
};

// Update the fetchXml
var cGrid = document.getElementById(sgrid).control;
var fetchXML = cGrid.getParameter('fetchXml');
if (fetchXML == null) {
fetchXML = cGrid.getParameter('effectiveFetchXml');
}
if (fetchXML == null) {
fetchXML = cGrid.getParameter('fetchXmlForFilters');
}
fetchXML = updateFetchXml(fetchXML, sfield, id);

cGrid.setParameter('fetchXml', fetchXML);
cGrid.setParameter('effectiveFetchXml', fetchXML);
cGrid.setParameter('fetchXmlForFilters', fetchXML);
// updateXml(sgrid, "fetchXml", sfield);

// // It does not use either of these for filtering, but...
// // I'm not sure where else these could be referenced
// updateXml(sgrid, "effectiveFetchXml", sfield);
// updateXml(sgrid, "fetchXmlForFilters", sfield);

// Refresh the grid
//Xrm.Page.getControl(sgrid).refresh();
document.getElementById(sgrid).control.refresh();

}

function registerSubGridUpdate(filterControl, sgrid, sfield) {
// debugger;
// var p = document.getElementById(filterControl);
var s = document.getElementById(sgrid);
if (s == null || s.readyState != "complete") {
// delay one second and try again. This is required due to grids becoming asynchronous

setTimeout('registerSubGridUpdate("' + filterControl + '","' + sgrid + '","' + sfield + '");', 1000);
return;
}

debugger;

// Enable FetchXML in the grid
// document.getElementById(sgrid + '_filterSet').control.RefreshWithFilters();

// Update to return nothing since it can't link to guid empty
// or click on the first data row, selecting it.

// The getControl for subgrids nolonger works in UR1.
// var r = Xrm.Page.getControl(filterControl)._control.get_nnerControl().get_allRecordIds();


GridFilter(filterControl, sgrid, sfield);
}

CRM JavaScript Calculated Field from equation

I face a big problem in CRM there is no calculated field based on other fields
the main problem alot of code to done

I make this method which allow me to make calculated field based on formula
this formula contains the names of the fields which this formula depending on it.
if the any fields that exist in the formula changes it will change the total field automatic.





var LoadedArray = new Array();

function CalculateField(fieldName, equation) {
// debugger;
var MainEquation = '';
MainEquation = equation;
var tempEquation = equation.CustomReplace("\*", ',').CustomReplace('/', ',').CustomReplace('+', ',').CustomReplace('-', ',');
tempEquation = tempEquation.CustomReplace('.', '').CustomReplace('0', '').CustomReplace('1', '').CustomReplace('2', '');
tempEquation = tempEquation.CustomReplace('3', '').CustomReplace('4', '').CustomReplace('5', '').CustomReplace('6', '');
tempEquation = tempEquation.CustomReplace('7', '').CustomReplace('8', '').CustomReplace('9', '');

var controls = tempEquation.split(',');
for (var i = 0; i < controls.length; i++) {
if (controls[i] == '' || controls[i] == null) continue;
var attribute = GetAttribute(controls[i]);
if (LoadedArray[fieldName] == undefined) {
var tempFunc = function () {
CalculateField(fieldName, equation);
};
//attribute.addOnChange(tempFunc);
Xrm.Page.data.entity.attributes.get(controls[i]).addOnChange(tempFunc);

}
var value = GetValueAttribute(controls[i]);
if (value == undefined) value = 0;
MainEquation = MainEquation.replace(controls[i], value);


}
SetValueAttribute(fieldName, eval(MainEquation));
ChangeFieldBackGround(fieldName, 'yellow');
SetEnabledState(fieldName, true);
if (LoadedArray[fieldName] == undefined) {
var tempDisableFunc = function () {
SetEnabledState(fieldName, false);
}
Xrm.Page.data.entity.addOnSave(tempDisableFunc);
}
LoadedArray[fieldName] = 1;

}



function SetEnabledState(controlName, value) {
var AddressType = Xrm.Page.ui.controls.get(controlName);
AddressType.setDisabled(value);
}

function ChangeFieldBackGround(fieldname, color) {

//crmForm.all[fieldname].style.backgroundColor = color;
var controles = document.getElementById(fieldname).parentNode.parentNode.getElementsByTagName('input');
for (var i = 0; i < controles.length; i++) {
controles[i].style.backgroundColor = color;
}
}


An Example for calling this Method

CalculateField('new_totalamount','0.5*new_price*new_quantity');