Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Help with Case resolution Javascript that's not working

Posted on by 2,095

Hi all,

I'm needing some help with this custom ribbon button that I've added to resolve cases without the pop out dialogue appearing. Everything is working fine until I got a new requirement to prevent cases from resolving in the form area if the case is still assigned to the default service account user. The validation to check if the case owner equals service account is easy and I managed to use the openAlertDialog (https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openalertdialog) to notify them that they need to assign the case to themselves first.

I then had another idea for when they click the yes button to automatically assign the case to themselves and then resolve it in one go but after adding that logic into the code when I press the resolve ribbon button nothing happens if the case is assigned to the default service account user.

Here's the code snippet below.

//function to close cases
function closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode, formContext) {
    return new Promise((resolve, reject) => {
        // Incident Resolution properties
        var incidentresolution = {
            "subject": subject,
            "incidentid@odata.bind": "/incidents("   caseId   ")", //Id of incident
            "timespent": billableHours, //This is billable time in minutes
            "description": resolutionDescription
        };

        var parameters = {
            "IncidentResolution": incidentresolution,
            "Status": resolutionCode
        };

        var context;

        if (typeof GetGlobalContext === "function") {
            context = GetGlobalContext();
        } else {
            context = Xrm.Page.context;
        }

        var req = new XMLHttpRequest();

        req.open("POST", context.getClientUrl()   "/api/data/v9.1/CloseIncident", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success 
                        // Reload and save the form so that it displays the case as resolved
                        formContext.data.refresh(true).then(resolve('Done'));


                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                    reject('Failed')
                }
            }
        };
        req.send(JSON.stringify(parameters));
    });



}


// Resolve case without dialogue box
async function quickResolveTrigger(PrimaryItemIds, PrimaryControl) {
    //Grab the record ID
    var caseId = PrimaryItemIds[0].slice(1, -1);
    //Store the incident resolution fields required to resolve a case in variables
    var subject = 'Resolved'
    var billableHours = 60;
    var resolutionDescription = 'Resolved';
    var resolutionCode = 5;

    //Create the formContext variable
    var formContext = PrimaryControl;

    var formItem = formContext.ui.formSelector.getCurrentItem();
    var formLabel = formItem.getLabel();
    var ownerId = formContext.getAttribute('ownerid').getValue()[0].id.slice(1, -1);

        if (formLabel === 'Case for Interactive experience' && ownerId === 'D8CF7E4E-E236-E811-A83F-000D3A00E7A4') {

            var alertStrings = {
                confirmButtonLabel: "Yes",
                text: 'Assign case to yourself and resolve?'
            };
            var alertOptions = {
                height: 120,
                width: 260
            };
 // --------------------------------------------------------------This is the portion of the code that just isn't working ---------------------------START           
            Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
                function success() {
                    var globalContext = Xrm.Utility.getGlobalContext();
                    var currentUser = new Array();
                    currentUser[0] = new Object();
                    currentUser[0].entityType = "systemuser";
                    currentUser[0].id = globalContext.userSettings.userId;
                    currentUser[0].name = globalContext.userSettings.userName;
                    formContext.getAttribute("ownerid").setValue(currentUser);
                    Xrm.Utility.showProgressIndicator('Resolving'); // Opens the resolving progres indicator to lock UI until case is resolved
                    formContext.data.save().then(
                        function success(subject, caseId, billableHours, resolutionDescription, resolutionCode, formContext, buttonContext) {
                            // perform operations on refresh
                            await closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode, formContext);
                        },
                        function (error) {
                            console.log(error.message);
                            // handle error conditions
                        }
                    );
                },
                function (error) {
                    console.log(error.message);
                    // handle error conditions
                });
//-------------------------------------------------------------------------END--------------------------------------------------------------------------------

        } else if (formContext.data.getIsDirty()) {

            Xrm.Utility.showProgressIndicator('Resolving');
            // Save the form first. In case someone makes some changes before clicking the Quick Resolve. That way the changes are saved before the case is resolved.
            await formContext.data.save();

            //Call the close incident function and pass across the required parameters
            closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode, formContext);

        } else {
            Xrm.Utility.showProgressIndicator('Resolving');

            //Call the close incident function and pass across the required parameters
            await closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode, formContext);
        }

        


    // Closes the progress indicator on all occasions once above async function calls resolve
    Xrm.Utility.closeProgressIndicator();


}

So the quickResolveTrigger function is what's called when you click the ribbon button to resolve the case. Everything works except when I'm testing for when the case is assigned to the default user. It's only when I add in the function to resolve the case does clicking the resolve button have nothing happen. Not even a error on the console. 

Any help would be greatly appreciated.

Mike

  • Verified answer
    mobeenmohsin Profile Picture
    mobeenmohsin on at
    RE: Help with Case resolution Javascript that's not working

    Hi Mike,

    Please help to debug your code specially the success case for openAlertDialog and confirm which specific line is causing this issue, have you tried to call the closeIncident function directly in success case of openAlertDialog rather then in save success (formContext.data.save()). Please try to directly call the closeIncident function and see if that is the issue. If you still unable to find the reason then we request you to raise support request with Microsoft product team using https://admin.powerplatform.microsoft.com/support.

    Thank you for understanding.

    Best Regards,

    Mobeen

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans