Asked by:
create a basic plugin to calculate fields on the same form

Question
-
Using C# can someone tell me how I would go about getting field items calculated?
It's a basic plugin i am trying to create. For instance. in my account form i have 3 fields that are all money fields:
orders
complaints
Cases
I would like all three values calculated and placed in a field called "sum".I know how to create plugins on events, it's just a sample code that I am trying to get so I can start myself.
Thursday, November 21, 2013 1:58 PM
All replies
-
Here is some example code I have used to do something similar. Perhaps it will help you. The money fields are a little different but I included a couple example lines that deal with money as well. The first block I am getting values from the fields and doing the calculations. The second block I am setting those calculations back to the fields.
//calculating values and getting values from a field
//setting values to a field
Money item1 = (Money)entity["orders"];
Money item2 = (Money)entity["complaints"];
Money item3 = (Money)entity["cases"];
Money sum = new Money(item1 + item2 + item3);entity["new_sum"] = sum;
//updates the entity
service.Update(entity);
- Edited by KawasakiRider03 Thursday, November 21, 2013 3:11 PM
Thursday, November 21, 2013 3:11 PM -
Te get the sum value, you should use:
Money sum = new Money(item1.Value + item2.Value + item3.Value); //instead of Money sum = new Money(item1 + item2 + item3);
Otherwise the code is correct.
- Edited by Adam Borsik Thursday, November 21, 2013 3:22 PM
Thursday, November 21, 2013 3:18 PM -
It can be done easily through JavaScript. But anyway, you are looking plugin code. So register your plugin on Pre-Create stage of account entity.
Try to use below code :
public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); Entity _Entity; if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target business entity from the input parameters. _Entity = (Entity)context.InputParameters["Target"]; // Verify that the entity represents a account if (_Entity.LogicalName != "account") { return; } } else { return; }
decimal a = ((Money)_Entity.Attributes["new_orders"]).Value;
decimal b= ((Money)_Entity.Attributes["new_complaints"]).Value;
decimal c = ((Money)_Entity.Attributes["new_cases"]).Value;
_Entity.Attributes["new_sum"]=new Money(a+b+c);}
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
Vikram !
- Edited by _Vikram Thursday, November 21, 2013 3:48 PM
Thursday, November 21, 2013 3:35 PM -
Hi thanks you for the codes, it seems to be working but I am having one problem in that I am hoping to get the data to calculate every time there is an update. For instance when someone adds a new order to the calculations should fire without opening the form. This is what I wrote:
protected void ExecutePostServiceContractUpdate(LocalPluginContext localContext) {
if (localContext == null) {
throw new ArgumentNullException("localContext");
}
try {
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
//Get a IOrganizationService
IOrganizationService service = localContext.OrganizationService;
//create a service context
var ServiceContext = new OrganizationServiceContext(service);
//ITracingService tracingService = localContext.TracingService;
Entity entity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
//calculating values and getting values from a field
Money item1 = (Money) entity["new_order"];
Money item2 = (Money) entity["new_case"];
Money sum = new Money(item1.Value + item2.Value);
//setting values to a field
entity["new_sum"] = sum;
//updates the entity
service.Update(entity);
} catch (FaultException ex) {
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}- Edited by Sebd.DD Thursday, November 21, 2013 4:00 PM
Thursday, November 21, 2013 3:52 PM -
Hi,
You could register same plugin on pre update message but you need to register pre image to get unmodified fields value.
i.e. write same logic to read others fields as well :
Entity _PreImage = (Entity)context.PreEntityImages["PreImage"];
decimal a;
if(_Entity.Attributes.contains("new_orders"))
{
a = ((Money)_Entity.Attributes["new_orders"]).Value;
}
else
{
a = ((Money)_PreImage.Attributes["new_orders"]).Value;
}Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
Vikram !Thursday, November 21, 2013 4:06 PM