private CrmClient GetCrmClientFormCustCode(string custCode) { CrmClient client = null; var ctx = DcrmConnectorFactory.GetContext(); var partyQuery = from party in ctx.CreateQuery("account") join client_facturation in ctx.CreateQuery("crm_clientdefacturation") on party["accountid"] equals client_facturation["crm_titulaireid"] join contrat in ctx.CreateQuery("crm_contrat") on client_facturation["crm_titulaireid"] equals contrat["crm_account_id"] where client_facturation["crm_custcode"].Equals(custCode) select new CrmClient { AccountId = (string)party["accountid"].ToString(), CustCode = (string)client_facturation["crm_custcode"], CustomerId = (string)client_facturation["crm_customer_id"], Contrat = (string)contrat["crm_coid"], Msisdn = (string)contrat["crm_msisdn"] }; try { client = partyQuery.First(); } catch (Exception Ex) { MiscHelper.PrintMessage($"GetCrmClientFormCustCode : {Ex.Message}"); } return(client); }
private string GetGuidFromEntity(string entityName, string iDLookupKey, string entityLookupKey, string entityLookupValue) { var ctx = DcrmConnectorFactory.GetContext(); var entityQuery = from entity in ctx.CreateQuery(entityName) where entity[entityLookupKey].Equals(entityLookupValue) select new { Guid = GetEntityValue(entity, iDLookupKey) }; var guid = ""; try { var entity = entityQuery.First(); if (entity != null) { guid = entity.Guid.ToString(); } } catch (Exception Ex) { MiscHelper.PrintMessage($"GetGuidFromEntity : {Ex.Message}"); } return(guid); }
private string GetCustomerIdFromIncident(string incidentId) { var ctx = DcrmConnectorFactory.GetContext(); var entityQuery = from entity in ctx.CreateQuery("incident") where entity["incidentid"].Equals(incidentId) select new { Guid = (EntityReference)entity["customerid"] }; var guid = ""; try { var entity = entityQuery.First(); if (entity != null) { EntityReference reference = (EntityReference)entity.Guid; guid = reference.Id.ToString(); } } catch (Exception Ex) { MiscHelper.WriteLine($"GetGuidFromEntity : {Ex.Message}"); } return(guid); }
public async Task <int> UpdateRelatedIncidentsAsync(Guid incidentId, Guid newAccountId) { var ctx = DcrmConnectorFactory.GetContext(); var oldAccountId = GetCustomerIdFromIncident(incidentId.ToString()); var entityQuery = from incident in ctx.CreateQuery("incident") where incident["customerid"].Equals(oldAccountId) where incident["statecode"].Equals(0) select new { Guid = GetIncidentId(incident) }; var relatedIncidentsCount = 0; MiscHelper.WriteLine("Querying DCRM Please Wait..."); try { await Task <uint> .Run(() => { var multipleRequest = new ExecuteMultipleRequest() { Settings = new ExecuteMultipleSettings() { ContinueOnError = false, ReturnResponses = true }, Requests = new OrganizationRequestCollection() }; foreach (var guid in entityQuery) { var updateRequest = UpdateIncident(guid.Guid, newAccountId); multipleRequest.Requests.Add(updateRequest); relatedIncidentsCount++; } ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)ctx.Execute(multipleRequest); foreach (var responseItem in multipleResponse.Responses) { // An error has occurred. if (responseItem.Fault != null) { MiscHelper.WriteLine($"{multipleResponse.Responses[responseItem.RequestIndex]} : {responseItem.Fault}"); } } }); } catch (Exception ex) { MiscHelper.WriteLine($"GetRelatedIncidentsAsync() : {ex.Message}"); } return(relatedIncidentsCount); }
private UpdateRequest UpdateIncident(Guid incidentId, Guid accountID) { var ctx = DcrmConnectorFactory.GetContext(); var account = new EntityReference(Account.EntityLogicalName, accountID); var incident = new Entity(Incident.EntityLogicalName, incidentId); incident["customerid"] = account; UpdateRequest updateRequest = new UpdateRequest { Target = incident }; return(updateRequest); }
public async Task <bool> Resolve(int currentIndex, int maxCount, Guid incidentId, bool isPrintingProgress) { bool isOperationSuccessfull = false; var ctx = DcrmConnectorFactory.GetContext(); var dcrmConnector = DcrmConnectorFactory.Get(); var srv = dcrmConnector.GetService(); var incident = new EntityReference(Incident.EntityLogicalName, incidentId); IncidentResolution incidentResolution = new IncidentResolution { IncidentId = incident, StatusCode = new OptionSetValue(5) }; CloseIncidentRequest closeIncidentReq = new CloseIncidentRequest() { IncidentResolution = incidentResolution, Status = new OptionSetValue(5) }; try { ctx.Execute(closeIncidentReq); if (isPrintingProgress) { MiscHelper.DisplayProgression(maxCount); } isOperationSuccessfull = true; } catch (Exception ex) { var message = string.Format($"Could not close the incident : {incidentId} : {ex.Message} "); MiscHelper.PrintMessage(message); } return(isOperationSuccessfull); }
public async Task <bool> CreateIncident(int currentIndex, int maxCount, bool isCallingResolve) { var isOperationSuccessfull = false; var ctx = DcrmConnectorFactory.GetContext(); var dcrmConnector = DcrmConnectorFactory.Get(); var srv = dcrmConnector.GetService(); Guid incidentId = Guid.Empty; var createIncidentTimer = new MiscHelper(); createIncidentTimer.StartTimeWatch(); var accountId = this.IncidentWriteDict[HEADER_ACCOUNT_ID]; if (string.IsNullOrEmpty(accountId)) { var accountNumber = this.IncidentWriteDict[HEADER_CUSTOMER_ID]; MiscHelper.PrintMessage($"GUID for account : {accountNumber} won't be created"); return(isOperationSuccessfull); } var targetIncident = new Entity("incident"); targetIncident["customerid"] = new EntityReference(Account.EntityLogicalName, new Guid(accountId)); targetIncident["crm_custcode"] = this.IncidentWriteDict[HEADER_CUSTCODE]; targetIncident["crm_customer_id"] = this.IncidentWriteDict[HEADER_CUSTOMER_ID]; targetIncident["crm_contrat"] = this.IncidentWriteDict[HEADER_CONTRAT]; targetIncident["crm_numero_ligne_concernee"] = this.IncidentWriteDict[HEADER_MSISDN]; targetIncident["title"] = this.IncidentWriteDict[HEADER_TITRE]; targetIncident["description"] = this.IncidentWriteDict[HEADER_DESCRIPTION]; var guidTheme1 = new Guid(this.IncidentWriteDict[GUID_THEME1]); var theme1Entity = new EntityReference("crm_themeniveau1", guidTheme1); targetIncident["crm_theme_niveau_1_id"] = theme1Entity; var guidTheme2 = new Guid(this.IncidentWriteDict[GUID_THEME2]); var theme2Entity = new EntityReference("crm_themeniveau2", guidTheme2); targetIncident["crm_theme_niveau_2_id"] = theme2Entity; var guidTheme3 = new Guid(this.IncidentWriteDict[GUID_THEME3]); var theme3Entity = new EntityReference("crm_themeniveau3", guidTheme3); targetIncident["crm_theme_niveau_3_id"] = theme3Entity; var teamId = new Guid(this.IncidentWriteDict[HEADER_OWNER]); targetIncident["ownerid"] = new EntityReference(Team.EntityLogicalName, teamId); await Task <bool> .Run(() => { incidentId = srv.Create(targetIncident); if (isCallingResolve) { isOperationSuccessfull = Resolve(currentIndex, maxCount, incidentId, false).Result; } MiscHelper.DisplayProgression(maxCount); }); createIncidentTimer.StopTimeWatch(); return(isOperationSuccessfull); }
public async Task <int> ProcessImportAsync() { var entriesCpt = 0; using (var dataReadStream = File.OpenRead(InputFilePath)) using (var dataReader = new StreamReader(dataReadStream)) using (var countReadStream = File.OpenRead(InputFilePath)) using (var countReader = new StreamReader(countReadStream)) { // parsing .csv input file var dataRead = CsvParser.ParseHeadAndTail(dataReader, ';', '"'); var countRead = CsvParser.ParseHeadAndTail(countReader, ';', '"'); var dataQuery = dataRead.Item2; var countQuery = countRead.Item2; MiscHelper.PrintMessage("Counting the number of incidents to import..."); var incidentCountTimer = new MiscHelper(); incidentCountTimer.StartTimeWatch(); var maxCount = countQuery.Count(); incidentCountTimer.StopTimeWatch(); MiscHelper.PrintMessage($"{maxCount} were scanned in {incidentCountTimer.GetDuration()}"); MiscHelper.PrintMessage("Connecting do DCRM..."); DcrmConnectorFactory.GetContext(); var dataHeader = dataRead.Item1; var headerLine = GetHeaderLine(dataHeader); var createIncidentTasks = new List <Task <bool> >(); var ImportTimer = new MiscHelper(); ImportTimer.StartTimeWatch(); MiscHelper.PrintMessage($"\nProcessing the {maxCount} incidents, please wait ..."); // read every lines of the .csv source file and translate them into the .csv output file foreach (var line in dataQuery) { if (line.Count > 0) { ++entriesCpt; var demande = GetDemandeFromLine(dataHeader, line); try { demande.Process(); } catch (Exception ex) { var innerMessage = ex.InnerException != null ? ex.InnerException.Message : string.Empty; MiscHelper.PrintMessage($"\nDemandeCreationException ({entriesCpt}] : {ex.Message} {innerMessage}"); } demande.AddCreateIncidentWorkerTask(createIncidentTasks, entriesCpt, maxCount); } } // We need to wait for all tasks to be terminated await System.Threading.Tasks.Task.WhenAll(createIncidentTasks); MiscHelper.PrintMessage("\nAll The operation completed successfully"); ImportTimer.StopTimeWatch(); MiscHelper.PrintMessage($"\n\n{entriesCpt} incidents were created in {ImportTimer.GetDuration()}"); } return(entriesCpt); }
public async Task <int> ProcessResolveAsync() { var entriesCpt = 0; using (var dataReadStream = File.OpenRead(InputFilePath)) using (var dataReader = new StreamReader(dataReadStream)) using (var countReadStream = File.OpenRead(InputFilePath)) using (var countReader = new StreamReader(countReadStream)) { // parsing .csv input file var dataRead = CsvParser.ParseHeadAndTail(dataReader, ';', '"'); var countRead = CsvParser.ParseHeadAndTail(countReader, ';', '"'); var countQuery = countRead.Item2; var dataQuery = dataRead.Item2; MiscHelper.PrintMessage("Counting the number of incident to resolve..."); var incidentCountTimer = new MiscHelper(); incidentCountTimer.StartTimeWatch(); var maxCount = countQuery.Count(); incidentCountTimer.StopTimeWatch(); MiscHelper.PrintMessage($"\n\n{maxCount} were scanned in {incidentCountTimer.GetDuration()}"); MiscHelper.PrintMessage("Connecting do DCRM..."); // Calling GetContext in order to provoke a authentication to DCRM before we actually needs it DcrmConnectorFactory.GetContext(); var dataHeader = dataRead.Item1; // writting header to .csv output file var headerLine = GetHeaderLine(dataHeader); var resolvencidentThreads = new List <Thread>(); var ResolveTimer = new MiscHelper(); ResolveTimer.StartTimeWatch(); var resolveIncidentTasks = new List <Task <bool> >(); // read every lines of the .csv source file and translate them into the .csv output file foreach (var line in dataQuery) { if (line.Count > 0) { var demande = GetDemandeFromLine(dataHeader, line); var guidDemande = demande.GetGuid(); demande.AddResolveIncidentWorkerTask(resolveIncidentTasks, guidDemande, ++entriesCpt, maxCount); } } // We need to wait for all tasks to be terminated MiscHelper.PrintMessage($"\nProcessing the {maxCount} incidents, please wait ..."); await System.Threading.Tasks.Task.WhenAll(resolveIncidentTasks); MiscHelper.PrintMessage("\nAll The operation completed successfully"); ResolveTimer.StopTimeWatch(); MiscHelper.PrintMessage($"\n\n{entriesCpt} incidents were resolved in {ResolveTimer.GetDuration()}"); } return(entriesCpt); }