Tuesday, 1 May 2012

Validating and Setting State of entity record in CRM 2011

In this article , i am going to explain how we will check entity State validation before changing it's state
if result come valid then we are able to change entity state and if result come not valid it's mean we are not able
to change the state of Entity record

In this example i am creating case entity record and checking It's validation of state and after that changing State

 using (_serviceProxy = new OrganizationServiceProxy(
        // This statement is required to enable early-bound type support.
              new ProxyTypesBehavior());

       Guid _ContactId;
       Guid _IncidentId;

       // Create a Contact
       Contact _contact = new Contact();
       _contact.FirstName = "Navish";
       _contact.LastName = "Jain";
       _ContactId = _serviceProxy.Create(_contact);

       // Retrieve the default subject for a new incident\case
       var query = new QueryExpression()
           EntityName = Subject.EntityLogicalName,
           ColumnSet = new ColumnSet(allColumns: true),
           Criteria = new FilterExpression()
        query.Criteria.AddCondition("title", ConditionOperator.Equal,
             "Default Subject");

        var subjectRecords = _serviceProxy.RetrieveMultiple(query);
        Entity defaultSubject = subjectRecords[0];
        NotifyEntityRetrieved(Subject.EntityLogicalName, defaultSubject.Id);

       // Create a new incident record
       Incident _incident = new Incident();
       _incident.SubjectId = new EntityReference();
       _incident.SubjectId.LogicalName = Subject.EntityLogicalName;
       _incident.SubjectId.Id = defaultSubject.Id;
       _incident.CustomerId = new EntityReference();
       _incident.CustomerId.LogicalName = Contact.EntityLogicalName;
       _incident.CustomerId.Id = _ContactId;
       _incident.Title = "Incident record to check Valid State";
       _IncidentId = _serviceProxy.Create(_incident);

      IsValidStateTransitionRequest _isValidState = new IsValidStateTransitionRequest();
      _isValidState.Entity = new EntityReference
        LogicalName = Incident.EntityLogicalName,
        Id = _IncidentId
      _isValidState.NewState = IncidentState.Resolved.ToString();
      _isValidState.NewStatus = 5;
      IsValidStateTransitionResponse ValidStateResponse = (IsValidStateTransitionResponse)_serviceProxy.Execute(_isValidState);

      if (ValidStateResponse.IsValid)
         IncidentResolution resolution = new IncidentResolution();
         resolution.Subject = "Close Case Record";
         resolution.IncidentId = new EntityReference  { LogicalName = Incident.EntityLogicalName, Id = _IncidentId };

         // Create the request to close the incident
         CloseIncidentRequest closeRequest = new CloseIncidentRequest();
         closeRequest.IncidentResolution = resolution;
         closeRequest.Status = new OptionSetValue(5);
         CloseIncidentResponse closeResponse = (CloseIncidentResponse)_serviceProxy.Execute(closeRequest);

         // Re-open the incident
         SetStateRequest state = new SetStateRequest();
         state.State = new OptionSetValue((int)IncidentState.Active);
         state.Status = new OptionSetValue(3);
         state.EntityMoniker = new EntityReference
             LogicalName = Incident.EntityLogicalName,
             Id = _IncidentId
         SetStateResponse stateSet = (SetStateResponse)_serviceProxy.Execute(state);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.