Hi,
On my ribbon I enable a rule on the Delete button. The delete button will check to see if data exist. The logic is working. My problem is when the Delete Dialog appears and I click cancel and then I click on the same delete option again I get the following error message. What is causing the below error message.
Document is already attached.
var subGridName = null;
var selectedGridControl = null;
var subGridEntityName = null;
var entityTypeCode = null;
var selectedEntityReferences = [];
var originalFunctionDeleteRecordSubgrid = null;
function ribbonSubgridDelete(entity_subGrid) {
var caseId = Xrm.Page.data.entity.getId().replace(/{/g, "").replace(/}/g, "");
var entitysubgridtype = entity_subGrid.split('-');
subGridEntityName = entitysubgridtype[0];
subGridName = entitysubgridtype[1];
selectedGridControl = subGridName;
var selectedRows = Xrm.Page.getControl(subGridName).getGrid().getSelectedRows();
selectedEntityReferences = [];
selectedRows.forEach(function (selectedRow, i) {
selectedEntityReferences.push(selectedRow.getData().getEntity().getEntityReference());
});
var selectRowRecordId = selectedEntityReferences[0].id.replace(/{/g, "").replace(/}/g, "");
entityTypeCode = "1222";
try {
//to store the original function ones
originalFunctionDeleteRecordSubgrid = Mscrm.GridCommandActions.deleteRecords;
//add new standard subgrid
Mscrm.GridCommandActions.deleteRecords = function (selectedGridControl, selectedEntityReferences, entityTypeCode) {
getCheckifDataExist(caseId, selectRowRecordId);
}
}
catch (e) {
}
}
function getCheckifDataExist(caseId, selectRowRecordId) {
var iselect = "$select=new_name";
var ifilter = "&$filter=(new_selectId/Id eq guid'" + selectRowRecordId + "' and new_CaseId/Id eq guid'" + caseId + "')";
var options = iselect + ifilter;
//Same error message using this API
//SDK.REST.retrieveMultipleRecords(subGridEntityName, options, checkIfDataExistCallBack, checkIfDataExistError, checkIfDataExistComplete);
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_EntityName?$select=_new_caseid_value,_new_selectid_value,new_name&$filter=_new_caseid_value eq " + caseId + " and _new_selectid_value eq " + selectRowRecordId,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=1");
},
async: true,
success: function (data, textStatus, xhr) {
var results = data;
if (results.length > 0) {
alert("This record has already been assigned to a case");
return false;
} else {
originalFunctionDeleteRecordSubgrid(subGridName, selectedEntityReferences[0], entityTypeCode);
return true;
}
},
error: function (xhr, textStatus, errorThrown) {
Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
}
});
}






