Hi All,
I need your help on below mentioned scenario for Plugin development. We are on CRM 2011 on-premise UR 18.
Scenario: When end user close the opportunity as 'Lost', dialog box comes up asking for the close date, competitor,description and couple of other fields. Our requirement is that end user need to put reason for the 'Lost' opportunity into the "Description" field. If they don't put anything into that field than throw error to user informing that description is required.
so I have written below very basic plugin:
public class OpportunityLost : IPlugin { public void Execute(IServiceProvider ServiceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationService service = GetserviceClass.GetCRMService(ServiceProvider); string description = ""; if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity OppClose = (Entity)context.InputParameters["Target"]; if (OppClose.LogicalName.ToLower() != "opportunityclose") { return; } try { description = OppClose.GetAttributeValue<string>("description"); if (description == null || description == string.Empty) { throw new InvalidPluginExecutionException("Please enter reson for lost opportunity in description field"); } } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occurred in the Lost Opportunity plug-in", ex); } } } }
Plug in is registered with below settings.
Message : Create
Entity : opportuityclose
Secondary Entity : none
Event Pipeline : Pre-Operation
Execution Mode : Synchronous
This plugin is firing fine but it fires on both "Lost" and "Won" opportunity scenario. We want this plugin to fireonly when
opportunity is Lost.
Please note that Win and Lost messages won't work here as I won't be able to get 'description' field of the "Opportunityclose" dialog box. Context and entity images are not giving me anything useful (Correct me if I am wrong here ).
Is there anyone out there who has faced the similar situation who can help me to overcome this issue.
Thanks for reading.
Regards,
H.desai