2
votes

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);
1
you would be well-served to show what your plugin registration looks like. Based on how you have it registered, there could be a variety of reasons why it appears to not be firing this plugin.Joseph Duty
Whats the error message?James Wood
There's no error message. The plugin just simply never gets fired. I tried putting my InvalidPluginExecutionException message earlier up top on the code but never gets fired either... I tried both 'Post-operation' and 'Pre-operation' but still no luck..ichachan
are you sure youre implementing the IPlugin interface's Execute method and that your plugin builds? It's also possible that you're not actually changing the _settoactive field (check audit history for that). For troubleshooting purposes, change filtering attributes to all. Also, I generally use PluginExecutionContext.InputParameters["Target"] for the target entity, though your method probably would still work for that particular line.Joseph Duty
Do you have any other plugin working perfectly in your org? Verify after Calling user changed to Admin (user context)Arun Vinoth

1 Answers

0
votes

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.