I am trying to associate a view which consists records from a N:N relationship(Project and Project Members) on a sub grid on case entity. The fetchxml is fine as it yields results(tested in xrm toolbox fetchxml tester). Now when code is run i do not get any exception but i get an error at the end( i'm attaching the snapshot) and working code below. please have a look at it. Before that i have attached snapshot of properties sub grid.
function formOnLoad() {
var isGridLoaded = null;
var control = Xrm.Page.getControl("CaseAssignedTo");
try {
if (control == null) //(isGridLoaded = objSubGrid.control.getEntityName() == null)
{
throw "exception";
}
else {
Xrm.Page.data.process.addOnStageChange(skipStage);
alert("hi ");
}
}
catch (err) {
setTimeout(2000, Xrm.Page.data.process.addOnStageChange(skipStage));
return;
}
}
function skipStage(args) {
try {
var control = Xrm.Page.getControl("CaseAssignedTo");
var controlType = control.getControlType();
var controlName = control.getName();
var objSubGrid = window.parent.document.getElementById("CaseAssignedTo");
if (control != null) {
filterAddExistingContact();
if (Xrm.Page.getControl("header_process_resolveby")) {
Xrm.Page.data.save();
alert("saved");
}
}
}
catch (args) {
alert("exception" + args.message);
}
}
//filters an add existing lookup view (N:N)
function addExistingFromSubGridCustom(gridTypeCode, gridControl, context, fetch, layout, viewName) {
var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; // a dummy view ID
var relName = gridControl.control.GetParameter("relName");
var roleOrd = gridControl.control.GetParameter("roleOrd");
//creates the custom view object
var customView =
{
fetchXml: fetch,
id: viewId,
layoutXml: layout,
name: viewName,
recordType: gridTypeCode,
Type: 0
};
try {
var parent = GetParentObject(null, 0);
var parameters = [gridTypeCode, "", relName, roleOrd, parent];
var callbackRef = Mscrm.Utilities.createCallbackFunctionObject("locAssocObjAction", context, parameters, false);
//pops the lookup window with our view injected
LookupObjectsWithCallback(callbackRef, null, "multi", gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
}
catch (args) {
alert(args.message);
}
}
function filterAddExistingContact()
{
var entityName = "new_projectmembers";
var parentAccount = Xrm.Page.getAttribute("customerid").getValue();
if (parentAccount != null) {
var parentAccountid = parentAccount[0].id;
var parentAccountname = parentAccount[0].name;
// give the custom view a name
var viewDisplayName = "Associated contacts for " + parentAccountname + "";
//fetch to retrieve filtered data
var fetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' >" +
"<entity name='new_projectmembers'>" +
"<attribute name='new_projectmembersid'/>" +
"<attribute name='new_name' />" +
"<attribute name='createdon' />" +
"<order attribute='new_name' descending='false' />" +
"<link-entity name='new_projectmembers_project' from='new_projectmembersid' to='new_projectmembersid' visible='false' intersect='true' >" +
"<link-entity name='account' from='accountid' to='accountid' alias='ab' >" +
"<filter type='and'>" +
"<condition attribute='name' operator='eq' value='" + parentAccountname + "' />" +
"</filter>" +
"</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
//columns to display in the custom view (make sure to include these in the fetch query)
var layout = "<grid name='resultset' object='112' jump='new_name' select='1' icon='1' preview='1'>" +
"<row name='result' id='new_projectmembersid'>" +
"<cell name='new_name' width='200' />" +
"</row>" +
"</grid>";
var control = Xrm.Page.getControl("CaseAssignedTo");
var controlType = control.getControlType();
var controlName = control.getName();
var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}";
var gridcontrol = document.getElementById("CaseAssignedTo");
gridcontrol.control.Refresh();
addExistingFromSubGridCustom(controlType, gridcontrol, this, fetch, layout, viewDisplayName);
}
}

