Hi Experts,
Here is what I am doing
I have a custom action which is global, not tied to any entity. I have created a ribbon button on 'Order From'. This ribbon button has a js command which calls the actions. I have registered my plugin and added a step to call my action (as message).
My code blocks are as follows:
Plugin block
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Crm.Sdk.Messages;
namespace TaxPlugin
{
public class MyTaxActionClass : IPlugin
{
#region Secure/Unsecure Configuration Setup
private string _secureConfig = null;
private string _unsecureConfig = null;
public TaxActionClass(string unsecureConfig, string secureConfig)
{
_secureConfig = secureConfig;
_unsecureConfig = unsecureConfig;
}
#endregion
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
try
{
tracer.Trace("getting entity reference");
Entity order = (Entity)context.InputParameters["Target"]; <-- fails at this stage
tracer.Trace("Setting tax");
order["totaltax"] = new Money(new decimal(10.54));
service.Update(order);
tracer.Trace("Updated order entity");
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
JS Block
function callSureTax() { var req = new XMLHttpRequest(); req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/wkstx_suretaxWSCallAction", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { Xrm.Utility.alertDialog(this.statusText); } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); }
Here is the snapshot of custom action. This action is just a place holder so I can call a plugin from a ribbon button.
Everything gets called correctly, but I get exception on the line mentioned above in red.
this is what I get in exception
The given key was not present in the dictionary.Detail:
I don't know any way to get Order entity in the plugin code. When I trace
tracer.Trace(context.MessageName);
I get following name: "sdkmessageprocessingstep"
Is there a way to get an entity, am I doing anything wrong in this process.