Pages

Tuesday, 1 May 2012

Create , Validate and Close Incident and Appointment in CRM 2011


In this article , I am going to explain how to Create , Validate and Close Incident and Appointment.

For Better Understanding I divided this article in multiple part
(i)      Create an incident
(ii)     Create an appointment
(iii)    Validate the Appointment state transition
(iv)    Close the appointment
(v)     Validate the Incident state transition.
(vi)    Create the incident's resolution and Close the incident with the resolution.

Namespace need to include 

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;


Code to Create an incident

var incident = new Incident
{
    CustomerId = new EntityReference(Account.EntityLogicalName, _accountId),
    Title = "Sample Incident"
};
Guid _incidentId = _serviceProxy.Create(incident);


Code to  Create an appointment

var appointment = new Appointment
{
    ScheduledStart = DateTime.Now,
    ScheduledEnd = DateTime.Now.Add(new TimeSpan(0, 30, 0)),
    Subject = "Sample 30-minute Appointment",
    RegardingObjectId = new EntityReference(Incident.EntityLogicalName,
        _incidentId)
};
Guid _appointmentId = _serviceProxy.Create(appointment);


Code to Validate the Appointment state transition

var isAppointmentValidRequest = new IsValidStateTransitionRequest
{
    Entity = new EntityReference(Appointment.EntityLogicalName, _incidentId),
    NewState = "1", //Completed
    NewStatus = 3 //Completed
};
var isAppointmentValidResponse =
    (IsValidStateTransitionResponse)_serviceProxy.Execute(isAppointmentValidRequest);


Code to  Close the appointment

var setAppointmentStateReq = new SetStateRequest
{
    EntityMoniker = new EntityReference(Appointment.EntityLogicalName,
        _appointmentId),
    State = new OptionSetValue(1), //Completed
    Status = new OptionSetValue(3) //Completed
};

_serviceProxy.Execute(setAppointmentStateReq);


Code to Validate the Incident state transition.

var isIncidentValidRequest = new IsValidStateTransitionRequest
{
    Entity = new EntityReference(Incident.EntityLogicalName, _incidentId),
    NewState = "1", //Resolver
    NewStatus =5 //ProblemSolved
};
var isIncidentValidResponse =
    (IsValidStateTransitionResponse)_serviceProxy.Execute(isIncidentValidRequest);


Code to  Create the incident's resolution and Close the incident with the resolution

var incidentResolution = new IncidentResolution
{
    Subject = "Resolved Sample Incident",
    IncidentId = new EntityReference(Incident.EntityLogicalName, _incidentId)
};

// Close the incident with the resolution.
var closeIncidentRequest = new CloseIncidentRequest
{
    IncidentResolution = incidentResolution,
    Status = new OptionSetValue(5) //ProblemSolved
};
_serviceProxy.Execute(closeIncidentRequest);

Complete Source Code

using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(
    serverConfig.OrganizationUri,
    serverConfig.HomeRealmUri,
    serverConfig.Credentials,
    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(
        new ProxyTypesBehavior());
    Guid _accountId = new Guid("ffbb6e3f-ccfe-4d84-90d9-421418b03a8e");

    // Create an incident.
    var incident = new Incident
    {
        CustomerId = new EntityReference(Account.EntityLogicalName, _accountId),
        Title = "Sample Incident"
    };
    Guid _incidentId = _serviceProxy.Create(incident);


    // Create a 30-minute appointment regarding the incident.
    var appointment = new Appointment
    {
        ScheduledStart = DateTime.Now,
        ScheduledEnd = DateTime.Now.Add(new TimeSpan(0, 30, 0)),
        Subject = "Sample 30-minute Appointment",
        RegardingObjectId = new EntityReference(Incident.EntityLogicalName,
            _incidentId)
    };
    Guid _appointmentId = _serviceProxy.Create(appointment);


    // Validate the Appointment state transition.
    var isAppointmentValidRequest = new IsValidStateTransitionRequest
    {
        Entity = new EntityReference(Appointment.EntityLogicalName, _incidentId),
        NewState = "1", //Completed
        NewStatus = 3 //Completed
    };
    var isAppointmentValidResponse =
        (IsValidStateTransitionResponse)_serviceProxy.Execute(isAppointmentValidRequest);


    if (isAppointmentValidResponse.IsValid)
    {
        // Close the appointment.
        var setAppointmentStateReq = new SetStateRequest
        {
            EntityMoniker = new EntityReference(Appointment.EntityLogicalName,
                _appointmentId),
            State = new OptionSetValue(1), //Completed
            Status = new OptionSetValue(3) //Completed
        };

        _serviceProxy.Execute(setAppointmentStateReq);
    }


    // Validate the Incident state transition.
    var isIncidentValidRequest = new IsValidStateTransitionRequest
    {
        Entity = new EntityReference(Incident.EntityLogicalName, _incidentId),
        NewState = "1", //Resolver
        NewStatus =5 //ProblemSolved
    };
    var isIncidentValidResponse =
        (IsValidStateTransitionResponse)_serviceProxy.Execute(isIncidentValidRequest);


    if (isIncidentValidResponse.IsValid)
    {
        // Create the incident's resolution.
        var incidentResolution = new IncidentResolution
        {
            Subject = "Resolved Sample Incident",
            IncidentId = new EntityReference(Incident.EntityLogicalName, _incidentId)
        };

        // Close the incident with the resolution.
        var closeIncidentRequest = new CloseIncidentRequest
        {
            IncidentResolution = incidentResolution,
            Status = new OptionSetValue(5) //ProblemSolved
        };
        _serviceProxy.Execute(closeIncidentRequest);
    }

}



1 comment:

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