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)

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

I have the same question (0)
  • Rawish Kumar Profile Picture
    13,758 on at

    Hi Robin,

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

    if(status == 1)

    //do something

  • Robin Card Profile Picture
    on at

    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

  • Ben Thompson Profile Picture
    6,350 on at

    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.

  • Suggested answer
    Shidin Haridas Profile Picture
    3,499 on at

    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.

  • Verified answer
    Ben Thompson Profile Picture
    6,350 on at

    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.

  • Shidin Haridas Profile Picture
    3,499 on at

    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.

  • Ben Thompson Profile Picture
    6,350 on at

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

  • Robin Card Profile Picture
    on at

    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

  • Verified answer
    Shidin Haridas Profile Picture
    3,499 on at

    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.

  • Ben Thompson Profile Picture
    6,350 on at

    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).

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