Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I've created a JavaScript OnSave event that parses through a "Sales Quota Distribution" entity grid within an "Opportunity" entity form, and checks for duplicates in the "Resource" field of the "Sales Quota Distribution" grid. When there is a duplicate, a warning message will appear. This is working, but I'd like to be able to append the OnSave event to not allow the user to save the form if there are no duplicate resources. How can I do this?

Below is my current code:

function GetTotalResourceCount(executionContext) {
    console.log("function started");
    var execContext = executionContext;
    var formContext = executionContext.getFormContext();
    var resourceyescount = 0;
    try {
        var gridCtx = formContext._gridControl;
        var grid = gridCtx.getGrid();
        var allRows = grid.getRows();
        //get rows - use the getControl method and pass the grid name.
        //var gridContext = formContext.getControl("s_qd");
        //        if (formContext.getGrid().getTotalRecordCount() == 0) {
        //           setTimeout(function () { GetTotalResourceCount(execContext); }, 2000);
        //           return;
        //       }
        var duplicatesFound = 0;
        //loop through rows and get the attribute collection
        allRows.forEach(function (row, rowIndex) {
            var thisRow = row.getData().entity;
            var thisRowId = thisRow.getId();
var thisResource = "";
var thisResourceName = "";
var thisResourceID = "";
            console.log("this row id=" + thisRowId);
            var thisAttributeColl = row.getData().entity.attributes;
            thisAttributeColl.forEach(function (thisAttribute, attrIndex) {
                var msg = "";
                if (thisAttribute.getName() == "new_resource") {
                    thisResource = thisAttribute.getValue();
thisResourceID = thisResource[0].id;
thisResourceName = thisResource[0].name;
                }
            });

            // Loop through every row and find one with
            // thatresource == thisResource &&
            // thatrow ID != thisRowId
            var allRows2 = formContext.getGrid().getRows();
            //loop through rows and get the attribute collection
            allRows2.forEach(function (row, rowIndex) {
                var thatRow = row.getData().entity;
                var thatRowId = thatRow.getId();
                var thatAttributeColl = row.getData().entity.attributes;
var thatResource = "";
var thatResourceName = "";
var thatResourceID = "";
                thatAttributeColl.forEach(function (thatAttribute, attrIndex) {
                    if (thatAttribute.getName() == "new_resource") {
                        thatResource = thatAttribute.getValue();
thatResourceID = thatResource[0].id;
thatResourceName = thatResource[0].name;
                        if (thatResourceID == thisResourceID && thatRowId != thisRowId) {
                            duplicatesFound++;
                            var msg = "Duplicate resource " + thatResource;
                        }
                    }
                });
            });
        });

        if (duplicatesFound > 0) {
            console.log("duplicate found");
            Xrm.Utility.alertDialog("WARNING: There are duplicate resources in the Sales Quota Distribution grid.");
        }
    } catch (err) {
        console.log('Error occurred :' + err)
    }
}




Any help would be greatly appreciated. Thanks!
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

    Miguel, I've just tried this and received the following error:

    ScriptError3_2D00_26.PNG

  • RE: D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

    Hi,

    before looking at any logic, can you comment out everything and try with only 

    executionContext.getEventArgs().preventDefault();

    just for testing purposes, to see if this is working

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

    Hi Miguel,

    Thank you.  I added this to my code but it did not work. The code still ran as it did before, but I was still able to save.

    Here is my code with the new step added in the "if (duplicatesFound > 0)" if statement:

    function GetTotalResourceCount(executionContext) {

       console.log("function started");

       var execContext = executionContext;

       var formContext = executionContext.getFormContext();

       var resourceyescount = 0;

       try {

           var gridCtx = formContext._gridControl;

           var grid = gridCtx.getGrid();

           var allRows = grid.getRows();

           //get rows - use the getControl method and pass the grid name.

           //var gridContext = formContext.getControl("s_qd");

           //        if (formContext.getGrid().getTotalRecordCount() == 0) {

           //           setTimeout(function () { GetTotalResourceCount(execContext); }, 2000);

           //           return;

           //       }

           var duplicatesFound = 0;

           //loop through rows and get the attribute collection

           allRows.forEach(function (row, rowIndex) {

               var thisRow = row.getData().entity;

               var thisRowId = thisRow.getId();

    var thisResource = "";

    var thisResourceName = "";

    var thisResourceID = "";

               console.log("this row id=" + thisRowId);

               var thisAttributeColl = row.getData().entity.attributes;

               thisAttributeColl.forEach(function (thisAttribute, attrIndex) {

                   var msg = "";

                   if (thisAttribute.getName() == "new_resource") {

                       thisResource = thisAttribute.getValue();

    thisResourceID = thisResource[0].id;

    thisResourceName = thisResource[0].name;

                   }

               });

               // Loop through every row and find one with

               // thatresource == thisResource &&

               // thatrow ID != thisRowId

               var allRows2 = formContext.getGrid().getRows();

               //loop through rows and get the attribute collection

               allRows2.forEach(function (row, rowIndex) {

                   var thatRow = row.getData().entity;

                   var thatRowId = thatRow.getId();

                   var thatAttributeColl = row.getData().entity.attributes;

    var thatResource = "";

    var thatResourceName = "";

    var thatResourceID = "";

                   thatAttributeColl.forEach(function (thatAttribute, attrIndex) {

                       if (thatAttribute.getName() == "new_resource") {

                           thatResource = thatAttribute.getValue();

    thatResourceID = thatResource[0].id;

    thatResourceName = thatResource[0].name;

                           if (thatResourceID == thisResourceID && thatRowId != thisRowId) {

                               duplicatesFound++;

                               var msg = "Duplicate resource " + thatResource;

                           }

                       }

                   });

               });

           });

           if (duplicatesFound > 0) {

               console.log("duplicate found");

               Xrm.Utility.alertDialog("WARNING: There are duplicate resources in the Sales Quota Distribution grid.");

               executionContext.getEventArgs().preventDefault();

           }

       } catch (err) {

           console.log('Error occurred :' + err)

       }

    }

    Any ideas? Am I doing something incorrectly?

    Thanks!

  • Suggested answer
    RE: D365/JavaScript issue: Need help appending an OnSave event to not allow a user to save a form

    Hi,

    Please try with:

    executionContext.getEventArgs().preventDefault();

    You can check the documentation on https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/manage-auto-save

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,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans