web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error but still system allows SAVE

(0) ShareShare
ReportReport
Posted on by

Hi All,

My idea is the system can't save the record until amount greater minimum sales price.

One of member in this community suggest me to do by 2 ways.
 
1. Change your event to OnSave -> it mean that you remove the OnChange event at amount field. Add to the OnSave, when user click Save, will call the getminimumsalespriceByProductId method to validate data.
If it meet condition, you do nothing, system will save it automatically.
 
2. If you do not allow user to save data, you just call Xrm.Page.context.getEventArgs().preventDefault();
 
I have test it, the error comeout but the system still allow the record to be save.
My coding is:
 

if (this.status === 200) {
var result = JSON.parse(this.responseText).d;
var name = result.Name;
var productNumber = result.ProductNumber;
var miniumprice = result.np_minimumsalesprice.Value;
if (miniumprice > amount ) {
alert("This amount is not valid. It cannot be lesser than" + " " + miniumprice);
}
}
else {
alert(this.statusText);
}


 
Someone please help me :(

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    if (this.status === 200) {

    var result = JSON.parse(this.responseText).d;

    var name = result.Name;

    var productNumber = result.ProductNumber;

    var miniumprice = result.np_minimumsalesprice.Value;

    if (miniumprice > amount ) {

    alert("This amount is not valid. It cannot be lesser than" + " " + miniumprice);

    Xrm.Page.context.getEventArgs().preventDefault();

    }

    }

    else {

    alert(this.statusText);

    }

  • Suggested answer
    nghieppham Profile Picture
    4,755 on at

    Dear Noor,

    Please try this way 1 times.

    1. Disable Onchange event of amount field.

    2. Add this method to Onsave event.

    function getminimumsalespriceByProductId() {
    debugger;

    var product = Xrm.Page.getAttribute("productid").getValue();
    var productId = null;
    var amount = null;
    var miniumprice = null;
    var productId = null;
    if (product) {
    productId = product[0].id;
    }

    amount = Xrm.Page.getAttribute("amount").getValue();

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ProductSet(guid'" + productId + "')?$select=Name,new_MinimumSalesPrice,np_minimumsalesprice", false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    this.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.responseText).d;
    var name = result.Name;
    var productNumber = result.ProductNumber;
    miniumprice = result.np_minimumsalesprice;
    if (amount < miniumprice.Value) {
    Xrm.Page.ui.setFormNotification('This amount is not valid. It cannot be lesser than' + amount,'WARNING','2');
    Xrm.Page.context.getEventArgs().preventDefault();

    }

    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    }

    I recommend that you should use FormNotification, it will be more friendly to user.

    Regards,

    Please tick Verify if it is right for You.

  • Community Member Profile Picture
    on at

    Hi Pham Hong Nghiep,

    Thank you for your respond :)

    It show the warning above but in Product form, it actually save. Because in grid, the amount already there.

    It is normal actually?

  • Suggested answer
    nghieppham Profile Picture
    4,755 on at

    Hi Noor, 

    Please follow these steps to fix this bug: 

    1. Add 1 parameter to your method as below: 

    function getminimumsalespriceByProductId(context) {
    debugger;

    var productID = Xrm.Page.getAttribute("productid").getValue();
    var priceId = Xrm.Page.data.entity.getId();
    var amount = null;
    var miniumprice = null;
    var amount = null;
    var productId = null;
    if (product) {
    productId = product[0].id;
    }
    amount = Xrm.Page.getAttribute("amount").getValue();
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/ProductSet(guid'" + productID + "')?$select=Name,ProductNumber,new_MinimumSalesPrice,np_minimumsalesprice", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    this.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.responseText).d;
    var name = result.Name;
    var productNumber = result.ProductNumber;
    miniumprice = result.np_minimumsalesprice;
    if (amount < miniumprice.Value) {
    Xrm.Page.ui.setFormNotification('This amount is not valid. It cannot be lesser than' + amount, 'ERROR', '2');
    //Xrm.Page.context.getEventArgs().preventDefault();
    context.getEventArgs().preventDefault();
    }

    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    }

    Open your form under customize mode, then tick on "Pass execution as first parameter". Then it will prevent save and will not add to your subgrid. 

    https://xrmcenter.wordpress.com/2016/03/28/prevent-save-in-crm-form/ 

  • nghieppham Profile Picture
    4,755 on at

    Hi Noor,

    DId you resolve this issue?

  • Community Member Profile Picture
    on at

    Hi,

    Sorry for late reply, I have dinner just now. By the way, I already apply it but still same.

    It show the error, but when I click on Save button. It will save the amount.

    88822.Capture.PNG

  • Verified answer
    nghieppham Profile Picture
    4,755 on at

    Hi Noor, please follow the guide here xrmcenter.wordpress.com/.../prevent-save-in-crm-form

    you have to add the context as first parameter.

    1. Add 1 parameter to your method as below:

    function getminimumsalespriceByProductId(context) {

    debugger;

    var productID = Xrm.Page.getAttribute("productid").getValue();

    var priceId = Xrm.Page.data.entity.getId();

    var amount = null;

    var miniumprice = null;

    var amount = null;

    var productId = null;

    if (product) {

    productId = product[0].id;

    }

    amount = Xrm.Page.getAttribute("amount").getValue();

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/ProductSet(guid'" + productID + "')?$select=Name,ProductNumber,new_MinimumSalesPrice,np_minimumsalesprice", true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    this.onreadystatechange = null;

    if (this.status === 200) {

    var result = JSON.parse(this.responseText).d;

    var name = result.Name;

    var productNumber = result.ProductNumber;

    miniumprice = result.np_minimumsalesprice;

    if (amount < miniumprice.Value) {

    Xrm.Page.ui.setFormNotification('This amount is not valid. It cannot be lesser than' + amount, 'ERROR', '2');

    //Xrm.Page.context.getEventArgs().preventDefault();

    context.getEventArgs().preventDefault();

    }

    }

    else {

    alert(this.statusText);

    }

    }

    };

    req.send();

    }

    Open your form under customize mode, then tick on "Pass execution as first parameter". Then it will prevent save and will not add to your subgrid.

    xrmcenter.wordpress.com/.../prevent-save-in-crm-form

  • Community Member Profile Picture
    on at

    Hi,

    I already tick on "Pass execution as first parameter". When I test it show the error, but when I click on Save button. It will save the amount at Product form.

    Need I do at form OnSave or I can do at Amount field Onchange?

    function getminimumsalespriceByProductId(context) {

       debugger;

       var product = Xrm.Page.getAttribute("productid").getValue();

       var priceId = Xrm.Page.data.entity.getId();

       var amount = null;

       var miniumprice = null;

       var amount = null;

       var productId = null;

       if (product) {

           productId = product[0].id;

       }

       amount = Xrm.Page.getAttribute("amount").getValue();

       var req = new XMLHttpRequest();

       req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ProductSet(guid'" + productId + "')?$select=Name,ProductNumber,new_MinimumSalesPrice,np_minimumsalesprice", true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.onreadystatechange = function () {

           if (this.readyState === 4) {

               this.onreadystatechange = null;

               if (this.status === 200) {

                   var result = JSON.parse(this.responseText).d;

                   var name = result.Name;

                   var productNumber = result.ProductNumber;

                   miniumprice = result.np_minimumsalesprice;

                   if (amount < miniumprice.Value) {

                       Xrm.Page.ui.setFormNotification('This amount is not valid. It cannot be lesser than RM ' + miniumprice.Value, 'ERROR', '2');

                       context.getEventArgs().preventDefault();

                   }

               }

               else {

                   alert(this.statusText);

               }

           }

       };

       req.send();

    }

  • nghieppham Profile Picture
    4,755 on at

    Hi Noor,

    Please try it in OnSave.

  • Community Member Profile Picture
    on at

    Hi,

    I try it in OnSave, but still same.

    Actually, it can do as my idea or can't?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans