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 :
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?

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    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?

  • Community Member Profile Picture
    on at

    How?

    Can you please give me example?

    Thanks.

  • ashlega Profile Picture
    34,477 on at

    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
    on at

    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
    34,477 on at

    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
    on at

    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());

    }

    }

    }

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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
    on at

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

    Thank you very much for your time and efforts.

  • ashlega Profile Picture
    34,477 on at

    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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans