Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

validation on opportunity, but also allow the save operation

(0) ShareShare
ReportReport
Posted on by 790

dear all

i do have some flags on opportunity form related to child cases (belonging to the opportunity), if these cases get resolved, i update these flags back to opportunity.

i am doing this in order to prevent the user from advancing in the opportunity without resolving the related cases, but this also makes the opportunity refuses any save operation for the rest of the fields until the said cases resolved.

i have tried this validation using business rules, and one more time using JS , but same problem, if the validation condition occurs , then a warning message appears (which is fine) but also no save is allowed.

please check the following JS segment as well as the opportunity form image.

if ((case_1_added == "Yes") && (case_1_status != "Resolved")) {

Xrm.Page.ui.setFormNotification("You can't proceed until the related Document Review case is resolved. Then try again.", "ERROR", "2001"); // Set notification
context.getEventArgs().preventDefault();
return false;
}
else {
Xrm.Page.ui.clearFormNotification("2001"); // Clear notification
}

2018_2D00_11_2D00_13-13_5F00_51_5F00_51_2D00_Opportunity_5F00_-rest_2D00_04.png

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: validation on opportunity, but also allow the save operation

    Hi,

    In this case, I would personally go for workflow, trigger on process change ( select active stage), if the criteria is met, throw the meaningful error for user to see.

    At the same time, user still able to save the field value on form.

    The below example is using oob BPF.

    0207.Capture2.PNG

    0207.Capture2.PNG

    I believed V9.1 has some bugs with it, the error message will only display in log file. Need check with Microsoft.

    2364.Capture1.PNG

    survivingcrm.com/.../

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: validation on opportunity, but also allow the save operation

    You need to register "RegisterOnLoad" method on load. If you look in the method, it is proramatically attaching the event addOnStageChange and calling the other method.

  • Ahmad Saud Profile Picture
    Ahmad Saud 790 on at
    RE: validation on opportunity, but also allow the save operation

    Thanks Ravi

    but which event to use? i mean for the form, i have only OnLoad and OnSave.

    so what is the event  for "Change of Stage", can you please help?

    thanks,

  • Ahmad Saud Profile Picture
    Ahmad Saud 790 on at
    RE: validation on opportunity, but also allow the save operation

    Hi Alex

    i mean by not proceeding , is not to advance to the next stage.

    thanks 

     

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: validation on opportunity, but also allow the save operation

    Hi Ahmad,

    I think you can achieve this requirement with various different ways. I just tried it with JS and it worked as expected.

    Firstly, the reason why your script is not working is because you have register the method on save even and in your script you are preventing save. When you click on Next stage, it actualy change the stage and saves the record.

    You need to add the method on change of stage change i.e. JS should only work when you change the stage and not when you save the record. Also, on your method, you need to check your condition and then based on then use the code to move display notification and move to the previous stage. Below is the sample code ( I have used description field for testing purpose). You need to updated this as per your condition and register the method "RegisterOnLoad" on load of form.

    ============

    function RegisterOnLoad() {

       Xrm.Page.data.process.addOnStageChange(restrictBPFMovenext);

    }

    function restrictBPFMovenext() {  

       var description = Xrm.Page.getAttribute("description").getValue();

       if (description == null || description == "") {

           Xrm.Page.ui.setFormNotification("You can't proceed until the related Document Review case is resolved. Then try again.", "ERROR", "2001"); // Set notification        

           Xrm.Page.data.process.movePrevious();

       }

       else {

           Xrm.Page.ui.clearFormNotification("2001"); // Clear notification

       }

    }

    ===============

    The other way to achieve this is by triggering a workflow on stage change and then checking and cancelling the workflow. I haven't tested this but believes this should work. Other method is to have another field and make it required in the BPF stage. Refer this: community.dynamics.com/.../195814

    Hope this helps.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: validation on opportunity, but also allow the save operation

    Hi,

    i dont want the user to proceed if the case is not resolved. <-- Would be good if you can let us know, which action consider advance in the opportunity?

    Identify the action, put more checking.

  • Ahmad Saud Profile Picture
    Ahmad Saud 790 on at
    RE: validation on opportunity, but also allow the save operation

    dear all

    if i remove the PreventDefault(), then user will be able to advance in the opportunity without resolving the related document, i dont want the user to proceed if the case is not resolved.

    thanks,

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: validation on opportunity, but also allow the save operation

    Hi Ahmad,

    You could not save the data whether the opportunity is closed or not because you had used context.getEventArgs().preventDefault().

    This function is to prevent forms from saving data.

    Please refer to this link for more Microsoft description.

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/save-event-arguments/preventdefault

    You should use the code correctly according to your logic needs.

     

    Hope it helps.

    Best Regards

    Leo

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,615 on at
    RE: validation on opportunity, but also allow the save operation

    It’s because you are instructing to “preventDefault”, which means you are preventing save operation. Also “return false” will break the code execution.

    Comment those lines & try yourself to understand the code execution.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans