Jump to content
Server Maintenance This Week. ×

Create a child record from a script ?


phil411

This topic is 6711 days old. Please don't post here. Open a new topic instead.

Recommended Posts

  • Newbies

I am new to the concept of having several Tables in one Database.

Previously, when I had to create a record in a (child) related database, I transferred the data using globals, then called a script in the related database, which added the record.

Now, this should be easier with multiple Tables in one database I thought. Just call "New record", specifying the Database and insert the data.

However, "New record" will create a record in the parent database. You cannot specify the target table - or I simply did not find this function.

Well, I can jump to a layout where only the child database is listed, or as an alternative, can jump in a layout, where a portal is visible, then jump to the last record if the portal and then add the data (add some code to freeze the window, stop user-interaction etc. etc. )

But this looks for me more like a workaround then a good solution.

Could anyone with experience with FM7 give me a tip to do this a better way ?

Thanks.

Link to comment
Share on other sites

Hi, Phil, and welcome to the forums!

If your relationship is set to allow creation of related records on the right-hand side, then simply setting a value for a field in the related table (other than the key field) should create a new record in the child table.

HTH,

Jerry

Link to comment
Share on other sites

  • Newbies

Thanks for you answer QuinTech,

That works, but only for one record. If there are already records present in the table, no new record will be created, instead, the current record will be overwritten by the new value.

I should have described the problem more exact:

I have a text field where you can paste some lines of text. A script then parses the text in that field and filters out unneeded characters and blank lines. The remaining lines should go into the child table. One record per line.

I would like to omit switching to a layout where the child table is visible.

Link to comment
Share on other sites

I would like to omit switching to a layout where the child table is visible.

Hmmm... i don't know if that is possible. I could be wrong, though. I was about to recommend that you insert script steps such as:

Go to Field (Field in Portal)

Go to Portal Row (Last Row)

And then create your new record. This would, indeed, create a new child record without overwriting what you've already got, but given the last line in your post, i suspect you've already tried this and don't like it so much.

Maybe you could add a tiny, tiny portal (with fields in 1-point font) to the layout you want people to see, and then hide it somewhere? That way, you could have that last-portal-row functionality without having to switch layouts. If you cover the portal up with a button or a picture, it shouldn't be noticeable.

J

Link to comment
Share on other sites

  • Newbies

Right,

I am already doing this - If there is no other way, then ok. I just thought, there could be an easier way. From my experience, if you have to do operations like this with a larger numbers of records, performance will suffer, but I have just begun to try out FM7, maybe it behaves better then older versions, also the Computing Power of a G5 is much better compared to a G3 with OS9 and FM4/5, so you dont notice the slowdown.

btw. to delete all related records my workaround is:


Loop

   Go to Field [child::somefield0]

   exitLoop If [Count (child::somefield0,child::somefield1,child::somefields2) = 0]

   Delete Portal Row[No dialog]

End Loop

I suspect, this also is the way do do it.

Thanks again for your replies !

Link to comment
Share on other sites

Sorry, i can't think of any other way. Maybe someone else knows...

As for your related-record deletion, a quicker way might be:

Duplicate the parent record

Change the key field in the original record

Delete the duplicate (making sure the option "Delete related records" is selected in the relationship). This will delete those related records.

Back in the original record, change the key field back to what it was originally.

J

Link to comment
Share on other sites

You got it. One caveat though: in a multi-user situation, there's a potential for conflict if two users are calling the script simultaneously. I believe this can be solved by changing the global key calc to something that is user-specific, or Random.

Link to comment
Share on other sites

  • 6 months later...

You may be missing the point here: "this" is NOT using a portal. Just be anywhere where you can (a) create records in the target table via a relationship (in version 7 that could be one or more tables away), and (: have acces to the data you need to pass to the new record - whether it is in the current record, or a related one.

Link to comment
Share on other sites

  • 3 months later...

Well, it does have a relationship - it just doesn't have any related records. Until you set a field of the 'non-existing' record - then a new record is created, and it's ParentID has a value 0.5.

This is very similar to the last row in a portal. There's no record there, until you start typing into it. That creates a new record.

Link to comment
Share on other sites

This topic is 6711 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.