Opp Plugin Won't Fire: PLEASE HELP
-
Monday, December 24, 2012 5:17 PM
I am trying to write a plugin that will automatically close all related quotes that are either in the active or draft state when the related Opportunity record is closed as lost. I have tried to register the plugin under different messages, setstate, setstatedynamicentity, win, lose, update but plugin still won't fire. I have gon as far as commenting out all the code except for a line where I am throwing an exception just to see if the code fire but to no avail. So as this moment I don't even know if the code I have will do what I need but there is no way to testing if I cannot eve get the plugin to fire. PLEASE HELP. Below if part of the sample code. Thank you in advance.
publicclassCloseProposals: IPlugin
publicvoidExecute(IServiceProviderserviceProvider)
{
thrownewInvalidPluginExecutionException("HERE");
//try
//{
// IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
// {
// message = String.Format("Obtaining the opportunity");
// Entity opportunityEnity = (Entity)context.InputParameters["Target"]
// if (opportunityEnity.LogicalName == "opportunity")
// {
// message = String.Format("Setting up the service");
// IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// message = String.Format("Completed setting up service");
// try
// {
// //Get opportunity id first
// message = String.Format("Obtain Opp Id");
// //Get the opportunityid from the current opportunity
// EntityReference opportunityId = (EntityReference)opportunityEnity.Attributes["opporunityid"];
// message = String.Format("OppId = " + opportunityId);
// //use the service to get all proposals related to the opportunity
// using (var crm = new ValerusServiceContext(service))
// {
// message = String.Format("Obtaining the list of proposals for this opp that need to be deactivatde (either draft or active)");
// //Create a list of all the proposals/quotes that are either drafts or active
// var proposals = crm.QuoteSet.Where(q => q.OpportunityId == opportunityId && (q.StatusCode.Value == 0 || q.StatusCode.Value == 1)).ToList();
// //Show a message if proposal list is empty otherwise list is not loaded
// if (proposals.Count <= 0)
// {
// throw new InvalidPluginExecutionException(String.Format("Proposal list is empty"));
// }
// else
// {
// message = String.Format("Proposal list loaded");
// }
// //Loop through those proposals/quotes and set their status to inactive
// foreach (Quote quote in proposals)
// {
// message = String.Format("Inside proposals loop");
// SetStateRequest setStateRequest = new SetStateRequest();
// setStateRequest.EntityMoniker = new EntityReference(quote.LogicalName, quote.Id);
// setStateRequest.State = new OptionSetValue(3);// setStateRequest.Status = new OptionSetValue(5);
// SetStateResponse response = (SetStateResponse)service.Execute(setStateRequest);
// //var response = (SetStateResponse)service.Execute(setStateRequest);
// message = String.Format("quote status changed for quote" + quote.QuoteId);
// message = String.Format(" " + response);
// }
// //Do I need Savechanges if there is service.Execute?
// crm.SaveChanges();
// }
// //}
// }
// catch (Exception ex)
// {
// tracingService.Trace("OpportunityPlugin: {0}", ex.ToString());
// throw new InvalidPluginExecutionException("An error occurred in the Opportunity plug-in.", ex.InnerException);
// }
// }
// }
//}
//catch (FaultException<OrganizationServiceFault> ex)
//{
// throw new InvalidPluginExecutionException(message, ex);
//}
}
All Replies
-
Monday, December 24, 2012 8:22 PM
Hi
I have faced similar situation when working in crm 4.0.......The catch here is that...... You need to register the plugin on create of opportunity close entity. Then check for the state code to determine the won or lose of opp and then do whatever you want.
Please mark as answer if it answers your query.
RegardsManu- Proposed As Answer by Manu_Sharma Monday, December 24, 2012 8:22 PM
-
Wednesday, December 26, 2012 5:08 PM
Thank you very much Manu_Sharma for your recommended solution. I did try what you suggested but unfortunately I cannot seem to get the plugin to fire. Below is a screen shot of how registered the step for the plugin. When I click on the "Close as Lost" button on the ribbon plugin doesnt fire and I still get the message that there are still active or draft quotes associated with the opportunity. Any other ideas could help. Anybody. Thank you.
-
Wednesday, December 26, 2012 10:57 PMAnswerer
Hi,
The check for active or draft quotes is done before the plugin pipeline is executed, so this is probably why your plugin is not firing. I also note you have registered the step on the child pipeline - this isn't needed for CRM2011.
Scott
Scott Durow
Read my blog: www.develop1.net/public
Follow Me on Twitter
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" -
Thursday, December 27, 2012 4:57 PM
Thanks Scott. This one of my very first plugins that I have written and do not completely understand the whole pipeline execution yet. I have done a couple of create and update plugins with no problems in terms of registering the events but for some reason this one has been a major headache. When you say this is done before plugin pipeline execution, what do you mean exactly? If you mean that I should have registered the step on the Pre-Validation stage of execution, I have tried that as well with no luck. Thanks
-
Thursday, December 27, 2012 5:25 PM
Hi,
We had similar kind of requirement wherein we did hide the OOB win/lost buttons and kept custom buttons. Onclick of the custom buttons, we did certain validations at the opportunity and quote level and closed all the draft/active quotes. Then closed opportunity as Won or Lost.Vikranth http://howto-mscrm.blogspot.com "Please Mark it as answer if it helps in resolving your query"
- Proposed As Answer by vicky_smag (Vikranth Pandiri) Thursday, December 27, 2012 5:25 PM
-
Thursday, December 27, 2012 5:47 PMAnswerer
Hi,
Unfortunately, the pre-validation will also not fire until all quotes are dealt with. The reason being is that the check for quotes is done by the ribbon button before the server plugin pipeline is started. The only way to change this is to create a custom javascript action that bypasses the standard close dialog - this is quite an advanced subject and I would recommend against it. What are you trying to do exactly?
Scott
Scott Durow
Read my blog: www.develop1.net/public
Follow Me on Twitter
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" -
Thursday, December 27, 2012 5:56 PMThanks Vicky that sounds like a better solution than what I have been trying to do here. After you replaced the hidden win/lost button with custom buttons, did you have to write custom workflows or plugins to do the close of all draft/active quotes? Do you have any sample code on how you achived this? Did the ribbon work bench to hide the win/lsot buttons or did you go directly with the xml. Thanks
-
Friday, December 28, 2012 8:46 AM
Hi,
You can trigger the plugin on update of the hidden field to close draft/active quotes. This we have done for a CRM 4.0 org. I don't have that code handy at this time. You can still do it using CloseQuoteRequest with appropriate state for the quotes. Once all quotes gets closed, you can use WinOpportunityRequest/LoseOpportuntiyRequest to close your opportunity. Actual Revenue and Actual Close Date values will be populated once Opportunity is closed. Let me know if you require any more info.Vikranth http://howto-mscrm.blogspot.com "Please Mark it as answer if it helps in resolving your query"
- Marked As Answer by valCrmDev2 Monday, January 07, 2013 9:53 PM
-
Friday, December 28, 2012 4:04 PM
Scott, what we are trying to do is find a way to close all quotes that are in either the active or draft state that are related to any opportunity that we decide to close as lost from the ribbon button. So instead of someone manually going to change the status of each of the quotes that are either in draft or active state, for that opportunity, just fire off a plugin that will automatically close all those quotes so that we don't get that validation message that displays when you try to close an opportunity with related quotes in draft/active state. I hope that makes sense. I wrote a plugin that is supposed to fire off when the Close as Lost ribbon button is clicked. But it never fires no matter which evern I register it under because, which I just found out, there is some code that executes before my plugin code is even reachable which is why they never fire no matter which event message I select.
The only other route is the one to create a custom Close as Lost ribbon button. But as you said that seems more advance and I am not quite close to that level. This is my first plaugin thats really this complicated. I was wondering if someone had faced a similar situation and how they solved in. vicky_smag seems to have solved this before but I am not quite clear exaclty on how he achieved it. I created a custom ribbon button that pops up a modal dialog when clicked. Do I need to create my own dialog that is exactly like the OTB close lost dialog? And what else do I need to do get everything to work seamless as it does OTB. I am also worried that I may be missing some important functionality that come OTB by doing this workaround.
vicky_smag I am not completely sure I understand how to trigger plugin on update of the hidden field? Could you further expand on that please. Thanks
- Marked As Answer by valCrmDev2 Saturday, December 29, 2012 6:19 PM
- Unmarked As Answer by valCrmDev2 Wednesday, January 02, 2013 4:06 PM