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

A method to retrieve field values from a grid context via the ribbon

(0) ShareShare
ReportReport
Posted on by 2,146

Hi all,

Quick question. I've created a ribbon button available in the grid view, only appearing when a user selects one or more cases. This function allows a person to resolve all selected cases in the grid. Basically what I'm doing is that I'm passing through the ID of the selected cases by using the SelectedControlSelectedItemIds parameter which you can pass. I then use those IDs and a few other static variables to loop through the array of case IDs which then calls another function which uses the /api/data/v9.2/CloseIncident web API method.

This works fine. I'm now after a way to put in some conditions on what they can and cannot resolve based on certain fields on the case. This is fine with a form button as you can get the formContext which then allows you to get the values of fields on the form but I'm looking at the Grid Context which you can get from the SelectedControl parameter which you can pass via the ribbon. It's just I'm having trouble finding a way of retrieving certain attribute values from selected records in a grid to use as a check for whether selected case can be resolved. Can someone point me in the right direction on getting certain attribute values from selected records in a grid?

Would I need to create another function that uses the web API to loop through the selected record IDs and use the get value API method or is there a way to get attributes via the grid context? Does the field need to be visible on the grid? Below is the code snippet which works well for resolving selected cases from a grid.

async function ribbonGridResolve(SelectedControlSelectedItemIds, SelectedControl) {
    var subject = 'static subject'
    var billableHours = 60;
    var resolutionDescription = 'static description';
    var resolutionCode = 5;
    // Not sure whether I can use the gridContext to retrieve attributes from selected values
    var gridContext = SelectedControl;
    var i;
    Xrm.Utility.showProgressIndicator('Resolving');
    for (i = 0; i < SelectedControlSelectedItemIds.length; i  ) {
        var caseId = SelectedControlSelectedItemIds[i]
        var actualIndex = i   1;
        // This calls a separate function I created that specifically calls the web API closeincident method
        await closeCase(subject, caseId, billableHours, resolutionDescription, resolutionCode, SelectedControlSelectedItemIds.length, actualIndex);
    }

    Xrm.Utility.closeProgressIndicator();

}

I want to put in some additional checks within the for loop based on certain field values in each case but not sure how to retrieve attribute values in the grid and specifically for selected grid items.

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: A method to retrieve field values from a grid context via the ribbon

    Hi, 

    If you want to put some additional check based on certain field, you have two options: 

    1. Add the necessary columns to your subgrid. 
    2. Perform a retrieve with Xrm.WebApi.retrieveRecord()

    I implement the second option in my environment, this is my sample code: 

    async function ribbonGridResolve(SelectedControlSelectedItemIds, SelectedControl) {
        var subject = 'static subject'
        var billableHours = 60;
        var resolutionDescription = 'static description';
        var resolutionCode = 5;
        // Not sure whether I can use the gridContext to retrieve attributes from selected values
        var gridContext = SelectedControl;
        var i;
        Xrm.Utility.showProgressIndicator('Resolving');
        for (i = 0; i < SelectedControlSelectedItemIds.length; i  ) {
            var caseId = SelectedControlSelectedItemIds[i]
            var actualIndex = i   1;
            var isEnabled = await isResolveEnabled(caseId);
            console.log(isEnabled);
            if (isEnabled) {
                // This calls a separate function I created that specifically calls the web API closeincident method
                //await closeCase(subject, caseId, billableHours, resolutionDescription, resolutionCode, SelectedControlSelectedItemIds.length, actualIndex);
                console.log("Case: " caseId " isEnabled")
            }
            else{
                var alertStrings = { confirmButtonLabel: 'Ok', text: "Case XXX can't be resolved" };
                var alertOptions = { height: 120, width: 260 };
                
                Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
                    function success() {
                        // perform operations on alert dialog close
                        
                    },
                    function (error) {
                        console.log(error.message);
                        // handle error conditions
                    }
                );
            }
    
        }
    
        Xrm.Utility.closeProgressIndicator();
    
    }
    
    async function isResolveEnabled(caseId) {
        console.log(caseId);
        var isEnabled = false;
        await Xrm.WebApi.retrieveRecord("incident", caseId, "?$select=description").then(
            function success(result) {
                if (result.description!=null && result.description.includes("approved"))
                    isEnabled = true;
            },
            function (error) {
                console.log(error.message);
                // handle error conditions
            }
        );
        return isEnabled;
    }
    

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 61

Overall leaderboard

Product updates

Dynamics 365 release plans