ichachan
ichachan

Reputation: 667

Write CRM Plugin that Fires when a Field is updated

We're on CRM 2013 on-premise. I'm writing a plugin that fires when a field on Quote entity is updated.

So I registered my plugin on 'Update' message. Then the event is 'Post-operation'. (I tried Pre-operation but still no luck)

Basically the goal is when the field is updated, create a new entity 'ContractShell' and then create relationship between the Quote and the newly created 'ContractShell'.

However my problem is when the field is updated, my plugin never seems to fire. I just simply put a InvalidPluginExecutionException in my code, but for some reason it never fires.... Any ideas? Thanks.

Here's a screenshot of my plugin step:

Plugin step

Here's my code:

var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        // The InputParameters collection contains all the data passed in the message request.
        var targetEntity = context.GetParameterCollection<Entity>(context.InputParameters, "Target");

        if (targetEntity == null)
            throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");

        if (!context.OutputParameters.Contains("id"))
            return;

        Guid QuoteId = (Guid)targetEntity.Attributes["quoteid"];

        var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var service = serviceFactory.CreateOrganizationService(context.UserId);

        var contractShellEntity = new Entity();
        contractShellEntity = new Entity("new_);

        //assign the portfolio
        if (targetEntity.Attributes.Contains(Schema.Quote.Portfolio))
        {
            var quotePortfolio = (EntityReference)targetEntity.Attributes[Schema.Quote.Portfolio];
            contractShellEntity[Schema.new_ContractShell.PortfolioName] = new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
        }

        var contractShellId = service.Create(contractShellEntity);
        throw new InvalidPluginExecutionException(OperationStatus.Failed, "I created New Contract Shell"); 

        //Creating relationship between Contract Shell and the newly created Accounts
        var quoteContractReferenceCollection = new EntityReferenceCollection();
        var quoteContractRelatedEntity = new EntityReference
        {
            Id = contractShellId,
            LogicalName = contractShellEntity.LogicalName
        };
        quoteContractReferenceCollection.Add(quoteContractRelatedEntity);
        var quoteContractReferenceCollectionRelRelationship = new Relationship
        {
            SchemaName = Schema.new_ContractShell.ContractQuoteRelationship
        };
        service.Associate("quote", QuoteId, quoteContractReferenceCollectionRelRelationship, quoteContractReferenceCollection);

Upvotes: 3

Views: 2814

Answers (1)

Joseph Duty
Joseph Duty

Reputation: 795

You need to register not only the plugin but an SDKMessageProcessingStep. Also, you have to implement the Execute method in your plugin to be able to register it, so either you're missing code in your snippet, or your code is the problem.
Also, your InvalidPluginExecutionException is nested after a number of checks. Theres a good chance you don't have any output parameters if you don't know how to register a plugin, so your code would actually return before you hit the exception.

Upvotes: 0

Related Questions