Answered by:
opportunity close pre event plugin.

Question
-
Hi All,
I want to be able to stop an opportunity from being closed as 'won' if the user does not have a specific role (e.g. only certain users can actually close an opportunity as 'won'). I have tried to create a plugin registered on the pre-stage, parent pipeline on the create of an opportunity close. it then checks if the users have the required roles and throws an error message if they dont (effectively stopping the opp from closing) or does nothing and lets it continue as normal. However it doesn't seem to be triggering at all. it doesnt even output simple debug message to a text file, which implies that it isn' t actually triggering the plugin :-(have i simply registered the plugin incorrectly?
any help is appreciated!
Thanks!
Tuesday, September 14, 2010 11:46 AM
Answers
-
I believe you have to register a pre-event plug-in on the Win message in the parent pipeline. The platform generates a separate Win message for a won opportunity.
Note that the only messages processed by a child pipeline are create, update, and delete.
Tech Writer
- Marked as answer by DavidBerryMVP, Moderator Saturday, November 6, 2010 12:46 AM
Tuesday, September 14, 2010 10:12 PM -
You can also prevent it using Javascript in OnSave event.
Use event.Mode = 5 to find the opportunity stage (Closing Stage)
Refer https://community.dynamics.com/blogs/hardworkdays/comments/64290.aspx
- Proposed as answer by VinothBalasubramanian Wednesday, September 15, 2010 9:42 AM
- Marked as answer by DavidBerryMVP, Moderator Saturday, November 6, 2010 12:47 AM
Wednesday, September 15, 2010 9:42 AM
All replies
-
This operation runs in child pipeline..so register the plug in child pipleline and try it..
MayankP My Blog My twitter- Proposed as answer by George DoubinskiMVP, Moderator Wednesday, September 15, 2010 1:51 AM
Tuesday, September 14, 2010 12:15 PM -
:-( it throws an unhandled exception error:
Thread information:
Thread ID: 4
Thread account name: xxxxx\sa-aar-crmtest01-crm
Is impersonating: False
Stack trace: at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values)
at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IPluginExecutionContext context)
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.Execute(String messageName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, PropertyBag fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.CrmServiceInternal.Execute(RequestBase request, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.InProcessCrmService.Execute(Object request)
at Microsoft.Crm.Application.Platform.ServiceCommands.PlatformCommand.ExecuteInternal()
at Microsoft.Crm.Application.Platform.ServiceCommands.WinOpportunityCommand.Execute()
at Microsoft.Crm.Web.SFA.OpportunityDetailPage.DoClose(Object sender, DataEventArgs e)
at Microsoft.Crm.Web.SFA.OpportunityDetailPage.Close(Object sender, DataEventArgs e)
at Microsoft.Crm.Application.Forms.DataEventProcessor.Raise(FormEventId eventId, FormState state, IUser user, String objectId, Entity entity)
at Microsoft.Crm.Application.Forms.AppForm.RaiseDataEvent(FormEventId eventId)
at Microsoft.Crm.Application.Forms.EndUserForm.Initialize(Entity entity)
at Microsoft.Crm.Application.Forms.CustomizableForm.Execute(Entity entity, String formType)
at Microsoft.Crm.Web.SFA.OpportunityDetailPage.ConfigureForm()
at Microsoft.Crm.Application.Controls.AppUIPage.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Custom event details:Tuesday, September 14, 2010 12:27 PM -
this is my code...well the start of it anyway:
'*********************************************************************************************************
' Close Opportunity only if they are a 'financial controller'
'*********************************************************************************************************
Public Class StopOpportunityClose
Implements IPlugin
Public service As CrmService
Public f As New Common
Dim item As DynamicEntity
Dim itemid As String
Public Sub Execute(ByVal context As IPluginExecutionContext) Implements IPlugin.Execute
If context.MessageName = "Create" Then
'Dim temp As New ConvertEmailToCase
service = CType(context.CreateCrmService(True), ICrmService)Tuesday, September 14, 2010 12:28 PM -
you will not able to do following operation in child pipeline..
context.CreateCrmService(True)
you can need use method describe in following article to get CRM Service..
http://msdn.microsoft.com/en-us/library/cc151083.aspx
MayankP My Blog My twitter- Proposed as answer by George DoubinskiMVP, Moderator Wednesday, September 15, 2010 1:51 AM
Tuesday, September 14, 2010 12:36 PM -
Thanks :-) However, how can i get the status value (e.g. Lost, Won, etc). This does not appear to work :-(
If entity.Properties.Contains("statuscode") Then
Dim OppCloseState As Status = New Status
OppCloseState = (entity.Properties.Item("statuscode"))
f.DebugMsg("opportunityclose state: " + OppCloseState.name.ToString())
End IfTuesday, September 14, 2010 1:53 PM -
Lost, Won, Active are the statecode so you can get this using statecode field.
MayankP My Blog My twitterTuesday, September 14, 2010 2:18 PM -
but that just returns 'Completed' regardless of if it's Won or Lost.Tuesday, September 14, 2010 2:24 PM
-
how you are getting entity field in following code?
If entity.Properties.Contains("statuscode") Then
Dim OppCloseState As Status = New Status
OppCloseState = (entity.Properties.Item("statuscode"))
f.DebugMsg("opportunityclose state: " + OppCloseState.name.ToString())
End Ifare you using Assembly images?
MayankP My Blog My twitterTuesday, September 14, 2010 3:11 PM -
I believe you have to register a pre-event plug-in on the Win message in the parent pipeline. The platform generates a separate Win message for a won opportunity.
Note that the only messages processed by a child pipeline are create, update, and delete.
Tech Writer
- Marked as answer by DavidBerryMVP, Moderator Saturday, November 6, 2010 12:46 AM
Tuesday, September 14, 2010 10:12 PM -
The Win message will make the following calls in the child pipeline (which is where you need to register this plug-in):
- Update of Opportunity (setting statecode to Completed and statuscode to Won)
- Create of OpportunityClose (with the appopriate details)In the plug-in, do something similar to the following:
If entity.Properties.Contains("statuscode") Then Dim OppCloseState As Status = (entity.Properties.Item("statuscode")) //Create the CrmService Dim Service As CrmService = CreateCrmService(...) //Query for the role of the current user //Throw InvalidPluginExecutionException(...) End If
Since this is executing in the child pipeline, the transaction should be rolled back.
- Proposed as answer by George DoubinskiMVP, Moderator Wednesday, September 15, 2010 1:51 AM
Tuesday, September 14, 2010 10:48 PM -
no im not using assembly images. am i supposed to be?Wednesday, September 15, 2010 8:19 AM
-
You can also prevent it using Javascript in OnSave event.
Use event.Mode = 5 to find the opportunity stage (Closing Stage)
Refer https://community.dynamics.com/blogs/hardworkdays/comments/64290.aspx
- Proposed as answer by VinothBalasubramanian Wednesday, September 15, 2010 9:42 AM
- Marked as answer by DavidBerryMVP, Moderator Saturday, November 6, 2010 12:47 AM
Wednesday, September 15, 2010 9:42 AM -
this is what i use to get the entity:
Dim entity As DynamicEntity = CType(context.InputParameters.Properties(ParameterName.Target), DynamicEntity)
Wednesday, September 15, 2010 10:09 AM -
HI, this is what my code is currently doing, but it keeps on complaining that 'Object reference not set to an instance of an object!'. how can i get the status of the Opportunity Close window? Someone here said i need to use images? However, i thought images were used for 'delete' actions so you can see the previous values? are they also used for values that are about to be set?
Public Sub Execute(ByVal context As IPluginExecutionContext) Implements IPlugin.Execute If context.MessageName = "Create" Then Dim temp As New ConvertEmailToCase service = temp.CreateCrmService(context, True) f.debugfile = "c:\temp\oppclose.txt" f.DebugMsg("----------opportunity close as won plugin-------") Dim entity As DynamicEntity = CType(context.InputParameters.Properties(ParameterName.Target), DynamicEntity) If entity.Properties.Contains("statuscode") Then Dim OppCloseState As New Status Dim temo As String = "" f.DebugMsg("about to start getting statuscode") temo = CType(entity.Properties.Item("statuscode"), Status).Value.ToString() f.DebugMsg("about to output statuscode") f.DebugMsg("opportunityclose status: " + temo) Else f.DebugMsg("statuscode not found!") End If
Wednesday, September 15, 2010 12:51 PM -
Is the plug-in registered on Create of OpportunityClose or of Opportunity?Wednesday, September 15, 2010 3:47 PM
-
create of opportunitycloseThursday, September 16, 2010 7:25 AM
-
Images are also useful in the case of Update as they will show the value that was changed (pre/post).Saturday, November 6, 2010 1:01 AM
-
I have a similar situation where I need to fire a plugin post an opportunity won in CRM 2011.
I created a step in Create of Opportunity close. While I can get the Opportunity ID, I am not able to tell whether the opportunity is won or lost?
Am I missing anything.
I tried to register a step in Win message of Opportunity. my plugin didn't fire at all. Am I missing anything. Should I register Image?
Sunday, July 15, 2012 6:38 PM -
Hi CRM elite,
I have exactly the same problem? Can you please share how you resolved this? Thanks in advance.
I have a similar situation where I need to fire a plugin post an opportunity won in CRM 2011.
I created a step in Create of Opportunity close. While I can get the Opportunity ID, I am not able to tell whether the opportunity is won or lost?
Am I missing anything.
I tried to register a step in Win message of Opportunity. my plugin didn't fire at all. Am I missing anything. Should I register Image?
Friday, May 24, 2013 4:05 PM