using System;
using Microsoft.Xrm.Sdk;
namespace SSCMarks
{
public class SscMarks : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.Depth > 1)
{
return;
}
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity sourceRecordInfo = (Entity)context.InputParameters["Target"];
// Obtain the IOrganizationService instance which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
decimal teluguMarks = 0;
decimal secondLanguage = 0;
decimal englishMarks = 0;
decimal mathsMarks = 0;
decimal scienceMarks = 0;
decimal socialMarks = 0;
decimal totalSecuredmarks = 0;
decimal securedPercentage = 0;
int results = 749180001;
int grade = 749180002;
if (context.MessageName.ToLower() == "create")
{
// get telugu marks
if (sourceRecordInfo.Contains("sdc_telugumarks") && sourceRecordInfo["sdc_telugumarks"] != null)
{
teluguMarks = (decimal)sourceRecordInfo["sdc_telugumarks"];
}
// get second language marks
if (sourceRecordInfo.Contains("sdc_secondslanguagemarks") && sourceRecordInfo["sdc_secondslanguagemarks"] != null)
{
secondLanguage = (decimal)sourceRecordInfo["sdc_secondslanguagemarks"];
}
// get english marks
if (sourceRecordInfo.Contains("sdc_englishmarks") && sourceRecordInfo["sdc_englishmarks"] != null)
{
englishMarks = (decimal)sourceRecordInfo["sdc_englishmarks"];
}
// get maths marks
if (sourceRecordInfo.Contains("sdc_mathsmarks") && sourceRecordInfo["sdc_mathsmarks"] != null)
{
mathsMarks = (decimal)sourceRecordInfo["sdc_mathsmarks"];
}
// get science marks
if (sourceRecordInfo.Contains("sdc_sciencemarks") && sourceRecordInfo["sdc_sciencemarks"] != null)
{
scienceMarks = (decimal)sourceRecordInfo["sdc_sciencemarks"];
}
// get social marks
if (sourceRecordInfo.Contains("sdc_socialmarks") && sourceRecordInfo["sdc_socialmarks"] != null)
{
socialMarks = (decimal)sourceRecordInfo["sdc_socialmarks"];
}
// calculate total secured marks
totalSecuredmarks = teluguMarks + secondLanguage + englishMarks + mathsMarks + scienceMarks + socialMarks;
// set total secured marks
sourceRecordInfo["sdc_totalsecuredmarks"] = totalSecuredmarks;
// calculate secured percentage
securedPercentage = (totalSecuredmarks * 100) / 600;
// set secured percentage
sourceRecordInfo["sdc_securedpercentage"] = securedPercentage;
// calculate grade
if (securedPercentage >= 75)
{
grade = 749180000;
}
else if (securedPercentage >= 60)
{
grade = 749180001;
}
else
{
grade = 749180002;
}
// set grade
sourceRecordInfo["sdc_grade"] = new OptionSetValue(grade);
// calculate result
if (teluguMarks >= 35 && secondLanguage >= 35 && englishMarks >= 35 &&
mathsMarks >= 35 && scienceMarks >= 35 && socialMarks >= 35)
{
results = 749180000; // Pass
}
else
{
results = 749180001; // Fail
}
// set result
sourceRecordInfo["sdc_results"] = new OptionSetValue(results);
}
else
{
// update event - read from post image to get latest complete values
Entity postValues = (Entity)context.PostEntityImages["latestvalues"];
// get telugu marks
if (postValues.Contains("sdc_telugumarks") && postValues["sdc_telugumarks"] != null)
{
teluguMarks = (decimal)postValues["sdc_telugumarks"];
}
// get second language marks
if (postValues.Contains("sdc_secondslanguagemarks") && postValues["sdc_secondslanguagemarks"] != null)
{
secondLanguage = (decimal)postValues["sdc_secondslanguagemarks"];
}
// get english marks
if (postValues.Contains("sdc_englishmarks") && postValues["sdc_englishmarks"] != null)
{
englishMarks = (decimal)postValues["sdc_englishmarks"];
}
// get maths marks
if (postValues.Contains("sdc_mathsmarks") && postValues["sdc_mathsmarks"] != null)
{
mathsMarks = (decimal)postValues["sdc_mathsmarks"];
}
// get science marks
if (postValues.Contains("sdc_sciencemarks") && postValues["sdc_sciencemarks"] != null)
{
scienceMarks = (decimal)postValues["sdc_sciencemarks"];
}
// get social marks
if (postValues.Contains("sdc_socialmarks") && postValues["sdc_socialmarks"] != null)
{
socialMarks = (decimal)postValues["sdc_socialmarks"];
}
// calculate total secured marks
totalSecuredmarks = teluguMarks + secondLanguage + englishMarks + mathsMarks + scienceMarks + socialMarks;
// calculate secured percentage
securedPercentage = (totalSecuredmarks * 100) / 600;
// calculate grade
if (securedPercentage >= 75)
{
grade = 749180000;
}
else if (securedPercentage >= 60)
{
grade = 749180001;
}
else
{
grade = 749180002;
}
// calculate result
if (teluguMarks >= 35 && secondLanguage >= 35 && englishMarks >= 35 &&
mathsMarks >= 35 && scienceMarks >= 35 && socialMarks >= 35)
{
results = 749180000; // Pass
}
else
{
results = 749180001; // Fail
}
// create update entity and set all calculated fields
Entity entUpdate = new Entity();
entUpdate.LogicalName = sourceRecordInfo.LogicalName;
entUpdate.Id = sourceRecordInfo.Id;
entUpdate["sdc_totalsecuredmarks"] = totalSecuredmarks;
entUpdate["sdc_securedpercentage"] = securedPercentage;
entUpdate["sdc_grade"] = new OptionSetValue(grade);
entUpdate["sdc_results"] = new OptionSetValue(results);
service.Update(entUpdate);
}
}
catch (Exception ex)
{
tracingService.Trace("SscMarks: {0}", ex.ToString());
throw;
}
}
}
}
}