1
votes

I am having issues with my plugins not displaying exceptions that are shown (though it is acting correctly in that it is not saving), but only in one situation. Here is the scenario:

We have an entity, and there can only exist one active of these per business unit. I have plugins to detect activate, assign and create which all check the owning business unit of the piece and throw an exception if it is a duplicate.

This works on create, on activate, and on Assign WHEN assign is called by pressing the assign button at the top or pressing the magnifying glass next to the owner field. The images below work. Magnifying GlassAssign Button

However, if a user types in the new owner name in the textbox and selects a user, then presses save, the assign plugin gets called, and the change is prevented, but the exception message box does not appear. I have used our internal logging system and seen that the exception line is being hit, but still not displayed.

Public Class OurEntityPreAssign
    Implements IPlugin

    Public Sub Execute(ByVal serviceProvider As System.IServiceProvider) Implements Microsoft.Xrm.Sdk.IPlugin.Execute
        Dim context As IPluginExecutionContext = CType(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
        Dim factory As IOrganizationServiceFactory = CType(serviceProvider.GetService(GetType(Microsoft.Xrm.Sdk.IPluginExecutionContext)), IOrganizationServiceFactory)
        Dim service As IOrganizationService = factory.CreateOrganizationService(context.UserId)
        Dim entity As Entity
        ' Get Assignee
        ' Compare it to the PreImage
        ' If Same BUID, do nothing
        ' Else check for duplicate
        ErrorLogUtil.WriteToFile("PreAssign") '--> This gets logged
        If context.InputParameters.Contains("Assignee") AndAlso TypeOf context.InputParameters("Assignee") Is EntityReference Then
            Dim assigneeER As EntityReference = CType(context.InputParameters("Assignee"), EntityReference)
            entity = service.Retrieve(assigneeER.LogicalName, assigneeER.Id, New Microsoft.Xrm.Sdk.Query.ColumnSet(True))

            Dim er As EntityReference = CType(entity.Attributes("businessunitid"), EntityReference)
            Dim initialBusinessUnit As EntityReference = CType(context.PreEntityImages("PreImage").Attributes("owningbusinessunit"), EntityReference)

            ErrorLogUtil.WriteToFile("initialBusinessUnitID:" & initialBusinessUnit.Id.ToString & ", assigneeER: " & er.Id.ToString)' -->These values are different, which means we need to check to see if the new one already has a record

            If Not er.Id = initialBusinessUnit.Id Then
                If Not er.Name = "Disabled Users" Then
                    If OurEntity.isDuplicate(er.Id, service, context.PreEntityImages("PreImage").LogicalName) Then
                        ErrorLogUtil.WriteToFile("ExceptionThrown") '-->This is being hit, which means that the throw call should also be hit, or some other error should appear
                        Throw New InvalidPluginExecutionException("My Message Here")
                    End If
                End If
            End If
        End If
    End Sub

End Class

Just so it's clear, I HAVE looked at this post and it does not have any answers and it is indeed a different issue because that asker is receiving some sort of error message at least.

Has anybody else ever experienced this?

2

2 Answers

0
votes

Not sure why you’re not getting the error box. One solution is to register your plug-in on the update message. When a user assigns a record the update message fires just before the assign message. You can check that the "Target" entity contains the ownerid field. If it does run the comparison check and throw the error.

HTH

0
votes

Have you tried using the constructor for InvalidPluginExecutionException that accepts a message and an Exception?

So instead of:

Throw New InvalidPluginExecutionException("My Message Here")

something like:

Throw New InvalidPluginExecutionException("My Message Here", new FaultException<OrganizationServiceFault>())