Skip to main content

Notifications

Dynamics 365 general forum

Get the value of the selected data in a subgrid

Posted on by 25

Hello everybody,
I try to get the value of the selected record in subgrid . Here is the code I use, but I get this error:
"Unable to get property 'getData' of undefined or null"

var test = function (){
var selectedRecord = Xrm.Page.getControl("the name of my subgrid").getGrid().getSelectedRows().getAll()[0].getData().getEntity();
var attributes = selectedRecord.getAttributes().getAll();
var attributeValue = attributes.getByName("name").getValue();
alert (attributeValue);
}


I am looking forward to your answers.
Regards

  • Suggested answer
    Iswarya Profile Picture
    Iswarya 1,345 on at
    RE: Get the value of the selected data in a subgrid

    hi,

    function subgriddata(executionContext) {
    var entityObject = executionContext.getFormContext().data.entity;
    var monthlyCost = entityObject.attributes.getByName("monthlyunit");
    }

    try this

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Get the value of the selected data in a subgrid

    Hi Partner,

    1. If you want to get value from multiple selected rows:

    The following code will only return the first record attributes due to getAll()[0]

    var selectedRecord = Xrm.Page.getControl("Name").getGrid().getSelectedRows().getAll()[0].getData().getEntity();

    var attributeValue = selectedRecord.attributes.getByName("attr").getValue();

    alert(attributeValue);

    So you should also remove getAll()[] function .

    2. I just use Console.log to output all selected rows with specific attribute in console to test whether code is working, you could push them into an array with your requirement.

    3. It seems that your attribute(or the related entity's field) is a Lookup field, it's a reference type,

    so then you could try 

    selectedRecord.attributes.getByName("attr").getValue()[0].name

    or

    row.getData().getEntity().attributes.get("attr").getValue()[0].name

    to get its name value.

    Regards,

    Clofly

  • I see U Profile Picture
    I see U 25 on at
    RE: Get the value of the selected data in a subgrid

    Thanks for your answer.
    I have tried it - as alert get [object Object] and not the name of the dataset.
    What does the line that starts with "console.log" do?
    Because in my case I need several values from the selected dataset, because I want to create a dataset with these values.

    Best Regards

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Get the value of the selected data in a subgrid

    Hi Partner,

    getSelectedRows returns a collection, please iterate it with forEach function.

    You could try code below, it works from my test:

    var rows = Xrm.Page.getControl("Name").getGrid().getSelectedRows();
      rows.forEach(function (row, i) {
      console.log(row.getData().getEntity().attributes.get("attribute").getValue());
    });

    And argument in getControl should be Name property of sub grid.

    pastedimage1565690374269v1.png

    Supplement:

    Please delete selectedRecord.getAttributes().getAll(), simply with selectedRecord.attributes.getByName("attr").getValue(),

    the whole statement could be this:

    Xrm.Page.getControl("Name").getGrid().getSelectedRows().getAll()[0].getData().getEntity().attributes.getByName("attr").getValue();

    Regards,

    Clofly

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,104 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans