Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Closing an incident in Dynamics 365 using the API

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? 

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Closing an incident in Dynamics 365 using the API

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans