Hello,
In a custom entity, I have business rules to show/hide fields based on an option set value..
IF Timesheet Type does not contain data THEN Hide field Customer Name Hide field Project Name Clear Customer Name Clear Project Name ELSE IF Timesheet Type equals "Project" THEN Hide field Customer Name Clear Customer Name Show field Project Name Set Project Name as Business Required ELSE Hide field Project Name Clear Project Name Show field Customer Name Set Customer Name as Business Required
In the same entity, I have a custom plugin that fires onCreate of a record and replicates the data to a separate table.
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TimeEntryCreate.Models;
using System.CodeDom.Compiler;
using System.Web.UI;
using System.Activities.Statements;
using System.Activities;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;
using System.IdentityModel.Metadata;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Linq;
namespace TimeEntryCreate
{
public partial class TimeEntryCreate
{
private string _conStr;
int approved = 803090000;
public static int requiredHours = 8;
public enum Type
{
Actual = 803090000,
Request = 803090001
}
public enum EntryType
{
IN = 803090000,
OUT = 803090001,
}
private static Entity GetEntryTime(IPluginExecutionContext context, IOrganizationService service)
{
Entity entity = (Entity)context.InputParameters["Target"];
string id = context.PrimaryEntityId.ToString();
var fetchXmlCreate = $@"<fetch><entity name=""cr884_timeentry""><attribute name=""cr884_status"" /><attribute name=""cr884_type"" /><attribute name=""cr884_id"" /><attribute name=""cr884_timeentryid"" /><attribute name=""cr884_entrytype"" /><attribute name=""new_checkin_time"" /><attribute name=""cr884_projectid"" /><attribute name=""cr884_resourceid"" /><filter><condition attribute=""cr884_timeentryid"" operator=""eq"" value=""{id}"" /></filter></entity></fetch>";
Entity timeEntry = service.RetrieveMultiple(new FetchExpression(fetchXmlCreate)).Entities.FirstOrDefault();
return timeEntry;
}
private static void FillApproval(IOrganizationService service, Entity timeEntry, Guid resourceId, Approval approval, Guid projectId)
{
var requiredHours = GetRequiredHours(service, resourceId);
DateTime checkinDate = ((DateTime)timeEntry.Attributes["new_checkin_time"]);
var fetchXmlIn = $@"<fetch><entity name=""cr884_timeentry""><attribute name=""cr884_status"" /><attribute name=""cr884_type"" /><attribute name=""cr884_id"" /><attribute name=""cr884_timeentryid"" /><attribute name=""cr884_entrytype"" /><attribute name=""new_checkin_time"" /><attribute name=""cr884_resourceid"" /><attribute name=""cr884_projectid"" /><attribute name=""cr884_userid"" /><filter><condition attribute=""cr884_entrytype"" operator=""eq"" value=""{(int)EntryType.IN}"" /><condition attribute=""cr884_resourceid"" operator=""eq"" value=""{resourceId}"" /></filter> <order attribute='createdon' descending='true' /></entity></fetch>";
Entity lastIn = service.RetrieveMultiple(new FetchExpression(fetchXmlIn)).Entities.FirstOrDefault();
if (lastIn != null)
{
DateTime lastInTime = ((DateTime)lastIn.Attributes["new_checkin_time"]);
TimeSpan temp = checkinDate - lastInTime;
decimal totalTime = (decimal)temp.TotalHours;
approval.Duration = totalTime;
approval.ResourceId = resourceId;
approval.OutDateTime = checkinDate;
approval.InDateTime = lastInTime;
var remainingHours = GetRemainingHours(service, approval);
approval.ProjectId = ((EntityReference)lastIn.Attributes["cr884_projectid"]).Id;
approval.RemainingTime = CalculateRemainigHours(service, approval);
if (lastIn.Contains("cr884_userid"))
approval.UserId = ((EntityReference)lastIn.Attributes["cr884_userid"]).Id;
approval.ActualOvertime = CalculateOvertime(service, approval);
approval.IsHoliday = IsHoliday(service, approval);
}
else
{
DateTime lastInTime = ((DateTime)lastIn.Attributes["new_checkin_time"]);
TimeSpan temp = checkinDate - lastInTime;
decimal totalTime = (decimal)temp.TotalHours;
approval.Duration = totalTime;
approval.ResourceId = resourceId;
approval.OutDateTime = checkinDate;
approval.InDateTime = lastInTime;
approval.ProjectId = ((EntityReference)lastIn.Attributes["cr884_projectid"]).Id;
approval.IsHoliday = IsHoliday(service, approval);
}
}
private static void CreateEntry(IOrganizationService service, Guid userId, Approval approval)
{
EntityReference resourceRef = new EntityReference("cr884_timesheetuser", userId);
EntityReference projectRef = new EntityReference("cr884_timesheetproject", approval.ProjectId);
EntityReference holidayRef = new EntityReference("new_flagclassification", approval.IsHoliday);
EntityReference userRef = null;
if (approval.UserId != null)
userRef = new EntityReference("cr884_resource", approval.UserId.Value);
Entity approvalEntity = new Entity("new_timesheetovertime");
approvalEntity["new_duration"] = approval.Duration;
approvalEntity["new_resourcename"] = resourceRef;
approvalEntity["new_actualovertime"] = approval.ActualOvertime;
approvalEntity["new_checkin_time"] = approval.InDateTime;
approvalEntity["new_outdatetime"] = approval.OutDateTime;
approvalEntity["new_projectname"] = projectRef;
approvalEntity["new_username"] = userRef;
approvalEntity["new_remaininghours"] = approval.RemainingTime;
approvalEntity["new_classification"] = holidayRef;
service.Create(approvalEntity);
}During plugin execution, I am getting the following strange error:

If I turn off the business rules the plugin executes successfully without throwing the error.
As long as the business rules are turned on under this entity, it keeps on failing.
Any idea what logic is missing for the plugin to handle this exception? Could you please which line should i amend changes to make it work?
Any help is greatly appreciated.
Best regards,
EBMRay










