Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Distinguish between opportunity save and opportunity close in JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi,

I have written some JavaScript that triggers on an opportunity's OnSave event, in a nutshell it checks for open activities on the opportunity and cancels the save if there are none.

It's working great except for the fact that closing an opportunity as won or lost also triggers the OnSave event. Using GetSaveMode returns a value of 1, which is exactly the same as if a user had clicked the save button.

How can I determine that it was the opportunity close button that was clicked and not the save button?

Thanks,

Robin

*This post is locked for comments

  • Ben Thompson Profile Picture
    6,350 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Is there any value in getFormContext().data.entity.attributes.getByName("actualclosedate").getValue() as your javascript is triggered? (that code is definitely missing something - its stolen from the first piece of code I have to hand which is some minified r9 code).

  • Verified answer
    Shidin Haridas Profile Picture
    3,497 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Sorry if my explanation was not clear enough.

    I am not suggesting to override the OOB dialog box. This is what I am doing in my system:

    Scenario 1: Do not allow users to Close an Opportunity unless a PO has been created.

    On click of 'Close as Won', my custom script checks if the PO field has a value.

    0458.Capture2.PNG

    If no value is found, the user is prompted to create a PO.

    0458.Capture2.PNG

    Scenario 2: In case the PO field has a value, then call the internal functions which show the OOB close as win/lost dialogs

    Capture3_2300_.PNG

    Robin - In your case, instead of checking for PO field, your function will be doing a check of the open activities.

    Let me know if you need more details.

  • Robin Card Profile Picture
    on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Thanks, I've tried that and there's nothing I can see in the event source or the data being saved that can differentiate between the buttons, as you say it's a save followed by a status change

  • Ben Thompson Profile Picture
    6,350 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Those buttons open up the opportunity won and opportunity closed forms within a modal dialog box which are currently not customisable.

  • Shidin Haridas Profile Picture
    3,497 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Ben - Why wouldn't the customization of those buttons never work?

    What exactly were the customization that you were doing at that point?

    Agree on the risk of a later release changing all the internal function names.

  • Verified answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    The risk with 2 is that a hotfix will be released that reverts that code back to standard behaviour.

    Equally from the experiments I've seen conducted on customising those buttons I've never seen them work. The last time anyone suggested customising those buttons we ended up hiding them and creating custom ones that triggered custom business workflows that asked the appropriate why did we lose or how did we win questions.

  • Suggested answer
    Shidin Haridas Profile Picture
    3,497 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Hi Robin,

    How about doing the following?

    1. Assume that the function you call in the OnSave is 'checkForOpenActivities'.

    2. Modify the OOB 'Close as Won/Lost' button to call a custom javascript function, which then calls the checkForOpenActivities function.
    If open activities are not found and all is good, then call the OOB function (just launches the modal pop-up window) in this piece of code.

    So basically, what I am suggesting is that you do the required checks and depending on them, show the pop-up to the user.

  • Ben Thompson Profile Picture
    6,350 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    A closed opportunity is (from memory) a record save request followed by a status change request - I say from memory as it's perfectly possible (from 2016 onwards if I remember correctly) to perform both functions in a single save request by including the statuscode within the update.

    Hence the only way you could identify whether the save is a simple save request or a closure request would be to check what fields are being updated and if actualclosedate has a value in it then it likely to be a closure rather than a save.

  • Robin Card Profile Picture
    on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Hi,

    Thanks, but it appears the save event is triggered before the close event so the status is still at 0 when my script runs

  • Rawish Kumar Profile Picture
    13,758 on at
    RE: Distinguish between opportunity save and opportunity close in JavaScript

    Hi Robin,

    Can you put a condition like below to check the status as close/won :

    if(status == 1)

    //do something

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans