I have a subgrid which is n:n relationship to current record.
I want to add a filtered view to the \"Add Existing\" button of this subgrid.
Any idea?
(I
First, you have to export a solution containing the entity with the type you want to filter:
In the customizations.xml find the RibbonDiffXml node and add the following code:
And in the the CommandDefinitions node, add this:
The code comes from an XML file that you can find in the CRM 2011 SDK and has been modified to call a custom function from a custom Javascript Library.
Then, create the new JS library with the name specified above in the Library attributes.
Add a first generic function:
/*****************************************/
/* */
/* Add Custom View To Subgrid */
/* */
/*****************************************/
function addExistingFromSubGridCustom(params) {
var relName = params.gridControl.getParameter("relName"),
roleOrd = params.gridControl.getParameter("roleOrd"),
viewId = "{00000000-0000-0000-0000-000000000001}"; // a dummy view ID
var customView = {
fetchXml: params.fetchXml,
id: viewId,
layoutXml: params.layoutXml,
name: params.name,
recordType: params.gridTypeCode,
Type: 0
};
var lookupItems = LookupObjects(null, "multi", params.gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
if (lookupItems && lookupItems.items.length > 0) {
AssociateObjects(crmFormSubmit.crmFormSubmitObjectType.value, crmFormSubmit.crmFormSubmitId.value, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName);
}
}
and finally, add the function which should be called by the button:
function addExistingCustomFilter(gridTypeCode, gridControl, primaryEntityName) {
// Here you can specify for which entity the filter should be applied.
// For example, filter only when you try to add an existing record to a client.
// In the other cases, you will call the default method.
if (primaryEntityName != "client" ) {
Mscrm.GridRibbonActions.addExistingFromSubGridStandard(gridTypeCode, gridControl);
return;
}
// Add some logic to retrieve information needed to filter your view if you want to
//Update the fetch that will be used by the grid.
var fetch = '' +
'' +
' ' +
'' +
' ' +
' ' +
' ' +
' ';
// Add conditions to your fetch xml dynamically
// Call the generic method with the rights arguments.
addExistingFromSubGridCustom({
gridTypeCode: gridTypeCode,
gridControl: gridControl,
fetchXml: fetch,
name: "My dynamyc custom filtered view!",
layoutXml: '' +
// Provide a layout xml ...
' '
});
}
Publish everything and it should be ok!