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

Community site session details

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

How to update or set status reason field using java script on case entity.

(0) ShareShare
ReportReport
Posted on by 342

I want to update status reason field value from "closed" to "problem solved" on Case form how can i achieve this using java script code.

I have the same question (0)
  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at
    RE: How to update or set status reason field using java script on case entity.

    Hi Sandy,

    Could you let me know that whether "closed" is a new customization option that you added to Status Reason field of Case entity?

    I didn't find an existing case status called "closed" from definition of Case entity, only found "problem solved".

    pastedimage1589250463466v1.png

    You can run code below to close a case.(resolve a case by "CloseIncident" action)

    var guid = Xrm.Page.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();
    
    var incidentResolution = 
    {
        "subject": "The case has been resolved.",
        "incidentid@odata.bind": "/incidents("   guid   ")", 
        "timespent": 60, 
        "description": "Additional description."
    }
    
    var CloseIncidentRequest = function (incidentResolution, status) {
        this.IncidentResolution = incidentResolution;
        this.Status = status;
        this.getMetadata = function () {
            return {
                boundParameter: null,
                parameterTypes: {
                    "Status": {
                        "typeName": "Edm.String", //Primitive Type
                        "structuralProperty": 1
                    },
                    "IncidentResolution": {
                        "typeName": "mscrm.incidentresolution", //Entity Type
                        "structuralProperty": 5
    
                    }
                },
                operationType: 0,
                operationName: "CloseIncident"
            };
        };
    };
    
    var closeIncidentRequest = new CloseIncidentRequest(incidentResolution, 5);
    
    Xrm.WebApi.online.execute(closeIncidentRequest).then(
        function (result) {
            if (result.ok) {
                console.log("Case has been resolved by code.");
            }
        },
        function (error) {
            console.log(error);
        }
    );

    You can run code below to reopen a case.(reopen a case by updating its status directly)

    var id = Xrm.Page.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();
    
    var etn = Xrm.Page.data.entity.getEntityName();
    
    var reOpenData =
    {
        "statecode": 0,
        "statuscode": 1
    }
    
    Xrm.WebApi.updateRecord(etn, id, reOpenData).then(
        function success(result) {
            console.log("Case is reopened again.");
        },
        function (error) {
            console.log(error.message);
        }
    );

    Regards,

    Clofly

  • Sandy Hello Profile Picture
    342 on at
    RE: How to update or set status reason field using java script on case entity.

    Thanks Clofly for your response. Yes "closed" is the custom value added to the status reason. My requirement is when statusreason is "closed" and some other filelds on form or set to "yes" then the status reason is set to "problem solved".

    Below code is not working

    Xrm.Page.getAttribute("statuscode").setValue(5); is not working as expected

  • cloflyMao Profile Picture
    25,210 on at
    RE: How to update or set status reason field using java script on case entity.

    Hi Sandy,

    Because when a case is set to "Problem Solved"(resolve a case), an associated entity record "incidentresolution" will be also created.

    Steps above could be regarded as a whole process, so we couldn't simply set statuscode to 5 to close the case.

    If I set statecode to 1(Resolved) and set statuscode to 5(Problem Solved), then a dialog with error message will be opened, it will tell us to close case with CloseIncidentRequest class.

    pastedimage1589278067112v1.png

    So instead, we should close case by CloseIncident action(the javascript version of CloseIncidentRequest), after we run the action for a case, then the statuscode of the case will be set to "Problem Solved"(5) automatically.(and an incidentresolution entity record will be created at same time)

    You could use Xrm.WebApi.online.execute or create an AJAX request to the action directly.

    Xrm.WebApi.online.execute method is my first code.

    Or take article below as reference if you prefer to the second method:

    http://butenko.pro/2017/03/20/close-incident-using-webapi/

    In a word, run the action when statusreason is "closed" and some other filelds on form are set to "yes".

    Regards,

    Clofly

  • Abigail Profile Picture
    552 on at
    RE: How to update or set status reason field using java script on case entity.

    You can modify the status reason field options for the Case entity and custom entities to define which other status reason options people can choose. The only restriction is that each status reason option for an active status must allow at least one path to an inactive status. Otherwise you could create a condition where it would not be possible to resolve or cancel the case.

    When you edit a status reason field the Edit Status Reason Transitions button is in the menu. When you click this button the Status Reason Transitions dialog provides the option to choose Enable Status Reason Transitions. When this option is selected you must define which status reason values are allowed for each status reason. To remove the filtering applied, remove the Enable Status Reason Transitions selection. The transitions you have defined will be kept but not applied.

    I hope this helps!

  • cloflyMao Profile Picture
    25,210 on at
    RE: How to update or set status reason field using java script on case entity.

    Hi Sandy,

    Could it work for you?

    Regards,

    Clofly

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans