Skip to main content

Notifications

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

  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: How to trigger a javaScript on Opportunity close

    Hi Vladimir,

    statecode and statuscode both fields will not be a part of opportunity change event . The only possible way to trigger a javaScript is customizing the close button of opportunity if you want to run a javascript on opportunity close.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to trigger a javaScript on Opportunity close

    If you need it to be JavaScript, register for the change events of statecode and statuscode. When they have the values that are interesting for you, go ahead with the corresponding actions.

    function doSomething() {
    
      var stateCode = Xrm.Page.getAttribute("statecode").getValue();
    
      var statusCode = Xrm.Page.getAttribute("statuscode").getValue();
    
      if (stateCode == 1 && statusCode == 3) {
    
        // Do whatever is needed
    
      }
    
    }
    
    var stateCodeAttr = Xrm.Page.getAttribute("statecode");
    
    var statusCodeAttr = Xrm.Page.getAttribute("statuscode");
    stateCodeAttr .addOnChange(doSomething);
    
    statusCodeAttr.addOnChange(doSomething);
  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: How to trigger a javaScript on Opportunity close

    Hi Necdet,

    Thanks for suggestion. It worked. :)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to trigger a javaScript on Opportunity close

    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!

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to trigger a javaScript on Opportunity close

    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

  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: How to trigger a javaScript on Opportunity close

    Hi Davek

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to trigger a javaScript on Opportunity close

    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.

  • Verified answer
    necsa Profile Picture
    necsa 3,455 on at
    RE: How to trigger a javaScript on Opportunity close

    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

  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: How to trigger a javaScript on Opportunity close

    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.

  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: How to trigger a javaScript on Opportunity close

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans