In CRM 2013 I have a javascript that adds a N:N relationship between Case and Account via a ribbon function. So on the click of this button, it will pop up a CRM lookup dialog and then users can add existing Accounts into a Case. When the relationship
is created, we have a plugin that will fire on the create of this relationship, and then create a new custom entity record 'CaseAccount'. Everything works fine, the relationship gets added and the record is added successfully.
On the Case form, we have a subgrid that shows all available CaseAccount under that Case. However after clicking the ribbon button to create the relationship, the new CaseAccount record doesn't immediately show up in the subgrid. Users must refresh the Case
page in order to show the records in the subgrid.
I tried to put a function to refresh the subgrid using Xrm.Page.data.refresh(). However I notice that the subgrid is refreshing when the CRM lookup dialog is firing. However after clicking 'OK' on the lookup window and the record is finally added, there
is no more refreshing. Therefore the new records don't show up on the subgrid?
Any suggestion on how to refresh subgrid completing a lookup dialog?
Below is the code that we have. Credit to Magnetism Solution for portion of the code.
function addLookupDialog(gridTypeCode) {
var relName = "new_case_account";
var roleOrd = 2;
var viewId = "{00000000-0000-0000-0000-000010001002}";
if (!IsNull(relName)) {
var customView = {
fetchXml: "(my fetchxml)",
id: viewId,
layoutXml: "(my fetchxml grid)",
name: "Filtered Lookup View",
recordType: gridTypeCode,
Type: 0
};
var parent = GetParentObject(null, 0);
var parameters = [gridTypeCode, "", relName, roleOrd, parent];
var callbackRef = Mscrm.Utilities.createCallbackFunctionObject("locAssocObjAction", this, parameters, false);
//pops the lookup window with our view injected
var lookupItems = LookupObjectsWithCallback(callbackRef, null, "multi", gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
}
if (lookupItems && lookupItems.items.length > 0) {
//beginning of rollup 12 must make modification
var parentId;
var parentTypeCode;
if (typeof (GetParentObject) == "function") { //post rollup 12 has its own function to get this
var parent = GetParentObject();
parentId = parent.id;
parentTypeCode = parent.objectTypeCode;
}
else { //pre rollup 12 still needs to use the old way
var parent = typeof (crmFormSubmit) == "undefined" ? $get("crmFormSubmit") : crmFormSubmit;
if (parent) {
parentId = parent.crmFormSubmitId.value;
parentTypeCode = parent.crmFormSubmitObjectType.value;
}
else {
parentId = window.parent.crmFormSubmit.crmFormSubmitId.value;
parentTypeCode = window.parent.crmFormSubmit.crmFormSubmitObjectType.value;
}
}//end of rollup 12 modification
AssociateObjects(parentTypeCode, parentId, gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName);
//refreshing subgrid, but not working yet
Xrm.Page.data.refresh().then(Xrm.Page.getControl("myGrid").refresh(), alert("Error Refreshing"));
//}
}
}