Requirement Overview
The target entered shall be an overall annual target per individual based on expected new business revenue. The target will be in the currency of the related Business Unit. E.g. UK = GBP,
NORWAY = EUR.
The target will be split into monthly targets based on the corresponding budget phasing record for the Business Unit, Budget Year & related Territory (optional).
Solution Brief
1. Create new CRM Entity called New Bus. Budgets
Field
|
Type
|
Usage/Values
|
|
Lookup to user entity
|
Relate record to specific user
|
Budget Year
|
Text (4 Characters)
|
|
|
Lookup to Currency entity
|
Currency of the Financial Target
|
Target
|
Money
|
|
Version
|
Whole Number
|
|
Budget Phasing
|
Lookup to Budget Phasing entity
|
|
Business Unit
|
Lookup to Business Unit entity
|
|
|
Lookup to Territory entity
|
|
Active – Active
Inactive – Revised
2.
Create New Entity call New Bus.
Stats
Field
|
Type
|
Usage/Values
|
|
Lookup to New Bus. Budgets entity
|
|
|
Text (4 Characters)
|
|
|
Lookup to Currency entity
|
|
|
Money
|
|
|
Date
|
|
Logical Flow
1. Create New Business Budget entity Record for user with target
o Create Plugin uses the related budget phasing entity record to calculate the target for each month.
o Jan=250*(3/100)
o Feb=250*(5/100)
o —
o Dec=250*(1/100)
o New Bus. Stats entity record create for every month of the year based on calculation.
2. If New Business Budget record is amended. Version number field is incremented and all New Bus. Stats entity records are recalculated.
3. If New Business Budget entity is deleted, all related New Bus. Stats are also deleted.
————————————————————————————————————————-using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace SampleSolution2016
{
public class ClientStatisticsMonthlyTarget : IPlugin
{
/// <summary>
///
/// Plugin registration steps :
///
/// Message : Create
/// PrimaryEntityName : new_newbusinessbudgets
/// Event : PostOperation
/// </summary>
/// <param name=”serviceProvider”></param>
public void Execute(IServiceProvider serviceProvider)
{
try
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName != “Create” && context.PrimaryEntityName != “new_newbusinessbudgets”)
{
return;
}
if ((context.InputParameters.Contains(“Target”)) && (context.InputParameters[“Target”] is Entity))
{
Entity entity = (Entity)context.InputParameters[“Target”];
//new_budgetphasing —>budget
//new_budgetyer
//new_target
//new_territory
Money target = (Money)entity[“new_target”];
Entity Phasing = new Entity(“p3dev_budgetphasing”);
//Get budget phasing – check year + territory first, than just budget year
EntityReference businessUnit = (EntityReference)entity[“new_businessunit”];
Entity bu = service.Retrieve(“businessunit”, businessUnit.Id, new ColumnSet(“name”));
QueryExpression query = new QueryExpression(“new_budgetphasing”);
query.ColumnSet = new ColumnSet(true);
//
query.Criteria.AddCondition(new ConditionExpression(“new_businessunit”, ConditionOperator.Equal, bu.Id));
if (entity.Contains(“new_territory”))
{
EntityReference territory = (EntityReference)entity[“new_territory”];
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“new_territory”, ConditionOperator.Equal, territory.Id));
}
else
{
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“new_territory”, ConditionOperator.Null));
}
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“new_year”, ConditionOperator.Equal, entity[“new_budgetyear”].ToString()));
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“statuscode”, ConditionOperator.Equal, 1));
EntityCollection results = service.RetrieveMultiple(query);
if (results.Entities.Count == 0)
{
query = new QueryExpression(“p3dev_budgetphasing”);
query.ColumnSet = new ColumnSet(true);
query.Criteria.AddCondition(new ConditionExpression(“new_territory”, ConditionOperator.Null));
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“new_year”, ConditionOperator.Equal, entity[“new_budgetyear”].ToString()));
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“statuscode”, ConditionOperator.Equal, 1));
results = service.RetrieveMultiple(query);
}
if (results.Entities.Count > 0)
{
Phasing = results.Entities[0];
//Counter loop 1 to 12 (Jan to Dec) create Statistics record for every month based on budget phasing record for territory and/or year
int counter = 1;
Money TargetToDate = new Money(0);
while (counter <= 12)
{
//
//new_name
//new_budgetyear
//new_newbusbudgetid
//new_period
//new_currency
//new_target
Entity Stats = new Entity(“new_newbusinessstatistics”);
Stats[“new_name”] = entity[“new_name”].ToString() + “_” + Phasing[“p3dev_year”].ToString() + “_” + counter.ToString();
Stats[“new_newbusbudgetid”] = entity.ToEntityReference();
Stats[“new_budgetyear”] = Phasing[“new_year”].ToString();
decimal phasePercentage = 0;
DateTime y1 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 1);
DateTime y2 = new DateTime();
//Check Counter value to determine phasing month value to select & set last day of month parameter for later lookup
if (counter == 1)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_january”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 2)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_february”].ToString());
try
{
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 29);
}
catch
{
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 28);
}
}
else
{
if (counter == 3)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_march”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 4)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_april”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 30);
}
else
{
if (counter == 5)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_may”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 6)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_june”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 30);
}
else
{
if (counter == 7)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_july”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 8)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_august”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 9)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_september”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 30);
}
else
{
if (counter == 10)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_october”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
else
{
if (counter == 11)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_november”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 30);
}
else
{
if (counter == 12)
{
phasePercentage = Convert.ToDecimal(Phasing[“new_december”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“new_year”].ToString()), counter, 31);
}
}
}
}
}
}
}
}
}
}
}
}
//Add basic attributes to new Statistics record
Stats[“new_period”] = y2;
phasePercentage = (phasePercentage / 100) * target.Value;
Money targetRevenue = new Money(phasePercentage);
Stats[“new_currency”] = entity[“new_currency”];
Stats[“new_target”] = targetRevenue;
//Stats[“p3dev_actualrevenue”] = new Money(0);
//DateTime analysisPeriod = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 1);
//Stats[“p3dev_analysisperiod”] = analysisPeriod;
service.Create(Stats);
counter++;
}
}
}
}
catch (Exception)
{
throw;
}
}
}
}
———————————————————————————————————————
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace SampleSolution2016
{
/// <summary>
/// Plugin registration steps :
///
/// Message : Update
/// PrimaryEntityName : new_newbusinessbudgets
/// Event : PreOperation
/// Fitering Attributes : new_budgetphasing, new_budgetyear, new_businessunit, new_currency, new_name, new_target, new_territory, new_user
///
///
/// Register On Image :
/// Image Type : PreImage
/// Name : PreImagenewbusinessbudgets
/// Enitiy Alias : PreImagenewbusinessbudgets
/// Parameters :new_budgetphasing, new_budgetyear, new_businessunit, new_currency, new_name, new_target, new_territory, new_user, new_version
/// </summary>
public class UpdateClientStatisticsMonthlyTarget : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName != “Update” && context.PrimaryEntityName != “new_newbusinessbudgets”)
{
return;
}
if ((context.InputParameters.Contains(“Target”)) && (context.InputParameters[“Target”] is Entity))
{
Entity entity = (Entity)context.InputParameters[“Target”];
Entity preImageNewBusinessBudges = (Entity)context.PreEntityImages[“PreImagenewbusinessbudgets”];
// currency
//Business Unit
//Budget Year
//Target
//Budget Phasing
//Territory
Money target = new Money();
if (entity.Contains(“new_target”))
{
target = (Money)entity[“new_target”];
}
else
{
target = (Money)preImageNewBusinessBudges[“new_target”];
}
Entity Phasing = new Entity(“p3dev_budgetphasing”);
EntityReference businessUnit;
if (entity.Attributes.Contains(“new_businessunit”))
{
businessUnit = (EntityReference)entity[“new_businessunit”];
}
else
{
businessUnit = (EntityReference)preImageNewBusinessBudges[“new_businessunit”];
}
Entity bu = service.Retrieve(“businessunit”, businessUnit.Id, new ColumnSet(“name”));
QueryExpression query = new QueryExpression(“p3dev_budgetphasing”);
query.ColumnSet = new ColumnSet(true);
//
query.Criteria.AddCondition(new ConditionExpression(“p3dev_businessunit”, ConditionOperator.Equal, bu.Id));
EntityReference territory;
if (entity.Contains(“new_territory”))
{
territory = (EntityReference)entity[“new_territory”];
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_territory”, ConditionOperator.Equal, territory.Id));
}
else if (preImageNewBusinessBudges.Contains(“new_territory”))
{
territory = (EntityReference)preImageNewBusinessBudges[“new_territory”];
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_territory”, ConditionOperator.Equal, territory.Id));
}
else
{
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_territory”, ConditionOperator.Null));
}
if (entity.Contains(“new_budgetyear”))
{
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_year”, ConditionOperator.Equal, entity[“new_budgetyear”].ToString()));
}
else
{
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_year”, ConditionOperator.Equal, preImageNewBusinessBudges[“new_budgetyear”].ToString()));
}
query.Criteria.AddFilter(LogicalOperator.And);
query.Criteria.AddCondition(new ConditionExpression(“statuscode”, ConditionOperator.Equal, 1));
EntityCollection results = service.RetrieveMultiple(query);
if (results.Entities.Count == 0)
{
query = new QueryExpression(“p3dev_budgetphasing”);
query.ColumnSet = new ColumnSet(true);
query.Criteria.AddCondition(new ConditionExpression(“p3dev_territory”, ConditionOperator.Null));
query.Criteria.AddFilter(LogicalOperator.And);
if (entity.Contains(“new_budgetyear”))
{
query.Criteria.AddCondition(new ConditionExpression(“p3dev_year”, ConditionOperator.Equal, entity[“new_budgetyear”].ToString()));
query.Criteria.AddFilter(LogicalOperator.And);
}
else
{
query.Criteria.AddCondition(new ConditionExpression(“p3dev_year”, ConditionOperator.Equal, preImageNewBusinessBudges[“new_budgetyear”].ToString()));
query.Criteria.AddFilter(LogicalOperator.And);
}
query.Criteria.AddCondition(new ConditionExpression(“statuscode”, ConditionOperator.Equal, 1));
results = service.RetrieveMultiple(query);
}
if (results.Entities.Count > 0)
{
Phasing = results.Entities[0];
//Counter loop 1 to 12 (Jan to Dec) create Statistics record for every month based on budget phasing record for territory and/or year
int counter = 1;
QueryExpression bdgtPhasing = new QueryExpression(“new_newbusinessstatistics”);
bdgtPhasing.ColumnSet = new ColumnSet(true);
bdgtPhasing.Criteria.AddCondition(new ConditionExpression(“new_newbusbudgetid”, ConditionOperator.Equal, entity.Id));
bdgtPhasing.Criteria.AddFilter(LogicalOperator.And);
bdgtPhasing.Criteria.AddCondition(new ConditionExpression(“statuscode”, ConditionOperator.Equal, 1));
EntityCollection Stats = service.RetrieveMultiple(bdgtPhasing);
Money TargetToDate = new Money(0);
while (counter <= 12)
{
Entity Sta = Stats.Entities[counter – 1];
string name;
if (entity.Contains(“new_name”))
{
name = entity[“new_name”].ToString();
}
else
{
name = preImageNewBusinessBudges[“new_name”].ToString();
}
Sta[“new_name”] = name + “_” + Phasing[“p3dev_year”].ToString() + “_” + counter.ToString();
//Stats[“new_newbusbudgetid”] = entity.ToEntityReference();
Sta[“new_budgetyear”] = Phasing[“p3dev_year”].ToString();
decimal phasePercentage = 0;
DateTime y1 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 1);
DateTime y2 = new DateTime();
//Check Counter value to determine phasing month value to select & set last day of month parameter for later lookup
if (counter == 1)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_january”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 2)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_february”].ToString());
try
{
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 29);
}
catch
{
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 28);
}
}
else
{
if (counter == 3)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_march”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 4)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_april”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 30);
}
else
{
if (counter == 5)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_may”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 6)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_june”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 30);
}
else
{
if (counter == 7)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_july”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 8)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_august”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 9)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_september”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 30);
}
else
{
if (counter == 10)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_october”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
else
{
if (counter == 11)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_november”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 30);
}
else
{
if (counter == 12)
{
phasePercentage = Convert.ToDecimal(Phasing[“p3dev_december”].ToString());
y2 = new DateTime(Convert.ToInt32(Phasing[“p3dev_year”].ToString()), counter, 31);
}
}
}
}
}
}
}
}
}
}
}
}
//Add basic attributes to new Statistics record
Sta[“new_period”] = y2;
phasePercentage = (phasePercentage / 100) * target.Value;
Money targetRevenue = new Money(phasePercentage);
if (entity.Contains(“new_currency”))
{
Sta[“new_currency”] = entity[“new_currency”];
}
else
{
Sta[“new_currency”] = preImageNewBusinessBudges[“new_currency”];
}
Sta[“new_target”] = targetRevenue;
service.Update(Sta);
counter++;
}
}
int counterValue = (int)preImageNewBusinessBudges[“new_version”];
preImageNewBusinessBudges[“new_version”] = counterValue + 1;
}
}
catch (Exception)
{
throw;
}
}
}
}