Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Using Mscrm.GridCommandActions.deleteRecords - Custom Ribbon Delete Dialog on sub grid - Retrieving to see if data exist.

(0) ShareShare
ReportReport
Posted on by 1,530

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.  

Document is already attached.

What is causing the below error message.

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, "");

    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); } }); }

*This post is locked for comments

  • Verified answer
    rthompson Profile Picture
    rthompson 1,530 on at
    RE: Custom Ribbon Delete Dialog on sub grid - Retrieving to see if data exist. Click Cancel then retrieve again get error message

    Okay.  I figure out my issue using the Mscrm.GridCommandActions.deleteRecords.

    Thought that I had fixed my issue from this posting.

    https://community.dynamics.com/crm/f/117/t/306224

    But come to find out that the logic was only working some of the time.   

    What I found out is that when setting the Mscrm.GridCommandActions.deleteRecords to a variable to keep the original Mscrm.GridCommandActions.deleteRecords.  This type of logic would not work all the time.

    Example:

            //to store the original function ones
            originalFunctionDeleteRecordSubgrid = Mscrm.GridCommandActions.deleteRecords;
    
            //add new standard subgrid
            Mscrm.GridCommandActions.deleteRecords = function (selectedGridControl, selectedEntityReferences, entityTypeCode) {
                getCheckifDataExist(caseId, selectRowRecordId);
            }

    When using the above logic the Ribbon Mscrm.GridCommandActions.deleteRecords would always remember my last function that I assign to the Mscrm.GridCommandActions.deleteRecords

            Mscrm.GridCommandActions.deleteRecords = function (selectedGridControl, selectedEntityReferences, entityTypeCode) {
                getCheckifDataExist(caseId, selectRowRecordId);
            }


    I could not figure out why this was happening.   I spent many hours trying to resolve this issue.  I could not find any examples that could help me fix this issue.

    So what I found out was in order to fix my issue I had to do the following.

    
            if (typeof Mscrm.GridCommandActions.deleteRecords_original == 'undefined') {
                Mscrm.GridCommandActions.deleteRecords_original = Mscrm.GridCommandActions.deleteRecords;
            }


    Here's my final working code:

    var subGridName = null;
    var selectedGridControl = null;
    var selectedEntityReferences = [];
    
    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];
    
        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, "");
    
        try {
            //Save the original Mscrm
            if (typeof Mscrm.GridCommandActions.deleteRecords_original == 'undefined') {
                Mscrm.GridCommandActions.deleteRecords_original = Mscrm.GridCommandActions.deleteRecords;
            }
    
            //Call the overwrite Mscrm
            Mscrm.GridCommandActions.deleteRecords = function (selectedControl, selectedControlSelectedItemReferences, selectedEntityTypeCode) {
                getCheckifDataExist(caseId, selectRowRecordId, selectedControl, selectedControlSelectedItemReferences, selectedEntityTypeCode);
            }
        }
        catch (e) {
    
        }
    }
    
    
    function getCheckifDataExist(caseId, selectRowRecordId, selectedControl, selectedControlSelectedItemReferences, selectedEntityTypeCode) {
        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;
    
        $.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;
    //Do not allow delete the row
                   if (results.length > 0) {
                    alert("This record has already been assigned to a case");
                    return false;
                } else {
    //Display the Delete Dialog
                    Mscrm.GridCommandActions.deleteRecords_original(selectedControl, selectedControlSelectedItemReferences, selectedEntityTypeCode);
                    return true;
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
            }
        });
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,403 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans