Reputation: 18895
I'm attempting to update the Add New button on a grid to open in the current window, rather than a new one. I've edited the ribbon XML, and I'm correctly getting this function called on click of the "+" icon:
export function createCase(selectedEntityTypeCode: number, parentEntityTypeCode: number, firstPrimaryItemId: string, primaryControl: string, selectedControl: string): void {
window.top.location.replace(CommonLib.getCreateEntityFromParentUrl(firstPrimaryItemId, parentEntityTypeCode, selectedEntityTypeCode));
}
The call to getCreateEntityFromParentUrl creates this string:
etc=112&extraqs=%3f_CreateFromId%3d%257b999BA23A-B07A-E611-80DD-FC15B4286CB8 %257d%26_CreateFromType%3d10010%26etc%3d112&newWindow=false&pagetype= entityrecord
Which opens a new Case form, with the correct Parent entity already populated, so I know it's reading from the CreateFromID and CreateFromType correctly.
If you don't actually create the Case, and click refresh in the browser, you're taken back to the parent entity (A Custom entity, "Location", in this case).
If you save create the Case, and then click refresh in the browser, you get this error:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #5B02AEE3Detail:
-2147220970 System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #5B02AEE3
2016-09-15T04:30:58.0199249Z -2147220969 allgnt_location With Id = 3e10a729-fd7a-e611-80dd-fc15b4286cb8 Does Not Exist 2016-09-15T04:30:58.0199249Z
You also get this error if you create a phone call from this entity, and click the Complete Call button in the Command bar.
The Id listed is an id for the Case, but apparently, CRM is trying to load it as the Location instead, which is obviously failing. Am I doing it wrong?
Upvotes: 2
Views: 374
Reputation: 18895
Thanks @Polshgiant for getting me started down the right track. I needed to call Xrm.Utility.openEntityForm. This Typescript function works for me!
/**
* Opens a create form for a child entity of a parent. Useful if a subgrid add new button should redirect to the new page, rather than the default open in a new window.
* @param parentEntityId Id of the parent entity
* @param parentEntityTypeCode Object Type Code of the parent Entity
* @param childLogicalName Child Logical Name
* @param parameters Object whos properties will be added to the extraQs parameters
*/
export function openCreateChildFormInCurrentWindow(parentEntityId: string, parentEntityTypeCode: number, childLogicalName: string, parameters?: any) {
const params = {
formid: null,
["_CreateFromId"]: parentEntityId,
["_CreateFromType"]: parentEntityTypeCode.toString()
} as Xrm.Utility.FormOpenParameters;
if (parameters) {
for (const param in parameters) {
if (parameters.hasOwnProperty(param)) {
params[param] = parameters[param];
}
}
}
Xrm.Utility.openEntityForm(childLogicalName, null, params, { openInNewWindow: false } as Xrm.Utility.WindowOptions);
}
Upvotes: 2