Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

Posted on by 270

I have a created a custom ribbon button that opens an HTML dialog, I need to pass the the selected rows into the dialog and when click submit I want to create/add a record to the selected rows. Thanks!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    [quote user="Rookie-CRMing"]This solution does not work for me.  It tells me 'Xrm.Page.getControl(...) is null'.[/quote]

    Your control name cannot have spaces. Check it in the Form Designer and use the control's name.

    I think you may need to be careful if this is an Editable Grid rather than the standard Read-only grid as it has different mechanisms for interacting with the related data.

    Hope that helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    This solution does not work for me.  It tells me 'Xrm.Page.getControl(...) is null'.

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    Rather than use the getGrid.getSelectedRows - the best option for ribbon buttons is to use the selectedItems passed to the function as I describe at ribbonworkbench.uservoice.com/.../129783-pass-the-currently-selected-grid-row-s-to-a-custo

    It is an array that will give you the Id of the items which you can then use in your query.

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    Thank you Scott and Ramanathan.

    Here is my code that I will be using to select rows from a view and add/create a record to the selected rows. I will call this code from the ribbon workbench. I know the code isn't quite right, I would appreciate if you guys can point me on what I need to do. Thanks!

    // JavaScript source code
    var selectedEntityReferences = [];
    var selectedRows = Xrm.Page.getControl("Active Contacts>").getGrid().getSelectedRows();
    selectedRows.forEach(function (selectedRow, i) {
        selectedEntityReferences.push(selectedRow.getData().getEntity().getEntityReference());
    });
    
    function createOperationUsingWebAPI() {
      var clientUrl = Xrm.Page.context.getClientUrl();
       
        var req = new XMLHttpRequest()
       
        req.open("POST", encodeURI(clientUrl + "/api/data/v8.0/accounts"), true);
        
        req.setRequestHeader("Accept", "application/json");
        
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        
        req.setRequestHeader("OData-MaxVersion", "4.0");
       
        req.setRequestHeader("OData-Version", "4.0");
        
        req.onreadystatechange = function () {
           
            var accountUri = this.getResponseHeader("OData-EntityId");
            
        };
       
        var body = JSON.stringify({
            
        "name": "Sample Account",
       
        "creditonhold": false,
        
        "address1_latitude": 47.639583,
        
        "description": "This is the description of the sample account",
       
        "revenue": 5000000,
        
        "accountcategorycode": 1
       
    });
    
    req.send(body);
    
    }
    


  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    Do you mean on a standard ribbon button associated to a sub-grid.

    If so you can use my how-to article here - ribbonworkbench.uservoice.com/.../129783-pass-the-currently-selected-grid-row-s-to-a-custo

    Hope this helps

  • Suggested answer
    ram r Profile Picture
    ram r on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    we can create records using webapi and relate records by populating lookup fields, below is the reference for all CRUD operations

    scaleablesolutions.com/crud-operations-using-microsoft-dynamic-crm-web-api

    Reference for webapi lookup

    exptechsolutions.blogspot.com/.../know-how-set-lookup-value-on.html

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    Got it thanks, Ramanathan. How do I create a corresponding record for the selected rows upon submission?

  • ram r Profile Picture
    ram r on at
    RE: Pass the currently selected grid row(s) to a Ribbon Button and create a Record JavaScript

    To get the selected row from a grid below are the code and reference. I am not clear with the dialog part, If its a webresouce you can pass selected id's as URL or directly access the grid to get selected records and perform the create operation as needed on submit click.

    var selectedEntityReferences = [];

    var selectedRows = Xrm.Page.getControl("<grid name goes here>").getGrid().getSelectedRows();

    selectedRows.forEach(function (selectedRow, i) {

    selectedEntityReferences.push(selectedRow.getData().getEntity().getEntityReference());

    });

    msdn.microsoft.com/.../dn932126.aspx

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