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
|
Field
|
Type
|
Usage/Values
|
Lookup to New Bus. Budgets entity
|
||
Text (4 Characters)
|
||
Lookup to Currency entity
|
||
Money
|
||
Date
|
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;
}
}
}
}