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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Answered

How to trigger a javaScript on Opportunity close

(0) ShareShare
ReportReport
Posted on by 460

Hi ,

i want to run a javascript when the Opportunity gets closed, but even though the opportunity form is getting disabled and status is changing the javascript is not getting triggered.

the code that i am using is :

function ABC(execObj) {
debugger;
try {
var status = execObj.getEventArgs();
if (status.getSaveMode()==5 || status.getSaveMode()==”5″) {
alert(“message”);

}
else if (saveMode == “6”) {

alert(“Opportunity ” + saveMode);
}

}
catch (ex) {
alert(“exception”);
}
}

Above mentioned function is not getting triggered on close of opportunity ..the function is registered on Opportunity form “OnSave”.

Any Suggestion ?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Hi Akanksha,

    I've doubted about calling JS function on close button of opportunity. But you can achieve this with this thread.

    community.dynamics.com/.../174483

    Hope it'll help you.

    Thanks

  • necsa Profile Picture
    3,455 on at

    Hi Akanksha,

    Please give me more detail. Javascript must trigger when opportunity closed? Or do you have different requerment?

    Did you try RibbonWorkbench?#

    thanks

    Happy CRM

  • Suggested answer
    Community Member Profile Picture
    on at

    Try: if (Xrm.Page.getAttribute("statecode").getValue() != 0) instead of if (status.getSaveMode()==5 || status.getSaveMode()==”5″).

    By default, CRM opportunities all have a status field called "Status" where 0 = Open, 1 = Won, 2 = Lost. Since both Won and Lost are closed, we can just say !=0.

  • Akanksha Ranjan Profile Picture
    460 on at

    HI Necdet Saritas,

    When we close an Opportunity, Opportunity Close dialog appears. In my requirement i want a popup of some message right after saving Opportunity Close dialog.

  • Akanksha Ranjan Profile Picture
    460 on at

    Hi DaveK,

    Problem that i am facing is : when Opportunity close dialog set save and Opportunity form becomes read-only the javascript that is registered onSave event is not getting triggered.

  • Verified answer
    necsa Profile Picture
    3,455 on at

    Hi Akanksha,

    Please try to trigger your javascript via RibbonWorkbench. You can custom closed button onclick event and put the javascript as action. I means when you click close button, button trigger Javascript. I hope it help you

    Thanks

    Happy CRM

  • Community Member Profile Picture
    on at

    Hi Akanksha,

    Are you familiar with plug-ins? This will handle your read-only situation since it is not client-side. I have a working plug-in that does exactly what you need but for Contacts. If you would like to see a snippet of the logic, let me know.

  • Akanksha Ranjan Profile Picture
    460 on at

    Hi Davek

    Great to hear that. Can u please share the snippet?

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    You cannot achieve this with the help of javascript. Because it will not pass the execution context to your function during opportunity close. The only option is you have to write plugins on opportunity close message.

    Please read below to get an idea.

    sumedha8.blogspot.qa/.../plugin-for-opportunity-win-lose.html

    Mansoor

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Akanksha, here is my snippet. The code below does the following: When a Contact becomes inactive, the inactive date is stamped. So, your solution is as simple as changing the code with values you want to use.

    protected void ExecutePostContactUpdate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }
    
        //Get the plugin context and connect to the CRM service
        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        //Create the service context
        var ServiceContext = new OrganizationServiceContext(service);
    
        ITracingService tracingService = localContext.TracingService;
    
        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity preImageEntity = (Entity)context.PreEntityImages["PreUpdateImage"];
            Entity postImageEntity = (Entity)context.PostEntityImages["PostUpdateImage"]; //register PostUpdateImage as a post-image
            int ACTIVE = 0; int INACTIVE = 1;
    
            try
            {
                if (preImageEntity.Attributes.Contains("statecode") && postImageEntity.Attributes.Contains("statecode"))
                {
                    OptionSetValue pre_statcode = (OptionSetValue)preImageEntity["statecode"];
                    OptionSetValue post_statcode = (OptionSetValue)postImageEntity["statecode"];
    
                    if (pre_statcode.Value == ACTIVE && post_statcode.Value == INACTIVE) //If the Contact is now Inactive
                    {
                        Entity tempContact = new Entity("contact");
                        tempContact.Id = postImageEntity.Id;
                        tempContact["new_inactivedate"] = DateTime.UtcNow;
                                
                        ServiceContext.ClearChanges();
                        ServiceContext.Attach(tempContact);
                        ServiceContext.UpdateObject(tempContact);
                        ServiceContext.SaveChanges();
                    }
    
                    else if (pre_statcode.Value == INACTIVE && post_statcode.Value == ACTIVE) //If the Contact is Re-activated
                    {
                        Entity tempContact = new Entity("contact");
                        tempContact.Id = postImageEntity.Id;
                        tempContact["new_inactivedate"] = null;
    
                        ServiceContext.ClearChanges();
                        ServiceContext.Attach(tempContact);
                        ServiceContext.UpdateObject(tempContact);
                        ServiceContext.SaveChanges();
                    }
                }
            }
    
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occured in the plug-in:", ex);
            }
            //end of code
        }
    }


    You want to use this code for post opportunity update pipeline. Good luck!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans