Esempio n. 1
0
        // Si la actividad devuelve un valor, se debe derivar de CodeActivity<TResult>
        // y devolver el valor desde el método Execute.
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension <ITracingService>();

            //Create the context
            IWorkflowContext            context        = executionContext.GetExtension <IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);
            Guid    idWorkFlowInstance = executionContext.WorkflowInstanceId;
            Boolean isError            = false;

            try
            {
                // GUID of target record (case)
                Guid gCaseId    = context.PrimaryEntityId;
                Guid gNewCaseId = Guid.Empty;

                //Obtenemos los valores de los parámetros del caso.
                //string strCaseTypeCode = CaseTypeCode.Get<string>(executionContext);
                EntityReference erCaseType       = CaseType.Get <EntityReference>(executionContext);
                EntityReference erCategory1      = Category1.Get <EntityReference>(executionContext);
                EntityReference erCategory2      = Category2.Get <EntityReference>(executionContext);
                EntityReference erOriginUser     = OriginUser.Get <EntityReference>(executionContext);
                EntityReference erOriginPDV      = OriginPDV.Get <EntityReference>(executionContext);
                EntityReference erOriginRegion   = OriginRegion.Get <EntityReference>(executionContext);
                OptionSetValue  oOriginChannel   = OriginChannel.Get <OptionSetValue>(executionContext);
                OptionSetValue  oStatusCode      = StatusCode.Get <OptionSetValue>(executionContext);
                EntityReference erCustomer       = Customer.Get <EntityReference>(executionContext);
                string          strTitle         = CaseTitle.Get <string>(executionContext);
                EntityReference erContract       = Contract.Get <EntityReference>(executionContext);
                Boolean?        isReincidentCase = ReincidentCase.Get <Boolean>(executionContext);
                EntityReference erAssociatedCase = AssociatedCase.Get <EntityReference>(executionContext);
                OptionSetValue  oTransactionType = TransactionType.Get <OptionSetValue>(executionContext);

                Entity eCase = new Entity("incident");

                if (erCaseType != null)
                {
                    eCase.Attributes["amxperu_casetype"] = erCaseType;
                }

                if (erCategory1 != null)
                {
                    eCase.Attributes["ust_category1"] = erCategory1;
                }

                if (erCategory2 != null)
                {
                    eCase.Attributes["ust_category2"] = erCategory2;
                }

                //if (erOriginUser != null)
                //    eCase.Attributes["ownerid"] = erOriginUser;

                if (erOriginPDV != null)
                {
                    eCase.Attributes["amxperu_originpdv"] = erOriginPDV;
                }

                if (erOriginRegion != null)
                {
                    eCase.Attributes["amxperu_originregion"] = erOriginRegion;
                }

                if (oOriginChannel != null)
                {
                    eCase.Attributes["amxperu_originchannel"] = oOriginChannel;
                }

                if (oStatusCode != null)
                {
                    eCase.Attributes["statuscode"] = oStatusCode;
                }

                if (erCustomer != null)
                {
                    eCase.Attributes["customerid"] = erCustomer;
                }

                if (!string.IsNullOrEmpty(strTitle))
                {
                    eCase.Attributes["title"] = strTitle;
                }

                if (erContract != null)
                {
                    eCase.Attributes["contractid"] = erContract;
                }

                //if (isReincidentCase != null)
                //    eCase.Attributes["amxperu_reincidentcase"] = isReincidentCase;

                if (erAssociatedCase != null)
                {
                    eCase.Attributes["ust_associatedcase1"] = erAssociatedCase;
                }

                if (oTransactionType != null)
                {
                    eCase.Attributes["ust_transactiontype"] = oTransactionType;
                }

                if (eCase.Attributes.Count > 0)
                {
                    gNewCaseId = service.Create(eCase);

                    //if (gNewCaseId != Guid.Empty)
                    //{
                    //    //Entity incident = new Entity("incident");
                    //    //incident.Id = gCaseId;
                    //    //incident["ust_isretentioncreated"] = true;
                    //    //service.Update
                    //}
                }
            }
            catch (FaultException <IOrganizationService> ex)
            {
                isError = true;
                throw new FaultException("IOrganizationServiceExcepcion: " + ex.Message);
            }
            catch (FaultException ex)
            {
                isError = true;
                throw new FaultException("FaultException: " + ex.Message);
            }
            catch (InvalidWorkflowException ex)
            {
                isError = true;
                throw new InvalidWorkflowException("InvalidWorkflowException: " + ex.Message);
            }
            catch (Exception ex)
            {
                isError = true;
                throw new Exception(ex.Message);
            }
            finally
            {
                if (isError)
                {
                    //Entity workflowToCancel = new Entity("asyncoperation");
                    //workflowToCancel.Id = idWorkFlowInstance;
                    //workflowToCancel["statecode"] = new OptionSetValue(3);
                    //workflowToCancel["statuscode"] = new OptionSetValue(32);
                    //service.Update(workflowToCancel);
                }
            }
        }
        }                                                          //Notificado


        #endregion

        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension <ITracingService>();

            //Create the context
            IWorkflowContext            context        = executionContext.GetExtension <IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);


            try
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    EntityReference usCaseType = CaseType.Get <EntityReference>(executionContext);
                    OptionSetValue  usEstado   = StatusCode.Get <OptionSetValue>(executionContext);

                    //tracingService.Trace("usCaseType " + CaseType.ToString());
                    //tracingService.Trace("usEstado " + usEstado.Value);

                    //Local Variables
                    int      estado     = usEstado.Value;
                    string   typeCase   = "";
                    DateTime modifiedOn = DateTime.Now;
                    int      estadoC    = 0;

                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName != "incident")
                    {
                        return;
                    }
                    if (entity == null)
                    {
                        return;
                    }

                    Entity dataCase = service.Retrieve("incident", entity.Id, new ColumnSet("statuscode", "ust_dateofnotification"));

                    //tracingService.Trace("dataCase " + dataCase.Id);

                    if (dataCase.Attributes.Contains("ust_dateofnotification") && dataCase.Attributes["ust_dateofnotification"] != null)
                    {
                        modifiedOn = (DateTime)dataCase.Attributes["ust_dateofnotification"];
                    }

                    DateTime fechatemp = modifiedOn;
                    int      diaT      = fechatemp.Day;
                    int      mesT      = fechatemp.Month;
                    int      anioT     = fechatemp.Year;

                    Entity caseType = service.Retrieve("amxperu_casetype", usCaseType.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("ust_code"));

                    if (caseType.Attributes.Contains("ust_code") && caseType.Attributes["ust_code"] != null)
                    {
                        tracingService.Trace("ust_code " + caseType.Attributes.Contains("ust_code").ToString());
                        //Get the Case Type Code
                        typeCase = caseType.Attributes["ust_code"].ToString(); //004  Reclamo OSIPTEL
                    }

                    //myTrace.Trace("typeCase :" + typeCase.ToString());
                    tracingService.Trace("estado " + estado.ToString());

                    if (typeCase == "004" && estado == 864340003) //Tipo reclamo OSIPTEL and Estado(Notificado)
                    {
                        //Buscar si hay hijo del reclamo Abierto
                        string codeQueja = "005";

                        var fetchQueja = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                         "<entity name='amxperu_casetype'>" +
                                         "<attribute name='amxperu_casetypeid' />" +
                                         "<attribute name='amxperu_name' />" +
                                         "<attribute name='createdon' />" +
                                         "<order attribute='amxperu_name' descending='false' />" +
                                         "<filter type='and'>" +
                                         "<condition attribute='ust_code' value ='" + codeQueja + "' operator= 'eq' />" +
                                         "</filter>" +
                                         "</entity>" +
                                         "</fetch>";

                        EntityCollection resultQ = service.RetrieveMultiple(new FetchExpression(fetchQueja));
                        foreach (var c in resultQ.Entities)
                        {
                            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                           "<entity name='incident'>" +
                                           "<attribute name='title' />" +
                                           "<attribute name='ticketnumber' />" +
                                           "<attribute name='createdon' />" +
                                           "<attribute name='caseorigincode' />" +
                                           "<attribute name='etel_isdispute' />" +
                                           "<attribute name='incidentid' />" +
                                           "<attribute name='statuscode' />" +
                                           "<attribute name='statecode' />" +
                                           "<order attribute='title' descending='false' />" +
                                           "<filter type='and'>" +
                                           "<condition attribute='amxperu_casetype' operator='eq' uiname='' uitype='amxperu_casetype' value='" + c.Id + "' />" +
                                           "<condition attribute='statecode' value='0' operator='eq' /> " +
                                           "<condition attribute='parentcaseid' value='" + entity.Id + "' uitype='incident' operator='eq' /> " +
                                           "</filter>" +
                                           "</entity>" +
                                           "</fetch>";

                            EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                            foreach (var d in result.Entities)
                            {
                                var fetchXmlCon = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                                  "<entity name='etel_crmconfiguration'>" +
                                                  "<attribute name='etel_crmconfigurationid' />" +
                                                  "<attribute name='etel_name' />" +
                                                  "<attribute name='createdon' />" +
                                                  "<attribute name='etel_value' />" +
                                                  "<attribute name='statecode' />" +
                                                  "<order attribute='etel_name' descending='false' />" +
                                                  "<filter type='and'>" +
                                                  "<condition attribute='etel_name' operator='eq' uiname='' value='ResolveOSIPTELClaim' />" +
                                                  "</filter>" +
                                                  "</entity>" +
                                                  "</fetch>";

                                EntityCollection resultCon = service.RetrieveMultiple(new FetchExpression(fetchXmlCon));

                                if (resultCon[0].Attributes["etel_value"].ToString() != null)
                                {
                                    double plazo = Convert.ToUInt32(resultCon[0].Attributes["etel_value"]);

                                    for (int i = 0; i < plazo; i++)
                                    {
                                        if (modifiedOn.DayOfWeek == DayOfWeek.Sunday || modifiedOn.DayOfWeek == DayOfWeek.Saturday)
                                        {
                                            plazo = plazo + 1;
                                        }
                                        modifiedOn = modifiedOn.AddDays(1);
                                    }
                                    DateTime fechaUtil = modifiedOn.AddDays(1);

                                    DateTime fechaActual = DateTime.Now;

                                    if (fechaUtil < fechaActual)
                                    {
                                        if (d.Attributes.Contains("statecode") && d["statecode"] != null)
                                        {
                                            estadoC = ((OptionSetValue)dataCase.Attributes["statecode"]).Value;
                                            if (estadoC != 0)
                                            {
                                                Entity resolution = new Entity("incidentresolution");
                                                resolution["subject"]    = "Closed";
                                                resolution["incidentid"] = new EntityReference(dataCase.LogicalName, dataCase.Id);

                                                CloseIncidentRequest closecase = new CloseIncidentRequest();

                                                closecase.IncidentResolution = resolution;
                                                closecase.Status             = new OptionSetValue(5);

                                                CloseIncidentResponse closeresponse = (CloseIncidentResponse)service.Execute(closecase);
                                            }
                                        }
                                        else
                                        {
                                            Entity resolution = new Entity("incidentresolution");
                                            resolution["subject"]    = "Closed";
                                            resolution["incidentid"] = new EntityReference(dataCase.LogicalName, dataCase.Id);

                                            CloseIncidentRequest closecase = new CloseIncidentRequest();

                                            closecase.IncidentResolution = resolution;
                                            closecase.Status             = new OptionSetValue(5);

                                            CloseIncidentResponse closeresponse = (CloseIncidentResponse)service.Execute(closecase);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //throw new InvalidPluginExecutionException("Mensaje de error. ");
                }
            }
            catch (global::System.Exception)
            {
                throw;
            }
        }