Pass dynamic initial value to when adding next row

Hello, I am working with master detail set-up with primary form and child(multiple records form) that pulls records based on the primary ID.

When i am adding new row to child I need initial value to be dynamically assigned one of the fields.

Here is an example to make it easier to understand

We have Primary table - Transaction with id, Name and amount

  1. 1, John, 50
  2. 2, Kenny 25

Secondary table Confirmed transactions
empty

When I open John in primary form, its child will show no secondary transactions but when I click to add new one, amount field should automatically get initial value of 50, so you can just press confirm and complete the process.
I am getting the info I need to inset in to the field, but I am unable to make it appear as initial value. is there a way adjust initial value of next record in one of the events?
Global’s do not work, they remain unchanged.

My second questions is when I am adding new record in multi-row form, as soon as one is added - one more new record row is opened below. Is there a way to stop that behaviour? I want anything to happen once new record is added. if one more is needed you would need to press add new again.

Thank you in advance, I got allot of useful tips from reading this forum in the recent months.

Bump, for some ideas.

I see that noone is able to help. Maybe someone knows a way to reference and set initial value variable in events?

Any ideas are greatly appreciated

In the event onLoadRecord of the detail form try something like

If (empty({primary_key_field}))
{
{fieldname} = [varname];
}

{primary_key_filed} must be the primary key field of the detail multi row form table, not the master.
But I’m not sure the event will trigger in multi row form on new record.

If the event fires the issue could be populating\updating correctly a [varname] that must be accessibile from the details form.
In that case you can get the value in the detail form by reading it directly from the DB table: in the same onLoadRecord event, instead of using [varname], perform a SQL lookup of the master table using the primary id passed from the master form.
But the issue in this case is that the lookup will read values saved from the master form. If the user edit those values on the master and add new record on the details form before saving the master, the values read by the sql lookup will be wrong.

Robydago thanks so much for your input, you definitely sent me in the right direction - I managed to solve this riddle, it was just my lack of familiarity with SC.

The issue was happening because both master and slave forms had same ID - lead_id and used {lead_id} to pull values I need from sql in onLoadRecord , it did work when existing records were pulled but {lead_id} var ended up empty for “new grid” input row. So it was unable to form query. Kind of played a trick on myself. I ended up just creating global var for lead_id in master form and using that instead - worked like a charm.

For those that will have same question,(need to change default value of new grid row field) here my explanation of how to make it work:

In your master table open onLoad event and add variable you will be sending to secondary (detail form)

sc_set_global({name_of_var});

in your detail application in onLoadRecord

if (empty({data_id})) // as pointed out by robydago - use id field that will not be empty unless records does not exist
{
{field_name} = [name_of_var];
}

Robydango - any ideas on how to make the system only add one row in grid view? When you press add new - new row shows up with fields, once you complete and press save - it opens new enpty row automatically. I need it to just save the data and that’s it.
Thank you

Proper,

just a remark: checking for (empty({field_name})) is ok if you know that {field_name} will never be empty in existing records: with your code you’re resetting {field_name} to [name_of_var] for all existing record where {field_name} is empty.

Checking for the {primary_key_field}) is usually safer, as it will check if it’s really a new record (unless with some php code you’re adding a new db table record beforehand and than opening that record in the form).

Sorry, no idea how to stop grid view forms from automatically adding a new empty row.

Robydago - you make a good point, and I think this is important that ID field is used, to prevent unexpected values from coming up on empty records.

I will make the adjustment in my example

In my case there is allot of code I excluded from the example to make it simple. Value in question can not be null in DB and has default value set in the table. However I did change to id, just because its a better(foolproof) approach.

If I find a way to stop automatic new row i will post here, thank again.

1 Like

I know this post is old so the issue is maybe “long time forgotten” :slightly_smiling_face: