Add button to the command bar
If your environment doesn’t have Ribbon Workbench, download the ribbon workbench solution to your environment and create a solution specifically for Ribbon Workbench. Add the existing entity without any metadata to that solution for creating custom buttons.
Navigate to the solutions page(in classic mode) you got to see a button on the above named Ribbon Workbench as like below image
Hit the Ribbon Workbench button, and it will navigate to the command editor page with a pop opened. Select the solution which is specific for buttons.
Note: Ribbon Workbench is too sensitive and it did not handle much entity data. It has a limit of only five entities for a solution.
Select the entity under the solution elements to add buttons. For the current context, we have to add buttons in Home, which refers to Main View. Drag and Drop the button on the desired location and add label and icon to it.
Add Rules and Commands to Button
Add a new JavaScript Web Resource to the solution (Not in button specific solution) with the following script, then save and publish the solution.
Navigate to the Command Editor, then add a new command by clicking the + icon, add a JavaScript action by selecting the library and adding the function name. Add the CRM Parameter and SelectedControlSelectedItemReferences.
Add an Enable rule to show the button only a record is selected. Click on + icon near Enable rules. Click on + Add Step and select SelectionCountRule and set the minimum field to 1. So that, the button appears on the command at least a record gets selected unless it is hidden.
Add the Enable Rule to the Command and Add the Command to the button as like the below image, then publish the Command Editor.
Getting Selected Records
In this blog, I have placed the button in Products Home grid. I got to see the button which appears after selecting a record.
The below GIF will demonstrate the working of this script.