web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Opportunity Update plugin misbehaving

(0) ShareShare
ReportReport
Posted on by

Have created a CRM 2013 Plugin to update a simple field in Opportunity, running this in my DEV ENV. Form has field is visible and editable.

My plugin keeps on having an OpportunityUpdate.Plugin: System.MissingMethodException: Constructor on type 'OpportunityUpdate.Plugin' not found message.

Below the Main Plugin and the UpdateOpportunity, also for some reason the update to the "pearl_opportunityguid" doesn't happen and doesn't error either. Any help would be greatly appreciated.

// Standard plugin code created by VS:

public class Plugin : IPlugin
{
protected class LocalPluginContext
{
internal IServiceProvider ServiceProvider
{
get;

private set;
}

internal IOrganizationService OrganizationService
{
get;

private set;
}

internal IPluginExecutionContext PluginExecutionContext
{
get;

private set;
}

internal ITracingService TracingService
{
get;

private set;
}

private LocalPluginContext()
{
}

internal LocalPluginContext(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}

// Obtain the execution context service from the service provider.
this.PluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the tracing service from the service provider.
this.TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the Organization Service factory service from the service provider
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

// Use the factory to generate the Organization Service.
this.OrganizationService = factory.CreateOrganizationService(this.PluginExecutionContext.UserId);
}

internal void Trace(string message)
{
if (string.IsNullOrWhiteSpace(message) || this.TracingService == null)
{
return;
}

if (this.PluginExecutionContext == null)
{
this.TracingService.Trace(message);
}
else
{
this.TracingService.Trace(
"{0}, Correlation Id: {1}, Initiating User: {2}",
message,
this.PluginExecutionContext.CorrelationId,
this.PluginExecutionContext.InitiatingUserId);
}
}
}

private Collection<Tuple<int, string, string, Action<LocalPluginContext>>> registeredEvents;

/// <summary>
/// Gets the List of events that the plug-in should fire for. Each List
/// Item is a <see cref="System.Tuple"/> containing the Pipeline Stage, Message and (optionally) the Primary Entity.
/// In addition, the fourth parameter provide the delegate to invoke on a matching registration.
/// </summary>
protected Collection<Tuple<int, string, string, Action<LocalPluginContext>>> RegisteredEvents
{
get
{
if (this.registeredEvents == null)
{
this.registeredEvents = new Collection<Tuple<int, string, string, Action<LocalPluginContext>>>();
}

return this.registeredEvents;
}
}

/// <summary>
/// Gets or sets the name of the child class.
/// </summary>
/// <value>The name of the child class.</value>
protected string ChildClassName
{
get;

private set;
}

/// <summary>
/// Initializes a new instance of the <see cref="Plugin"/> class.
/// </summary>
/// <param name="childClassName">The <see cref=" cred="Type"/> of the derived class.</param>
internal Plugin(Type childClassName)
{
this.ChildClassName = childClassName.ToString();
}

/// <summary>
/// Executes the plug-in.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}

// Construct the Local plug-in context.
LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);

localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", this.ChildClassName));

try
{
// Iterate over all of the expected registered events to ensure that the plugin
// has been invoked by an expected event
// For any given plug-in event at an instance in time, we would expect at most 1 result to match.
Action<LocalPluginContext> entityAction =
(from a in this.RegisteredEvents
where (
a.Item1 == localcontext.PluginExecutionContext.Stage &&
a.Item2 == localcontext.PluginExecutionContext.MessageName &&
(string.IsNullOrWhiteSpace(a.Item3) ? true : a.Item3 == localcontext.PluginExecutionContext.PrimaryEntityName)
)
select a.Item4).FirstOrDefault();

if (entityAction != null)
{
localcontext.Trace(string.Format(
CultureInfo.InvariantCulture,
"{0} is firing for Entity: {1}, Message: {2}",
this.ChildClassName,
localcontext.PluginExecutionContext.PrimaryEntityName,
localcontext.PluginExecutionContext.MessageName));

entityAction.Invoke(localcontext);

// now exit - if the derived plug-in has incorrectly registered overlapping event registrations,
// guard against multiple executions.
return;
}
}
catch (FaultException<OrganizationServiceFault> e)
{
localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));

// Handle the exception.
throw;
}
finally
{
localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
}
}
}

// Custom code

// <copyright file="UpdateOpportunity.cs" company="">
// Copyright (c) 2015 All Rights Reserved
// </copyright>
// <author>GMC</author>
// <date>03/29/2017 09:36:00 AM</date>
// <summary>Implements the UpdateOpportunity Process to update GUID.</summary>

using System;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace OpportunityUpdate
{
public class UpdateOpportunity : IPlugin
{
private ITracingService tracingService;

public void Execute(IServiceProvider serviceProvider)
{
int e = 001;
string eText = "";

//Extract the tracing service for use in debugging sandboxed plug-ins.
tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
// Opportunity Entity coming in
Entity entity = (Entity)context.InputParameters["Target"];

// IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
// IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);//CreateOrganizationService(null);

try
{

if (entity.LogicalName == "opportunity")
{
string guidValue = entity.Id.ToString();
eText = guidValue;

if (entity.Attributes.Contains("pearl_opportunityguid"))
{
entity.Attributes["pearl_opportunityguid"] = guidValue;
}
else
{
entity.Attributes.Add("pearl_opportunityguid", guidValue);
}
}
else
{
throw new InvalidPluginExecutionException("No entity with that name");
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("** Error in code position: " + e + " ** || *** eText: " + eText + " *** An error occurred in the UpdateOpportunity plug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("** Error in code position: " + e + " ** || *** eText: " + eText + " *** UpdateOpportunity: {0}", ex.ToString());
throw;
}
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Zohaib Uddin Khan Profile Picture
    2,822 on at

    Try Plugin profiler and do debugging. Thanks.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans