This topic is locked

detail table list, button in row, update field in row with value from master table field

6/19/2025 9:42:38 AM
PHPRunner General questions
M
Mallorca1 author

Hello together, I try since a longer time and can not find a solution for:

Phprunner11
Button in row of detail table List
Should update field in row with value from master table field
But getMasterRecord() just returns the first record of the table

What I am doing wrong here?
Thank you!

ChatGPT 6/19/2025

It sounds like you're trying to update a field in a detail table based on a value from the master table in PHPRunner, but you're encountering the issue where getMasterRecord() consistently returns the first record of the master table instead of the relevant record for the detail table row you're working with.

Here are a few steps to troubleshoot and possibly resolve your issue:

  1. Understanding getMasterRecord(): The getMasterRecord() function is designed to retrieve the record from the master table based on the current context of the detail table. Ensure that you are calling this function in the appropriate context so it knows which master record to return.


  2. Check the Context: Make sure that you are calling getMasterRecord() within the right event or function that has access to the current row's context. For example, if you are trying to use it in a button or an action that is within the detail table, ensure that it is being executed in the right event (like BeforeShowRow or BeforeProcessRow).


  3. Verify Relationships: Double-check that the relationship between your master and detail tables is set up correctly in PHPRunner. Ensure that the linking fields are properly defined and that data is being passed correctly.


  4. Debugging: To troubleshoot further, you can use debugging techniques like:

    • Logging the ID of the current detail record to ensure it matches with the expected master record.
    • Outputting the result of getMasterRecord() to see what it returns at different points in your code.


  5. Custom Code: If getMasterRecord() continues to not work as expected, you might consider writing custom SQL queries or using the Lookup feature if your data structure allows it. You can perform a manual lookup using the IDs available to you and retrieve the relevant master record data.


  6. Using the Correct Event: Ensure that you are using this in the correct event for the button. If you're placing the button in individual rows, you may want to use the BeforeProcessRow or AfterProcessRow event to ensure you're operating on the right record.



Here’s a simple example of how you might implement this:

// Assuming you have the detail table's unique ID and a master field that you want to copy
$masterRecord = getMasterRecord();
if($masterRecord) {
$newValue = $masterRecord['field_name']; // Replace with your actual field name
$recordId = $data['ID']; // Replace with your actual detail record ID
// Now update your detail table record
// Use your preferred method to update the database, e.g., SQL query or PHPRunner's built-in functions
}
  1. PHPRunner Support and Community: If you still have issues, consider checking PHPRunner's documentation or forums. The community can be quite helpful, and similar issues might have been discussed before.

By following these steps, you should be able to identify the issue and find a suitable solution for updating the field in your detail table based on the master table's data.

M
Mallorca1 author 6/19/2025

I tried all this and other sulutions from geminiAI but not working, I always just get the first record of the table which is master with getMasterRecord(). Lookup will not work since the detail table has not enough info to find the exact master record.

C
copper21 6/19/2025

If the button you are putting is in the row (grid) of the child list page, then this works for me when viewing the child list page from the master page:

Server Tab of button:

$record = $button->getMasterRecord();

$sql1 = DB::PrepareSQL("SELECT * FROM MasterTable WHERE PrimaryKey = :1", $record['ID']);

$rs1 = DB::Query($sql1);

$data1 = $rs1->fetchAssoc();

This should return the data from the master table for that specific record. You will have to change "MasterTable" with the table name, "PrimaryKey" with your primary key field, and "ID" with the name of your primary field name.

M
Mallorca1 author 6/20/2025

Unfortunately also this is not working since getMasterRecord() is not outputing the data which is displayed in the master it is always outputing the first row of the table of the master.

This Button (Server code) I have in the row of the detail list page:

$record1 = $button->getMasterRecord();
$sql1 = DB::PrepareSQL("SELECT * FROM appointments WHERE id = :1", $record1['id']);
$rs1 = DB::Query($sql1);
$data1 = $rs1->fetchAssoc();

$datavalues = array();
$datavalues["appointmentId"] = $data1["id"];

$record2 = $button->getCurrentRecord();

$keyvalues = array();
$keyvalues["id"] = $record2["id"];

DB::Update("briefings", $datavalues, $keyvalues);

/// Alternative this is having the same result:

$record1 = $button->getMasterRecord();

$datavalues = array();
$datavalues["appointmentId"] = $record1["id"];

$record2 = $button->getCurrentRecord();

$keyvalues = array();
$keyvalues["id"] = $record2["id"];

DB::Update("briefings", $datavalues, $keyvalues);

C
copper21 6/20/2025

I think I understand what you are trying to do. I wrote this:

//Get Master Record ID & Master Data
$record1 = $button->getMasterRecord();

//You Don't Need This If You Just Need Master Record Primary Key
$sql1 = DB::PrepareSQL("SELECT * FROM appointments WHERE id = :1", $record1['id']);
$rs1 = DB::Query($sql1);
$data1 = $rs1->fetchAssoc();

//Get Child Record Data
$record2 = $button->getCurrentRecord();

$data3 = array();
$keyvalues3 = array();
//Taking Master Primary Key & Updating Child Record "AppointmentID" Field With It
$data3["appointmentid"] = $record1["id"];
$keyvalues["id"] = $record2["id"];
DB::Update("briefings", $data3, $keyvalues3 );

If all you need is the master primary key, then you don't need to use the 3 lines of code under "//You Don't Need....."

$record2 will get your child record's primary key.

If all you need to do is update the "appointmentid" field in the child table, with the primary key of the master table, this should work.

Make sure the button is in the grid/row.

img alt

Make sure that the child and master tables are connected in the "Tables" section.

M
Mallorca1 author 6/21/2025

Thank you but still I just get the id of the first row of the master table not the id of the displayed row of the master table.

More details:
Master table (appointments) and custom view detail table (briefingToApointments) is connected in Tables over clientId.
This way I display a list, of all briefings of the client of the displayed master, on the master edit view, in a child list.
With a button in each row of the child list (briefingToApointments), I want to write the appointmentId, to the briefing table appointmentId field of the row the button is in, so the briefing is connected to one appointment.
But with all the codes I tried and we have posted here, I just get the id of the first appointment in the appointments table not the displayed appointment date of the master.

M
Mallorca1 author 6/25/2025

Since I could not find to solve it another way, I used the Session Variables. But yes, this will make a problem if someone opens a second tab.

C
copper21 6/25/2025

Can you send a screen shot of the button and where you have it on the list page? Like previously mentioned, it seems like you have it on the list page of the child table, which is connected to the parent table.

I am just trying to figure out why you are getting the first record every time. I have encountered something similar, but that was a long time ago and certainly don't remember what the solution was.

M
Mallorca1 author 6/28/2025

Thank you!
This is the child table list with the button in row:

img alt

C
copper21 6/30/2025

Mallorca1,

Thank you for the screenshot. The one thing that I noticed right away is that the "Master table info" snippet that PHPR puts in when two tables are connected isn't on your child/details table you are showing.

I am guessing that might have to be there in order to get the correct ID from the master table.

img alt

M
Mallorca1 author 7/1/2025

Thank you I also had this bevore but it's not changing anything. I always get the data of the first record of the table, not the one which is displayed on the main.

Is maybe this a problem: I have three other child/detail tables on this page all jsut connected to the master and not to each other.

M
Mallorca1 author 7/2/2025

img alt

Please find the screenshot of the master here.

Tandy 7/4/2025

What about joining the fields you want. On mine I listed fields that I want to see on details joined? Do not know if that will help but it did in my case.

A
AbriaCloud 7/8/2025

I am having a similar issue in that I resorted to using a session variable to determine the master record. It is a very reliable method. However, as mentioned earlier, the session key is being retrieved from the last opened tab, rather than the detail list row where the button is clicked.

The session variable option would work - IF you permit only one detail list to be shown at a time in the master list. Click a new one, close all the others.

How would you accomplish this? If you click on a detail row expansion option (hamburger icon, javascript request, etc.), how do you close all other detail lists on the same master listing? As far as I can tell, there is no trigger when a expansion of detail rows is selected.