Skip to main content

Notifications

Announcements

No record found.

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

Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

Posted on by 2,095

Hi all,

So I've been trying to via Javascript and the Ribbon Workbench create a quick resolve button for a case without the case resolution dialogue popping up. I've created two web resource scripts:

1.  One to act as a trigger to call the actual web resource JS function that closes the incident/case. This function will call my second function and pass along as parameters the case ID and all the incident resolution details.

2. The actual function that performs the closeincident action. It will take the parameter values passed through the 1st web resource script and perform the close incident function.

I built it this way in case I want to reuse the close incident function somewhere else. For example have a button on a email that allows a user to send the email and then close the case.

Thanks to Andrew Butenko for his post on how to do this using Javascript

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

Below is the breakdown of each script:

1 Script to trigger the actual close incident function

function triggerCloseIncident (executionContext) {
    var formContext = executionContext.getFormContext();
    // Get the case ID so that I can pass it as a parameter to the actual close incident function
    var caseIdRaw = formContext.data.entity.getId();
    var caseId = caseIdRaw.slice(1, -1);
    // Create variables for the incident resolution dialogue as well as the case status
    var subject = 'quick resolve'
    var billableHours = 60;
    var resolutionDescription = ' quick resolve';
    var resolutionCode = -1;
    // Call the second function which does the actual closing of the incident
    
    closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode);
    // Save and refresh the form after resolving the case
    formContext.data.refresh(true);
}

2 Script to actually close the incident case

function closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode) {
    // 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 
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));
}

I then tested this function by using a two option field on the case form and it works perfectly.

pastedimage1582677854830v1.png

I now want to instead of adding it as a field on the form itself and triggering the quick resolve from the form to instead create a button on the command bar which will call my javascript function 1 which then calls my javascript function 2. The problem is that I'm not sure how to get the case ID from the form via the ribbon workbench. You can see I'm using

formContext.data.entity.getId();

to get the case ID to pass to the other function but I'm assuming you can't do this on the ribbon workbench. Also I'm not sure how to perform the same save and refresh action that I'm able to perform using:

formContext.data.refresh(true);

Any help would be greatly appreciated.

  • MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

    Hi a33ik

    I think you are right. I did have the close incident function run asynchronously. I instead put formContext.data.refresh(true) after the closeincident method runs successfully.

    function closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode,formContext,buttonContext) {
        // 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 
                    if (buttonContext === 'Form') {
                        // Reload and save the form so that it displays the case as resolved
                        formContext.data.refresh(true)
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(parameters));
    }

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

    To be honest I have no idea why. You can try few other alternatives:

    formContext.data.entity.refresh();

    or

    Xrm.Navigation.openForm passing required parameters that point to your case.

    Can I see your closeIncident method? If it's doing the update in async way refresh happens before the update.

  • MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

    Thanks Andrew a33ik

    Love your work and I always follow your blog. I've done that and have the updated code below. The strange thing is it's not reloading the page after the incident is closed. I expect as soon as I click on the quick resolve incident button

    pastedimage1582689104398v1.png

    function ribbonQuickResolveTrigger (PrimaryItemIds,PrimaryControl) {
        var caseId = PrimaryItemIds[0].slice(1, -1);
        var formContext = PrimaryControl;
        var subject = 'quick resolve'
        var billableHours = 60;
        var resolutionDescription = 'quick resolve';
        var resolutionCode = -1;
        closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode);
        formContext.data.refresh(true);
       }

    Any ideas why?

    Basically I need the form refreshed so that it shows to the person who's pressed the quick resolve button that the case has indeed been resolved and that it's now locked from editing. At the moment when I click Quick Resolve. Nothing happens even though I know in the background it has resolved the case. I still need to do a hard refresh of the page in order to see that the case is now resolved.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

    Hello,

    Thanks for reading my blog. If you want to call your code from ribbon to get form context you should pass primarycontrol as a parameter to method - www.magnetismsolutions.com/.../getting-dynamics-365-formcontext-from-ribbon-workbench

  • MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: Quick Resolve Case on the Ribbon using Javascript. How do you get the record ID from the ribbon button?

    Ok so I managed to get the Case ID by passing the PrimaryItemIds CRM Parameter.

    pastedimage1582685557921v1.png

    I then updated my ribbon button trigger code to the below:

    function ribbonQuickResolveTrigger (PrimaryItemIds) {
        var caseId = PrimaryItemIds[0].slice(1, -1);
        var subject = 'quick resolve'
        var billableHours = 60;
        var resolutionDescription = 'quick resolve';
        var resolutionCode = -1;
        closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode);
        /* BIG QUESTION IS HOW DO I MIMICK THE RELOAD AND SAVE ACTION OF:
        
        formContext.data.refresh(true);
        
        */
       }

    EDIT:

    Ok so I figured out how to get the formContext via the ribbon by passing the PrimaryControl parameter through. Below is the updated code but the refresh and save isn't happening instantly after the case is resolved. I still need to refresh the page manually to see that the case status has been changed to resolved.

    function ribbonQuickResolveTrigger (PrimaryItemIds,PrimaryControl) {
        var caseId = PrimaryItemIds[0].slice(1, -1);
        var formContext = PrimaryControl;
        var subject = 'quick resolve'
        var billableHours = 60;
        var resolutionDescription = 'quick resolve';
        var resolutionCode = -1;
        closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode);
        formContext.data.refresh(true);
       }

    Not sure why this is happening. Is it because the form is reloading before the incident is resolved?

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans