////Thid Plugin will fire on Work order post Create to create Inspection records. public void CreateInspections(LocalPluginContext localContext) { try { IPluginExecutionContext context = localContext.PluginExecutionContext; ITracingService tracingService = localContext.TracingService; IOrganizationService service = localContext.OrganizationService; tracingService.Trace("Work Order Post Create"); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity workOrder = context.InputParameters["Target"] as Entity; if (workOrder.Attributes.Contains("msdyn_primaryincidenttype")) { Entity workorderrecord = service.Retrieve(workOrder.LogicalName, workOrder.Id, new ColumnSet("msdyn_serviceaccount", "smp_building", "msdyn_agreement", "msdyn_primaryincidenttype", "msdyn_customerasset", "ownerid")); CreateInspections inspection = new CreateInspections(); inspection.CreateInspectionsforWorkOrders(service, workorderrecord, tracingService); } if (workOrder.Attributes.Contains("msdyn_servicerequest")) { EntityReference serviceRequest = workOrder.GetAttributeValue <EntityReference>("msdyn_servicerequest"); this.UpdateDynamicQuestionandAnsewrs(serviceRequest.Id, workOrder, service, tracingService); Entity workorderrecord = new Entity(workOrder.LogicalName); workorderrecord.Id = workOrder.Id; workorderrecord.Attributes.Add("smp_dynamicproblemtypenotes", this.UpdateDynamicQuestionandAnsewrs(serviceRequest.Id, workOrder, service, tracingService)); service.Update(workorderrecord); } } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// CreateInspections /// </summary> /// <param name="localContext"></param> public void CreateInspections(LocalPluginContext localContext) { try { IPluginExecutionContext context = localContext.PluginExecutionContext; ITracingService tracingService = localContext.TracingService; IOrganizationService service = localContext.OrganizationService; tracingService.Trace("Work Order Post Update"); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity workOrder = context.InputParameters["Target"] as Entity; Entity workOrderRecord = service.Retrieve(workOrder.LogicalName, workOrder.Id, new ColumnSet("msdyn_serviceaccount", "smp_dynamicproblemtypenotes", "smp_triggerwoemail", "msdyn_servicerequest", "msdyn_agreement", "msdyn_primaryincidenttype", "msdyn_customerasset", "ownerid")); if (workOrder.Attributes.Contains("msdyn_primaryincidenttype")) { if (workOrderRecord.GetAttributeValue <EntityReference>("msdyn_agreement") != null) { CreateInspections inspection = new CreateInspections(); inspection.CreateInspectionsforWorkOrders(service, workOrderRecord, tracingService); } } if (workOrder.Attributes.Contains("msdyn_serviceaccount")) { EntityReference account = workOrder.GetAttributeValue <EntityReference>("msdyn_serviceaccount"); if (account != null) { Entity accountrecord = service.Retrieve(account.LogicalName, account.Id, new ColumnSet("smp_tieroneprovider")); Entity updatedworkorder = new Entity(workOrder.LogicalName, workOrder.Id); updatedworkorder.Attributes.Add("smp_tier1provider", accountrecord.GetAttributeValue <string>("smp_tieroneprovider")); service.Update(updatedworkorder); } } if (workOrder.Attributes.Contains("msdyn_timetopromised") || workOrderRecord.GetAttributeValue <bool>("smp_triggerwoemail") != true) { Entity updateRecord = new Entity(workOrder.LogicalName); updateRecord.Id = workOrder.Id; if (workOrderRecord.Attributes.Contains("msdyn_servicerequest") && workOrderRecord.GetAttributeValue <bool>("smp_triggerwoemail") != true) { EntityCollection serviceRequestDynamicNotes = this.GetServiceRequestDynamicNotes(workOrderRecord.GetAttributeValue <EntityReference>("msdyn_servicerequest").Id, service); if (serviceRequestDynamicNotes.Entities != null && serviceRequestDynamicNotes.Entities.Count > 0) { updateRecord.Attributes.Add("smp_dynamicproblemtypenotes", this.UpdateDynamicQuestionandAnswers(workOrderRecord.GetAttributeValue <EntityReference>("msdyn_servicerequest").Id, workOrder, service)); } EntityCollection workorderDynamicNotes = this.GetWorkOrderDynamicNotes(workOrderRecord.Id, service); if (workorderDynamicNotes.Entities != null && workorderDynamicNotes.Entities.Count > 0) { this.UpdateOldDPN(service, workOrderRecord.GetAttributeValue <EntityReference>("msdyn_servicerequest").Id, workOrder.Id, workorderDynamicNotes); } } updateRecord.Attributes.Add("smp_triggerwoemail", true); service.Update(updateRecord); } } } catch (Exception ex) { throw new Exception(ex.Message); } }