web
You’re offline. This is a read only version of the page.
close
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.");
            }
        }
    }
}
 
I have the same question (0)
  • Ali Hamza Profile Picture
    170 on at
    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
  • AM-12021631-0 Profile Picture
    8 on at
    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);
            }
        }
    }
    
     
  • wimkringsapg Profile Picture
    7 on at
    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
    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans