Validate Record state using IsValidStateTransitionRequest in Dynamics CRM

By | April 28, 2016

Introduction:

Suppose we are going to change the state of an opportunity record programmatically by using WinOpportunityRequest request, before we make change to the state of the record it is quite important to check, the transition which we are going to execute is valid or not.

For example suppose we are going to change the state of opportunity record as won using WinOpportunityRequest request. In this scenario it is important to check whether this opportunity is in the open state or not, because only the opportunity having state as open can be changed as won.

On the other hand, suppose that the selected opportunity is already in won or lost state and we are going to change the state of the same opportunity as won then we get the following error.

The opportunity is already closed.

To overcome from the above error we can use “IsValidStateTransitionRequest”.

IsValidStateTransitionRequest request validates the transition which we are going to execute and in response returns isValid as true/false.

IsValidStateTransitionRequest request takes the following parameters as input.

  • Entity: This is an entity reference of the entity record, whose state we are going to change.
  • NewState: This parameter takes the value of the entity state which we are going to set, in the string format as shown in below sample code.
  • NewStatus: This parameter take the value of the entity status which we are going to set, in the integer format as shown in the below sample code.

Below is the sample code to execute “IsValidStateTransitionRequest” request.

//this the entity reference of the opportunity record which state we are going to change.
EntityReference opportunityRecord = new EntityReference("opportunity", new Guid("12363FDD-EDF7-E511-80EA-3863BB361EA0"));

//create IsValidStateTransitionRequest request
IsValidStateTransitionRequest checkRecordState = new IsValidStateTransitionRequest();

//Initialize the entity parameter.This is an entity reference of the entity record, whose state we are going to change.
checkRecordState.Entity = opportunityRecord;

//Initialize the NewState parameter.This parameter takes the value of the entity state which we are going to set. 
checkRecordState.NewState = "Won";

//initialize the NewStatus Parameter.This parameter take the value of the entity status which we are going to set.
checkRecordState.NewStatus = 3; //Status value for won
//Execute the request.
 IsValidStateTransitionResponse checkStateResponse = (IsValidStateTransitionResponse)_service.Execute(checkRecordState);

             
//Check the response, response have a property IsValid,this specify the transition which
//we are going to execute is valid or not.
//Check if transition is valid then execute the WinOpportunityRequest request.
if(checkStateResponse.IsValid)
 {
   //Add the logic here to change the opportunity state as won
    WonOpportunityRequest(opportunityRecord); 
 }

Conclusion:

This will gives you the extra validation before execute the state change request.

Now Clone your Dynamics CRM records in just 1 click with Click2Clone.

Leave a Reply

Your email address will not be published. Required fields are marked *