Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

Posted on by Microsoft Employee

Hi,

I want get name of button or any other identity of button in Plugin Project. 

I have 2 button. 1- FulFill Order. 2- Cancel Order

I have created a plugin project(Assembly) and set "statuscode" as parameter. So, whenever statuscode is change, plugin will be called. 

According to my requirement, I want to validate few things before FulFill Order. Same way, I want to validate few things before Cancel Order. So I have set pre-operation as event type.

Plugin execute perfectly on the click on Fulfill Order and Cancel Order but I am not getting which button is clicked.

I don't know how can I know which button is clicked. I have separate code for Fulfill and Cancel.

Can anybody suggest me how can I know which button is clicked? or any other logic that suggest which button is clicked?

FullFillOrderWorkflow.png

*This post is locked for comments

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Right. Especially since your plugin is running in pre-operation.. Updated data is not, yet, in the database at that point, so, if you use "retrieve", you will see "old" field values

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    I got my mistake. I was trying to get data from entity rather context.

    Thank you very much for your time and efforts.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Try this:

    public void Execute(IServiceProvider serviceProvider)

    {

    ITracingService tracingService =(ITracingService)serviceProvider.GetService(typeof(ITracingService));

    IPluginExecutionContext context = (IPluginExecutionContext)

    serviceProvider.GetService(typeof(IPluginExecutionContext));

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

    {

    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName != "salesorder")

    return;

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    if (entity.contains("statuscode"))

    {

    var statusreason = ((OptionSetValue)(entity["statuscode"])).Value;

    throw new InvalidPluginExecutionException(statusreason.ToString());

    }

    }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Code:

    public void Execute(IServiceProvider serviceProvider)

    {

    ITracingService tracingService =(ITracingService)serviceProvider.GetService(typeof(ITracingService));

    IPluginExecutionContext context = (IPluginExecutionContext)

    serviceProvider.GetService(typeof(IPluginExecutionContext));

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

    {

    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName != "salesorder")

    return;

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    if (entity.Attributes.Contains("salesorderid"))

    {

    Guid regardingobjectid = new Guid(entity.Attributes["salesorderid"].ToString());

    var id = entity.Attributes["salesorderid"].ToString();

    Entity member = service.Retrieve("salesorder", regardingobjectid, new ColumnSet("statuscode", "ordernumber"));

    var statusreason = ((OptionSetValue)(member.Attributes["statuscode"])).Value;

    throw new InvalidPluginExecutionException(statusreason.ToString());

    }

    }

    }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Hi,

     pre-operation "Target" should have all the updated values..

     Are you sure the plugin is called only once? (it could be that "fulfill order", for example, does it twice.. first to update some other values, then to update the status)

     Also, could you post your code just in case?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Hi Alex,

    Thanks. I think you are suggesting Post-Operation event which calles plugin after order get fulfilled.

    I am using Pre-Operation(plz review screenshot). I have reviewed your code, it provide me 1. 1 is for New.

    I have conditions which need to validate before order get fulfill/cancel. i.e if condition is false, I simply raise PluginException and stop Fulfill Order execution. Same way for Cancel Order.

    Please let me know if I understood wrong.

    Please suggest your thoughts!

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Assuming those are out of the box buttons (not your own buttons), it might be something like this:

    var entity = (Entity)context.InputParameters["Target"];

    if(entity.contains("statuscode"))

    {

     int newStatusCode = ((OptionSetValue)entity.Attributes["statuscode"]).Value;

     if(newStatusCode == ...)//Use the value for "cancel"

     {

        //Do whatever you need to do for "Cancel" button

     }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    How?

    Can you please give me example?

    Thanks.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to get name of button or any other identity of button in Plugin Project if Event type is Pre-operation?

    Hi,

     you can't get button name in the plugin. Those buttons will set different statuscodes, though, so why don't you simply use some conditions on the statuscode?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans