Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Stop Workflow Action does not show error message

(1) ShareShare
ReportReport
Posted on by 8
Hello,
 
I have created a workflow that throws an error when user try to delete other's notes, using a CWA and a Stop Workflow Step.
The issue I am facing is that the when testing, the displayed message is not correct

Details:


Can you help me?
 
Funny enough, I had the same problem using plugin with the code below:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;

public class PreventAnnotationDeletion : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        if (context.MessageName.ToLower() != "delete" || context.PrimaryEntityName.ToLower() != "annotation")
            return;

        Guid annotationId = context.PrimaryEntityId;
        Entity annotation = service.Retrieve("annotation", annotationId, new ColumnSet("ownerid", "createdby"));

        if (annotation != null)
        {
            EntityReference owner = annotation.GetAttributeValue<EntityReference>("ownerid");
            EntityReference createdBy = annotation.GetAttributeValue<EntityReference>("createdby");

            if (owner.Id != createdBy.Id)
            {
                throw new InvalidPluginExecutionException("Deletion is not allowed. The owner and the created by fields do not match.");
            }
        }
    }
}
 
  • AM-12021631-0 Profile Picture
    8 on at
    Stop Workflow Action does not show error message
    Hi @wimkringsapg, you're definetly right, it was the first version of the code.

    However, after changing that the issue stayed the same.

    Also, big update: it seems like it's not about the code: I have managed to have the same problem with the following code!
     
    It seems like there's something hidden that's causing that.
  • wimkringsapg Profile Picture
    7 on at
    Stop Workflow Action does not show error message
    Hi,
    your requirement says that a user is not allowed to delete the notes of another user (if I am understand this correct).
    But in your code you are checking the owner of the annotation against the creator of the annotation...... did somehow the deleting-user became the owner of the annotation?? Otherwise I don't understand this comparison.  I think that you would compare the current user to the owner of the record.
     
    regards, Wim
  • AM-12021631-0 Profile Picture
    8 on at
    Stop Workflow Action does not show error message
    Hey @Ali Hamza, thanks for the response.
     
    I've tried to do like you said but no luck.
     
    Regarding CWA, I just implemented same logic of the plugin, but instead of throwing an exception from the plugin I return a boolean to the workflow that tells whether to throw the error or not.
     
    Here's last code version:
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using System;
    using System.Activities;
    
    public class CheckAnnotationCreator : CodeActivity
    {
        [Input("Annotation Created By")]
        [ReferenceTarget("systemuser")]
        public InArgument<EntityReference> AnnotationCreatedBy { get; set; }
    
        [Input("Annotation Object Id")]
        [ReferenceTarget("incident")]
        public InArgument<EntityReference> AnnotationObjectId { get; set; }
    
        [Output("Is Same User")]
        public OutArgument<bool> IsSameUser { get; set; }
    
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    
            try
            {
                tracingService.Trace("CheckAnnotationCreator: Execute started.");
    
                EntityReference createdBy = AnnotationCreatedBy.Get(executionContext);
                EntityReference objectId = AnnotationObjectId.Get(executionContext);
    
                if (createdBy == null || objectId == null || objectId.LogicalName != "incident")
                {
                    tracingService.Trace("CheckAnnotationCreator: Invalid input parameters.");
                    IsSameUser.Set(executionContext, false);
                    return;
                }
    
                tracingService.Trace($"CheckAnnotationCreator: Created By ID - {createdBy.Id}, User ID - {context.UserId}");
    
                bool isSameUser = createdBy.Id == context.UserId;
                IsSameUser.Set(executionContext, isSameUser);
    
                tracingService.Trace($"CheckAnnotationCreator: Is Same User - {isSameUser}");
            }
            catch (Exception ex)
            {
                tracingService.Trace($"CheckAnnotationCreator: Exception - {ex.Message}");
                throw new InvalidPluginExecutionException("An error occurred in the CheckAnnotationCreator workflow activity.", ex);
            }
        }
    }
    
     
  • Ali Hamza Profile Picture
    143 on at
    Stop Workflow Action does not show error message
    Hi, 
     
    Are you firing plugin on Pre-operation or Post?
     
    I believe in delete message we can't do retrieve through this approach, I think trying with PreImage and getting desired fields from it will help.
     
    For WF and custom WF, I am not sure what's happening there, if you are doing retrieval there as well, then passing the fields values using WF parameters may help resolving the error. 
     
    Thanks
    Regards
    Ali

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,025 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,852 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans