web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Closing an incident in Dynamics 365 using the API

(0) ShareShare
ReportReport
Posted on by 15

I need some help over how to update and implement this code on closing an incident that is on Microsoft documentation. 

The code for closing case can be found on https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.closeincidentrequest?view=dynamics-general-ce-9

I am creating an incident using this code - 

           

 private IOrganizationService m_OrgServ = null;

m_OrgServ = DynamicsAccessUtils.getDynamicsCon();

public void Case_entity()

{

Entity incident = new Entity("incident");

incident.Attributes["ibm_securityclassificationselector"] = new EntityReference("ibm_securityclassification", new Guid("00E0E87CA93AE911AA0B067E3E789E92"));

incident.Attributes["ibm_region_picker"] = new EntityReference("ibm_region", new Guid("27DB6B08EE7FEA11AA7E067E3E789E92"));

incident.Attributes.Add("ibm_callername", "hh");

incident.Attributes.Add("ibm_callerphone", "12345");

incident.Attributes["customerid"] = new EntityReference("account", new Guid("E6E9E0652CFEE811A9E9067E3E789E92"));

 

Guid caseId = m_OrgServ.Create(incident);

 

}

 In the Dynamics UI , you click a checkbox close case (close incident)

There is a mandatory optionset field , from which I want to choose the 3/4 option (Name of option - No Action)

There is a mandatory textbox , to fill in justification 

so can I said what I choose for these close incident fields over API and be able to close the case after I have created it, after that create incident code I have already? 

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    see below C# code to close incident.

    // 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((int)incident_statuscode.ProblemSolved)
                };
    
                _serviceProxy.Execute(closeIncidentRequest);

    As you can see first we have to create incident resolution entity record and then use CloseIncident Action from web API.

    To close incident from Web API, Create Incident Resolution entity record. See below WEB API code. 

    Please note I have just mapped subject field but you can map other fields as well based on your requirement.

    var entity = {};
    entity.subject = "Problem Solved";
    entity["incidentid@odata.bind"] = "/incidents()";
    
    Xrm.WebApi.online.createRecord("incidentresolution", entity).then(
        function success(result) {
            var newEntityId = result.id;
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
    .

    Store record ID in some variable. We will use this in next web api call.

    Now we have to execute CloseIncident action from web api. see below web api code.

    var parameters = {};
    var incidentresolution = {};
    incidentresolution.activityid = "00000000-0000-0000-0000-000000000000"; //Pass entity ID from previous web api response 
    incidentresolution["@odata.type"] = "Microsoft.Dynamics.CRM.incidentresolution";
    parameters.IncidentResolution = incidentresolution;
    parameters.Status = 0;
    
    var closeIncidentRequest = {
        IncidentResolution: parameters.IncidentResolution,
        Status: parameters.Status,
    
        getMetadata: function() {
            return {
                boundParameter: null,
                parameterTypes: {
                    "IncidentResolution": {
                        "typeName": "mscrm.incidentresolution",
                        "structuralProperty": 5
                    },
                    "Status": {
                        "typeName": "Edm.Int32",
                        "structuralProperty": 1
                    }
                },
                operationType: 0,
                operationName: "CloseIncident"
            };
        }
    };
    
    Xrm.WebApi.online.execute(closeIncidentRequest).then(
        function success(result) {
            if (result.ok) {
                //Success - No Return Data - Do Something
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );

    If found helpful, Please mark my answer verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans