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
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.
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);
Hi Necdet,
Thanks for suggestion. It worked. :)
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!
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
Hi Davek
Great to hear that. Can u please share the snippet?
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.
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
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.
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.
André Arnaud de Cal...
291,965
Super User 2025 Season 1
Martin Dráb
230,836
Most Valuable Professional
nmaenpaa
101,156