As we know that Custom Action gives you a special capability to extend xRM Platform and it gives a great future.
At My Previous Post I was talking about Custom Action as Validation Gate and many of my posts are related to Custom Action.
Now, I would like to show you about Custom Action that not only as validation, but also can give you a new idea to do next action.
Now, start with my previous custom action, but I will not only use that for giving this Business Error :
I would like to put 5600 to fill one of my field value in Quote :
Because actually, I already did pass the Quote entity to my Custom Action, remember this snipped :
Actually, there are two possibility ways to do :
1. Getting an Output, still getting an output from the custom action then in my plugin I set my Revenue field value based on the output from my custom action, rather than I just throwing an error, which is I get that 5600.0
2. I use my custom action to update my Revenue field.
This is what I did is using the number 2, because I want my plugin is only calling my custom action and all of the logic will be done by my custom action, my plugin no need to do anything anymore since this custom action will be called by anywhere, for example if I have an calculator apps in my web or my agent desktop app, etc.
Then, I modify my code in Action Code :
public class Action_SimpleCalculation : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
#region must to have
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// Create service with context of current user
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//create linq context
KonicaBaseContext baseContext = new KonicaBaseContext(service);
//create tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
#endregion
//To get access to the image of the Quote record
EntityReference entityRef = context.InputParameters["Target"] as EntityReference;
//To read the input parameter
Money money1 = context.InputParameters["Money1"] as Money;
Money money2 = context.InputParameters["Money2"] as Money;
//Capture Discount Rate
decimal discount = 0;
discount = (decimal) context.InputParameters["Discount"];
//Now, I am talking about Custom Action as Validation Gate
if(discount >= 100)
{
throw new InvalidPluginExecutionException("Hi, are you crazy to give such discount? We will not gain any profit, do you realize that?!");
}
else{
//This is how the calculation works and custom action as a Calculation Formula
Money sum = new Money(money1.Value + money2.Value);
Money sumafterDiscount = new Money();
sumafterDiscount = new Money(sum.Value - ((sum.Value) * (discount / 100)));
//using this as a response output
context.OutputParameters["MoneySum"] = sumafterDiscount;
//rather than only giving me an output, I would like to call next action, that is update the revenue field in my quot
//I already have my quote Id
Quote myQuote = new Quote();
myQuote.Id = entityRef.Id;
myQuote.tfp_Revenue = sumafterDiscount;
service.Update(myQuote);
}
}
}
Then in my Plugin I just call my Action
//create target entity as early bound
Quote TargetEntity = entity.ToEntity<Quote>();
//call Business Layer
QuoteBL quoteBL = new QuoteBL();
//Money moneySum = quoteBL.ExecuteCustomAction_SimpleCalculation(service, new Money(2000), new Money(5000), TargetEntity.Id) ;
//throw new InvalidPluginExecutionException("Hi, your total Amount will be = " + moneySum.Value.ToString());
//this one to prevent infinite loop, because custom action will update the revenue
if (TargetEntity.tfp_Revenue != null)
{
return;
}
//just call my custom action
quoteBL.ExecuteCustomAction_SimpleCalculation(service, new Money(2000), new Money(5000), TargetEntity.Id);
I don’t call the “throw new InvalidPluginExecutionException” anymore, but I call my Custom Action, instead.
After you saved to trigger plugin and custom action, you can test it and here is your expected result :
Hope it helps!
No comments:
Post a Comment