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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 274

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

I have the same question (0)
  • ram r Profile Picture
    on at

    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

  • MituCRMing Profile Picture
    274 on at

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

  • Suggested answer
    ram r Profile Picture
    on at

    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

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    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

  • MituCRMing Profile Picture
    274 on at

    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
    21 on at

    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.

  • Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

    [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.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans