protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext", new ArgumentNullException(nameof(localContext)));
            }

            try
            {
                // Check if an association has been created between the Complaint and Industry entities
                if ((localContext.PluginExecutionContext.MessageName != PluginMessage.Associate && localContext.PluginExecutionContext.MessageName != PluginMessage.Disassociate) ||
                    !(localContext.PluginExecutionContext.InputParameters["Relationship"] is Relationship relationship) ||
                    !(localContext.PluginExecutionContext.InputParameters["Target"] is EntityReference target) ||
                    !(localContext.PluginExecutionContext.InputParameters["RelatedEntities"] is EntityReferenceCollection refs) ||
                    relationship.SchemaName != nameof(opc_complaint.opc_complaints_industries_relatedindustries))
                {
                    return;
                }

                // Get complaints that were involved in the association
                var complaintIds = target.LogicalName == nameof(opc_complaint) ? new[] { target.Id } : refs.Select(u => u.Id).ToArray();

                // For all complaints set the recalculate checklist responses field (this will trigger a flow that depends on that field as a trigger)
                foreach (var complaintId in complaintIds)
                {
                    localContext.TracingService.Trace($"Updating complaint with ID: {complaintId} to set recalculate checklists flag.");

                    var complaint = new opc_complaint()
                    {
                        Id = complaintId,
                        opc_recalculatechecklistresponses = true
                    };
                    localContext.OrganizationService.Update(complaint);
                }
            }
            catch (Exception ex)
            {
                // Trace and throw any exceptions
                localContext.Trace($"Exception: {ex.Message} - Stack Trace: {ex.StackTrace}");
                throw new InvalidPluginExecutionException($"An error occurred in the plug-in. {nameof(RecalculateChecklistResponsesFlagPlugin)}: {ex.Message}", ex);
            }
        }
コード例 #2
0
            public void complaint_number_should_be_prefixed_with_legislation(opc_legislation legislation)
            {
                // Arrange
                var context = new XrmFakedContext();

                var complaintNumber = "0000100";
                var complaint       = new opc_complaint
                {
                    Id              = Guid.NewGuid(),
                    opc_number      = complaintNumber,
                    opc_legislation = legislation.ToEntityReference()
                };

                context.Initialize(new List <Entity> {
                    complaint, legislation
                });

                // Act
                context.ExecutePluginWithTarget <ComplaintPrefixPlugin>(complaint);

                // Assert
                complaint.opc_number.Should().Be($"{legislation.opc_acronym}-{complaintNumber}");
            }