public void TestIsValidStateTransition_FailsWhenStateCodeInvalid() { var man = new dg_man() { statecode = dg_manState.Active, statuscode = dg_man_statuscode.Active }; man.Id = orgAdminUIService.Create(man); var retrieved = orgAdminUIService.Retrieve(dg_man.EntityLogicalName, man.Id, new ColumnSet(true)) as dg_man; var request = new IsValidStateTransitionRequest { Entity = retrieved.ToEntityReference(), NewState = dg_manState.Active.ToString(), NewStatus = (int)dg_man_statuscode.Inactive }; try { orgAdminUIService.Execute(request); Assert.Fail(); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(FaultException)); } }
public void TestIsValidStateTransition_FailsWhenEnforceStateTransitionsFalse() { var field = new dg_field() { statecode = dg_fieldState.Active, statuscode = dg_field_statuscode.Active, }; field.Id = orgAdminUIService.Create(field); var retrieved = orgAdminUIService.Retrieve(dg_field.EntityLogicalName, field.Id, new ColumnSet(true)) as dg_field; var request = new IsValidStateTransitionRequest { Entity = retrieved.ToEntityReference(), NewState = dg_fieldState.Inactive.ToString(), NewStatus = (int)dg_field_statuscode.Inactive }; try { orgAdminUIService.Execute(request); Assert.Fail(); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(FaultException)); } }
public void TestIsValidStateTransition_FailsWhenNewStatusIsMissing() { var man = new dg_man() { statecode = dg_manState.Active, statuscode = dg_man_statuscode.Active }; man.Id = orgAdminUIService.Create(man); var retrieved = orgAdminUIService.Retrieve(dg_man.EntityLogicalName, man.Id, new ColumnSet(true)) as dg_man; var request = new IsValidStateTransitionRequest { Entity = retrieved.ToEntityReference(), NewState = dg_manState.Active.ToString() }; try { orgAdminUIService.Execute(request); throw new XunitException(); } catch (Exception e) { Assert.IsType <FaultException>(e); } }
/// <summary> /// Validates the state transition. /// <para> /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.isvalidstatetransitionrequest(v=crm.8).aspx /// </para> /// </summary> /// <param name="recordId">Record Id</param> /// <param name="entityLogicalName">Record's entity logical name</param> /// <param name="newStateCode"></param> /// <param name="newStatusCode"></param> /// <returns> /// Returns <c>true</c> if is valid. (<see cref="IsValidStateTransitionResponse.IsValid"/>) /// </returns> public bool Validate(Guid recordId, string entityLogicalName, string newStateCode, int newStatusCode) { ExceptionThrow.IfGuidEmpty(recordId, "id"); ExceptionThrow.IfNullOrEmpty(entityLogicalName, "entityLogicalName"); List <string> supportedEntityList = new List <string>() { "incident", "msdyn_postalbum", "msdyn_postconfig", "msdyn_postruleconfig", "msdyn_wallsavedquery", "msdyn_wallsavedqueryusersettings", "opportunity" }; if (!supportedEntityList.Contains(entityLogicalName.ToLower().Trim())) { ExceptionThrow.IfNotExpectedValue(entityLogicalName, "entityLogicalName", "", string.Format("{0} is not supported for this operation. For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.isvalidstatetransitionrequest(v=crm.8).aspx", entityLogicalName)); } IsValidStateTransitionRequest request = new IsValidStateTransitionRequest() { Entity = new EntityReference(entityLogicalName, recordId), NewState = newStateCode.Trim(), NewStatus = newStatusCode }; IsValidStateTransitionResponse response = (IsValidStateTransitionResponse)this.OrganizationService.Execute(request); return(response.IsValid); }
private static void CheckEnforceStateTransitions(IsValidStateTransitionRequest request, EntityMetadata entityMetadata, OptionMetadata stateOption) { if (entityMetadata.EnforceStateTransitions != true) { var tryState = stateOption != null?stateOption.Value.ToString() : "-1"; throw new FaultException($"This message can not be used to check the state transition of {request.Entity.LogicalName} to {tryState}"); } if (stateOption == null) { throw new FaultException($"-1 is not a valid state code on {request.Entity.LogicalName} with Id {request.Entity.Id}."); } }
private static void NotifyValidityOfIncidentSolvedStateChange(CrmServiceClient service) { // Validate the state transition. var isValidRequest = new IsValidStateTransitionRequest { Entity = new EntityReference(Incident.EntityLogicalName, _incidentId), NewState = IncidentState.Resolved.ToString(), NewStatus = (int)incident_statuscode.ProblemSolved }; var response = (IsValidStateTransitionResponse)service.Execute(isValidRequest); var isValidString = response.IsValid ? "is valid" : "is not valid"; Console.WriteLine(" The transition to a completed status reason {0}.", isValidString); }
public void TestIsValidStateTransition_FailsWhenEntityIsMissing() { var request = new IsValidStateTransitionRequest { NewState = dg_manState.Active.ToString(), NewStatus = (int)dg_man_statuscode.Active }; try { orgAdminUIService.Execute(request); Assert.Fail(); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(FaultException)); } }
public void TestIsValidStateTransition_FailsWhenEntityIsMissing() { var request = new IsValidStateTransitionRequest { NewState = dg_manState.Active.ToString(), NewStatus = (int)dg_man_statuscode.Active }; try { orgAdminUIService.Execute(request); throw new XunitException(); } catch (Exception e) { Assert.IsType <FaultException>(e); } }
public void TestIsValidStateTransition_FailsWhenRecordDoesNotExist() { var request = new IsValidStateTransitionRequest { Entity = new Microsoft.Xrm.Sdk.EntityReference(dg_man.EntityLogicalName, Guid.NewGuid()), NewState = dg_manState.Active.ToString(), NewStatus = (int)dg_man_statuscode.Active }; try { orgAdminUIService.Execute(request); Assert.Fail(); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(FaultException)); } }
public void TestIsValidStateTransition() { var man = new dg_man() { statecode = dg_manState.Active, statuscode = dg_man_statuscode.Active }; man.Id = orgAdminUIService.Create(man); var retrieved = orgAdminUIService.Retrieve(dg_man.EntityLogicalName, man.Id, new ColumnSet(true)) as dg_man; var request = new IsValidStateTransitionRequest { Entity = retrieved.ToEntityReference(), NewState = dg_manState.Inactive.ToString(), NewStatus = (int)dg_man_statuscode.Inactive }; var response = orgAdminUIService.Execute(request) as IsValidStateTransitionResponse; Assert.IsTrue(response.IsValid); }
[STAThread] // Required to support the interactive login experience static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { // Create any entity records that the demonstration code requires SetUpSample(service); #region Demonstrate // Create an EntityReference to represent an open case var caseReference = new EntityReference() { LogicalName = Incident.EntityLogicalName, Id = _caseIncidentId }; var checkState = new IsValidStateTransitionRequest(); // Set the transition request to an open case checkState.Entity = caseReference; // Check to see if a new state of "resolved" and // a new status of "problem solved" are valid checkState.NewState = IncidentState.Resolved.ToString(); checkState.NewStatus = (int)incident_statuscode.ProblemSolved; // Execute the request var checkStateResponse = (IsValidStateTransitionResponse)service.Execute(checkState); // Handle the response if (checkStateResponse.IsValid) { String changeAnswer = "y"; // default to "y" unless prompting for delete if (prompt) { // The case can be closed Console.WriteLine("Validate State Request returned that the case " + "can be closed."); Console.Write("\nDo you want to change the record state? " + "(y/n) [y]: "); changeAnswer = Console.ReadLine(); Console.WriteLine(); } if (changeAnswer.StartsWith("y") || changeAnswer.StartsWith("Y") || changeAnswer == String.Empty) { // Call function to change the incident to the closed state CloseIncident(service, caseReference); // Re-open the incident and change its state SetState(service, caseReference); } } else { // The case cannot be closed Console.WriteLine("Validate State Request returned that the " + "change is not valid."); } #endregion Demonstrate #region Clean up CleanUpSample(service); #endregion Clean up } else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Microsoft Dataverse"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// This method first connects to the Organization service. Afterwards, a /// case is created. The IsValidStateTransition is used to test if a state change /// is valid. The case is closed, re-opened and then closed with SetState. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //Create the Contact and Incident required for this sample. CreateRequiredRecords(); //<snippetIsValidStateTransition> // Create an EntityReference to represent an open case EntityReference caseReference = new EntityReference() { LogicalName = Incident.EntityLogicalName, Id = _caseIncidentId }; IsValidStateTransitionRequest checkState = new IsValidStateTransitionRequest(); // Set the transition request to an open case checkState.Entity = caseReference; // Check to see if a new state of "resolved" and // a new status of "problem solved" are valid checkState.NewState = IncidentState.Resolved.ToString(); checkState.NewStatus = (int)incident_statuscode.ProblemSolved; // Execute the request IsValidStateTransitionResponse checkStateResponse = (IsValidStateTransitionResponse)_serviceProxy.Execute(checkState); //</snippetIsValidStateTransition> // Handle the response if (checkStateResponse.IsValid) { String changeAnswer = "y"; // default to "y" unless prompting for delete if (promptForDelete) { // The case can be closed Console.WriteLine("Validate State Request returned that the case " + "can be closed."); Console.Write("\nDo you want to change the record state? " + "(y/n) [y]: "); changeAnswer = Console.ReadLine(); Console.WriteLine(); } if (changeAnswer.StartsWith("y") || changeAnswer.StartsWith("Y") || changeAnswer == String.Empty) { // Call function to change the incident to the closed state CloseIncident(caseReference); // Re-open the incident and change its state SetState(caseReference); } } else { // The case cannot be closed Console.WriteLine("Validate State Request returned that the " + "change is not valid."); } DeleteRequiredRecords(promptForDelete); } }
/// <summary> /// This method first connects to the Organization service. Afterwards, a /// case is created. The IsValidStateTransition is used to test if a state change /// is valid. The case is closed, re-opened and then closed with SetState. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //Create the Contact and Incident required for this sample. CreateRequiredRecords(); //<snippetIsValidStateTransition> // Create an EntityReference to represent an open case EntityReference caseReference = new EntityReference() { LogicalName = Incident.EntityLogicalName, Id = _caseIncidentId }; IsValidStateTransitionRequest checkState = new IsValidStateTransitionRequest(); // Set the transition request to an open case checkState.Entity = caseReference; // Check to see if a new state of "resolved" and // a new status of "problem solved" are valid checkState.NewState = IncidentState.Resolved.ToString(); checkState.NewStatus = (int)incident_statuscode.ProblemSolved; // Execute the request IsValidStateTransitionResponse checkStateResponse = (IsValidStateTransitionResponse)_serviceProxy.Execute(checkState); //</snippetIsValidStateTransition> // Handle the response if (checkStateResponse.IsValid) { String changeAnswer = "y"; // default to "y" unless prompting for delete if (promptForDelete) { // The case can be closed Console.WriteLine("Validate State Request returned that the case " + "can be closed."); Console.Write("\nDo you want to change the record state? " + "(y/n) [y]: "); changeAnswer = Console.ReadLine(); Console.WriteLine(); } if (changeAnswer.StartsWith("y") || changeAnswer.StartsWith("Y") || changeAnswer == String.Empty) { // Call function to change the incident to the closed state CloseIncident(caseReference); // Re-open the incident and change its state SetState(caseReference); } } else { // The case cannot be closed Console.WriteLine("Validate State Request returned that the " + "change is not valid."); } DeleteRequiredRecords(promptForDelete); } }
private void NotifyValidityOfIncidentSolvedStateChange() { //<snippetCloseAnIncident3> // Validate the state transition. var isValidRequest = new IsValidStateTransitionRequest { Entity = new EntityReference(Incident.EntityLogicalName, _incidentId), NewState = IncidentState.Resolved.ToString(), NewStatus = (int)incident_statuscode.ProblemSolved }; var response = (IsValidStateTransitionResponse)_serviceProxy.Execute(isValidRequest); var isValidString = response.IsValid ? "is valid" : "is not valid"; Console.WriteLine(" The transition to a completed status reason {0}.", isValidString); //</snippetCloseAnIncident3> }