protected override void Execute(CodeActivityContext executionContext) { ITracingService tracingService = executionContext.GetExtension <ITracingService>(); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); tracingService.Trace("{0}{1}", "Start Custom Workflow Activity: CalculateFacilityReplacementCost", DateTime.Now.ToLongTimeString()); var recordId = context.PrimaryEntityId; try { //Get all facilities for the school district FilterExpression filterName = new FilterExpression(); filterName.Conditions.Add(new ConditionExpression("caps_schooldistrict", ConditionOperator.Equal, recordId)); filterName.Conditions.Add(new ConditionExpression("caps_isschool", ConditionOperator.Equal, true)); filterName.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.Equal, 0)); filterName.Conditions.Add(new ConditionExpression("caps_geographicalschooldistrict", ConditionOperator.NotNull)); filterName.Conditions.Add(new ConditionExpression("caps_communitylocation", ConditionOperator.NotNull)); QueryExpression query = new QueryExpression("caps_facility"); query.ColumnSet.AddColumns("caps_communitylocation" , "caps_designcapacitykindergarten" , "caps_strongstartcapacitykindergarten" , "caps_designcapacityelementary" , "caps_strongstartcapacityelementary" , "caps_designcapacitysecondary" , "caps_strongstartcapacitysecondary" , "caps_currentfacilitytype" , "caps_geographicalschooldistrict" , "caps_neighbourhoodlearningcentre"); query.Criteria.AddFilter(filterName); EntityCollection results = service.RetrieveMultiple(query); tracingService.Trace("Line: {0}", "58"); foreach (caps_Facility facility in results.Entities) { var communityLocationRecord = service.Retrieve(facility.caps_CommunityLocation.LogicalName, facility.caps_CommunityLocation.Id, new ColumnSet("caps_projectlocationfactor")) as caps_BudgetCalc_CommunityLocation; tracingService.Trace("Line: {0}", "63"); var hostSchoolDistrictRecord = service.Retrieve(facility.caps_GeographicalSchoolDistrict.LogicalName, facility.caps_GeographicalSchoolDistrict.Id, new ColumnSet("caps_freightrateallowance")) as edu_schooldistrict; tracingService.Trace("Line: {0}", "65"); var facilityTypeRecord = service.Retrieve(facility.caps_CurrentFacilityType.LogicalName, facility.caps_CurrentFacilityType.Id, new ColumnSet("caps_schooltype")) as caps_FacilityType; tracingService.Trace("Line: {0}", "67"); var adjustedDesignK = (int)facility.caps_DesignCapacityKindergarten.GetValueOrDefault(0) + facility.caps_StrongStartCapacityKindergarten.GetValueOrDefault(0); var adjustedDesignE = (int)facility.caps_DesignCapacityElementary.GetValueOrDefault(0) + facility.caps_StrongStartCapacityElementary.GetValueOrDefault(0); var adjustedDesignS = (int)facility.caps_DesignCapacitySecondary.GetValueOrDefault(0) + facility.caps_StrongStartCapacitySecondary.GetValueOrDefault(0); tracingService.Trace("Line: {0}", "72"); var scheduleB = new ScheduleB(service, tracingService); //set parameters scheduleB.SchoolType = facilityTypeRecord.caps_SchoolType.Id; scheduleB.BudgetCalculationType = (int)caps_BudgetCalculationType.Replacement; scheduleB.IncludeNLC = facility.caps_NeighbourhoodLearningCentre.GetValueOrDefault(false); scheduleB.ProjectLocationFactor = communityLocationRecord.caps_ProjectLocationFactor.GetValueOrDefault(1); scheduleB.ExistingAndDecreaseDesignCapacity = new Services.DesignCapacity(0, 0, 0); scheduleB.ExtraSpaceAllocation = 0; scheduleB.ApprovedDesignCapacity = new Services.DesignCapacity(adjustedDesignK, adjustedDesignE, adjustedDesignS); scheduleB.MunicipalFees = 0; //scheduleB.ConstructionNonStructuralSeismicUpgrade = 0; scheduleB.ConstructionSeismicUpgrade = 0; //scheduleB.ConstructionSPIRAdjustment = 0; tracingService.Trace("Line: {0}", "89"); //scheduleB.SPIRFees = 0; scheduleB.FreightRateAllowance = hostSchoolDistrictRecord.caps_FreightRateAllowance.GetValueOrDefault(0); //Supplemental Items scheduleB.Demolition = 0; scheduleB.AbnormalTopography = 0; scheduleB.TempAccommodation = 0; scheduleB.OtherSupplemental = 0; //call Calculate tracingService.Trace("CalculateScheduleB: {0}", "Call Calculate Function"); CalculationResult result = scheduleB.Calculate(); tracingService.Trace("Line: {0}", "104"); //Update the facility var recordToUpdate = new caps_Facility(); recordToUpdate.Id = facility.Id; recordToUpdate.caps_PreliminaryReplacementValue = result.Total; recordToUpdate.caps_PreliminaryReplacementCalculatedOn = DateTime.Now; service.Update(recordToUpdate); } //Update Project Request this.error.Set(executionContext, false); } catch (Exception ex) { tracingService.Trace("Error Details: {0}", ex.Message); //might want to also include error message this.error.Set(executionContext, true); this.errorMessage.Set(executionContext, ex.Message); } }
internal static decimal RunCalculation(ITracingService tracingService, IWorkflowContext context, IOrganizationService service, Guid recordId) { tracingService.Trace("{0}{1}", "Regarding Record ID: ", recordId); var columns = new ColumnSet("caps_schooltype" , "caps_projecttype" , "caps_includenlc" , "caps_communitylocation" , "caps_municipalfees" , "caps_constructioncostsspir" , "caps_hostschooldistrict" , "caps_changeindesigncapacitykpositive" , "caps_changeindesigncapacityepositive" , "caps_changeindesigncapacityspositive" , "caps_changeindesigncapacityknegative" , "caps_changeindesigncapacityenegative" , "caps_changeindesigncapacitysnegative" , "caps_facility" , "caps_demolitioncost" , "caps_abnormaltopographycost" , "caps_temporaryaccommodationcost" , "caps_othercost" , "caps_schbadditionalspaceallocation"); var projectRequestRecord = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, columns) as caps_Project; var projectTypeRecord = service.Retrieve(projectRequestRecord.caps_ProjectType.LogicalName, projectRequestRecord.caps_ProjectType.Id, new ColumnSet("caps_budgetcalculationtype")) as caps_ProjectType; var communityLocationRecord = service.Retrieve(projectRequestRecord.caps_CommunityLocation.LogicalName, projectRequestRecord.caps_CommunityLocation.Id, new ColumnSet("caps_projectlocationfactor")) as caps_BudgetCalc_CommunityLocation; var hostSchoolDistrictRecord = service.Retrieve(projectRequestRecord.caps_HostSchoolDistrict.LogicalName, projectRequestRecord.caps_HostSchoolDistrict.Id, new ColumnSet("caps_freightrateallowance")) as edu_schooldistrict; int adjustedDesignK = 0; int adjustedDesignE = 0; int adjustedDesignS = 0; if (projectRequestRecord.caps_Facility != null) { tracingService.Trace("Facility: {0}", projectRequestRecord.caps_Facility.Id); var facilityColumnSet = new ColumnSet("caps_designcapacitykindergarten" , "caps_strongstartcapacitykindergarten" , "caps_designcapacityelementary" , "caps_strongstartcapacityelementary" , "caps_designcapacitysecondary" , "caps_strongstartcapacitysecondary"); var facilityRecord = service.Retrieve(projectRequestRecord.caps_Facility.LogicalName, projectRequestRecord.caps_Facility.Id, new ColumnSet(true)) as caps_Facility; adjustedDesignK = (int)facilityRecord.caps_DesignCapacityKindergarten.GetValueOrDefault(0) + facilityRecord.caps_StrongStartCapacityKindergarten.GetValueOrDefault(0); adjustedDesignE = (int)facilityRecord.caps_DesignCapacityElementary.GetValueOrDefault(0) + facilityRecord.caps_StrongStartCapacityElementary.GetValueOrDefault(0); adjustedDesignS = (int)facilityRecord.caps_DesignCapacitySecondary.GetValueOrDefault(0) + facilityRecord.caps_StrongStartCapacitySecondary.GetValueOrDefault(0); } int increaseDesignK = (int)projectRequestRecord.caps_ChangeinDesignCapacityKPositive.GetValueOrDefault(0); int increaseDesignE = (int)projectRequestRecord.caps_ChangeinDesignCapacityEPositive.GetValueOrDefault(0); int increaseDesignS = (int)projectRequestRecord.caps_ChangeinDesignCapacitySPositive.GetValueOrDefault(0); int decreaseDesignK = (int)projectRequestRecord.caps_ChangeinDesignCapacityKNegative.GetValueOrDefault(0); int decreaseDesignE = (int)projectRequestRecord.caps_ChangeinDesignCapacityENegative.GetValueOrDefault(0); int decreaseDesignS = (int)projectRequestRecord.caps_ChangeinDesignCapacitySNegative.GetValueOrDefault(0); int subtotalDesignK = adjustedDesignK + decreaseDesignK; int subtotalDesignE = adjustedDesignE + decreaseDesignE; int subtotalDesignS = adjustedDesignS + decreaseDesignS; tracingService.Trace("CalculateScheduleB: {0}", "Populate Variable"); var scheduleB = new ScheduleB(service, tracingService); //set parameters scheduleB.SchoolType = projectRequestRecord.caps_SchoolType.Id; scheduleB.BudgetCalculationType = projectTypeRecord.caps_BudgetCalculationType.Value; scheduleB.IncludeNLC = projectRequestRecord.caps_IncludeNLC.GetValueOrDefault(false); scheduleB.ProjectLocationFactor = communityLocationRecord.caps_ProjectLocationFactor.GetValueOrDefault(1); tracingService.Trace("Facility - K:{0} E:{1} S:{2}", adjustedDesignK, adjustedDesignE, adjustedDesignS); tracingService.Trace("Subtotal - K:{0} E:{1} S:{2}", subtotalDesignK, subtotalDesignE, subtotalDesignS); tracingService.Trace("Approved - K:{0} E:{1} S:{2}", subtotalDesignK + increaseDesignK, subtotalDesignE + increaseDesignE, subtotalDesignS + increaseDesignS); scheduleB.ExistingAndDecreaseDesignCapacity = new Services.DesignCapacity(subtotalDesignK, subtotalDesignE, subtotalDesignS); scheduleB.ExtraSpaceAllocation = projectRequestRecord.caps_SchBAdditionalSpaceAllocation; scheduleB.ApprovedDesignCapacity = new Services.DesignCapacity(subtotalDesignK + increaseDesignK, subtotalDesignE + increaseDesignE, subtotalDesignS + increaseDesignS); scheduleB.MunicipalFees = projectRequestRecord.caps_MunicipalFees.GetValueOrDefault(0); //scheduleB.ConstructionNonStructuralSeismicUpgrade = projectRequestRecord.caps_ConstructionCostsNonStructuralSeismicUpgr; scheduleB.ConstructionSeismicUpgrade = projectRequestRecord.caps_ConstructionCostsSPIR; //scheduleB.ConstructionSPIRAdjustment = projectRequestRecord.caps_ConstructionCostsSPIRAdjustments; //scheduleB.SPIRFees = projectRequestRecord.caps_SeismicProjectIdentificationReportFees; scheduleB.FreightRateAllowance = hostSchoolDistrictRecord.caps_FreightRateAllowance.GetValueOrDefault(0); //Supplemental Items scheduleB.Demolition = projectRequestRecord.caps_DemolitionCost.GetValueOrDefault(0); scheduleB.AbnormalTopography = projectRequestRecord.caps_AbnormalTopographyCost.GetValueOrDefault(0); scheduleB.TempAccommodation = projectRequestRecord.caps_TemporaryAccommodationCost.GetValueOrDefault(0); scheduleB.OtherSupplemental = projectRequestRecord.caps_OtherCost.GetValueOrDefault(0); //call Calculate tracingService.Trace("CalculateScheduleB: {0}", "Call Calculate Function"); CalculationResult result = scheduleB.Calculate(); //Update PRFS Option with Calculations var recordToUpdate = new caps_Project(); recordToUpdate.Id = recordId; //Section 2 recordToUpdate.caps_SchBSpaceAllocationNewReplacement = result.SpaceAllocationNewReplacement; recordToUpdate.caps_SchBTotalSpaceAllocation = result.SpaceAllocationNewReplacement + projectRequestRecord.caps_SchBAdditionalSpaceAllocation.GetValueOrDefault(0); //recordToUpdate.caps_SchBSpaceAllocationNLC = result.SpaceAllocationNLC; //Section 3 recordToUpdate.caps_SchBBaseBudgetRate = result.BaseBudgetRate; recordToUpdate.caps_SchBProjectSizeFactor = result.ProjectSizeFactor; recordToUpdate.caps_SchBProjectLocationFactor = result.ProjectLocationFactor; recordToUpdate.caps_SchBUnitRate = result.UnitRate; //Section 4 recordToUpdate.caps_SchBConstructionNewSpaceReplacement = result.ConstructionNewReplacement; recordToUpdate.caps_SchBConstructionRenovations = result.ConstructionRenovation; recordToUpdate.caps_SchBSiteDevelopmentAllowance = result.SiteDevelopmentAllowance; recordToUpdate.caps_SchBSiteDevelopmentLocationAllowance = result.SiteDevelopmentLocationAllowance; //Section 5 recordToUpdate.caps_schbdesignfees = result.DesignFees; recordToUpdate.caps_SchBPostContractNewReplacement = result.PostContractNewReplacement; recordToUpdate.caps_SchBPostContractRenovations = result.PostContractRenovation; recordToUpdate.caps_SchBPostContractSeismic = result.PostContractSeismic; //recordToUpdate.caps_SchBMunicipalFees = result.MunicipalFees; recordToUpdate.caps_SchBEquipmentNew = result.EquipmentNew; recordToUpdate.caps_SchBEquipmentReplacement = result.EquipmentReplacement; recordToUpdate.caps_SchBProjectManagementFees = result.ProjectManagement; recordToUpdate.caps_SchBLiabilityInsurance = result.LiabilityInsurance; recordToUpdate.caps_SchBPayableTaxes = result.PayableTaxes; recordToUpdate.caps_SchBRiskReserve = result.RiskReserve; recordToUpdate.caps_SchBRiskReservePercent = result.RiskReservePercent; recordToUpdate.caps_SchBNLCBudgetAmount = result.NLCBudgetAmount; service.Update(recordToUpdate); return(result.Total); }