| Editorial Team

Dynamics 365 is a powerful platform that enables
businesses to manage their data and processes
effectively. In this blog post, we will look at how to
concatenate all names of records in a sub-grid using
JavaScript.
Subgrids in Dynamics 365 are a powerful way to display
related records on a form. They allow you to show
multiple records in a grid format within a form,
providing a quick and easy way to view related data.
However, sometimes it is helpful to be able to display
all the names of the records in a sub-grid as a single
string. For example, you may want to display all the
names of the contacts associated with an account on the
account form. This can be achieved using JavaScript.
Here are the steps to concatenate all names of records
in a subgrid in Dynamics 365 using JavaScript:
Step 1: Identify the Subgrid Control
The first step is to identify the subgrid control that you want to work with. You can do this by opening the form editor, selecting the subgrid, and looking at the properties of the control. Take note of the name of the subgrid control.
Step 2: Retrieve the Subgrid Records
Once you have identified the subgrid control, you need
to retrieve the records that are displayed in the
subgrid. You can do this using the following code:
var subgrid = Xrm.Page.getControl(“subgrid_name”);
var records = subgrid.getGrid().getRows().getLength();
In this code, “subgrid_name” is the name of the subgrid
control that you want to retrieve records from. This
code retrieves all the records in the subgrid and stores
them in the “records” variable.
Step 3: Loop through the Subgrid Records
Next, you need to loop through the records in the
subgrid and retrieve the name of each record. You can do
this using the following code:
var names = “”;
for (var i = 0; i < records; i++) {
var recordName =
subgrid.getGrid().getRows().get(i).getData().getEntity().getPrimaryAttributeValue();
names += recordName + “,”;
}
This code loops through all the records in the subgrid,
retrieves the name of each record, and appends it to a
string called “names”. The names are separated by a
comma.
Step 4: Display the Concatenated Names
Finally, you can display the concatenated names in a
field on the form using the following code:
Xrm.Page.getAttribute(“new_names”).setValue(names);
In this code, “new_names” is the logical name of the
field where you want to display the concatenated names.
The concatenated names are set as the value of the field
using the setValue() method.
Conclusion:
In this blog post, we looked at how to concatenate all names of records in a subgrid in Dynamics 365 using JavaScript. By following these simple steps, you can display all the names of the records in a subgrid as a single string, making it easier to view related data on a form. This is just one example of the many powerful capabilities of Dynamics 365 and the customization options available to tailor the platform to your business needs.