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?
*This post is locked for comments
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
I got my mistake. I was trying to get data from entity rather context.
Thank you very much for your time and efforts.
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());
}
}
}
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());
}
}
}
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?
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!
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
}
}
How?
Can you please give me example?
Thanks.
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?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156