Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Copying related records from one table to another

Featured Replies

Hi,

My database has three tables. You can see the relationship here:

fm1.jpg

The main table is called "Load" and it is related to the table "CustomerCommission" by the field "CommissionCustomerID" and is related to the table "LoadCommission" by the field "LoadID".

I want to find all the records in the "CustomerCommission" table that match the current Load's "CustomerCommissionID" and copy some of their fields to new records in "LoadCommission" table (there is a portal to the "LoadCommission" table in the Load layout.

Here's the script:

Go to Layout [ "Load" (Load) ]

Go to Related Record [ From table: "CustomerCommission"; Using layout: "CustomerCommission" (CustomerCommission) ]

[ Show only related records ]

Loop

Set Field [ LoadCommission::LoadID; Load::LoadID ]

Set Field [ LoadCommission::Type; CustomerCommission::Type ]

Set Field [ LoadCommission::Amount; CustomerCommission::Amount ]

Set Field [ LoadCommission::AccountName; CustomerCommission::AccountName ]

Commit Records/Requests

[ Skip data entry validation; No dialog ]

Go to Record/Request/Page

[ Next ]

End Loop

Go to Layout [ "Load" (Load) ]

I know I'm missing some steps... never done a copy like this before. When I run it, it just keeps overwriting the first record in "LoadCommission", it never actually adds another record.

Thanks folks!

Andrew

Edited by Guest

I am guessing the first time around a record is created in LoadCommission.

The second time, there already is a related record, so the Set Field steps operate on the existing record, setting its fields over and over again.

The solution is to use a dummy relationship to create and set the related records, then as the final step set the match field to the proper value.

  • Author

I'm not sure what you mean. What is a dummy relationship, could you give me an example of how it applies?

I'm glad to know that this can be done, but have no idea what you mean at all.

Thanks!

I have posted a demo of the concept in this thread.

  • Author

Thanks comment,

I kind of understand.. but not really. Would you mind taking a look at my file and see if you can tell me what I need to change?

test.fp7.zip

Edited by Guest

Can you post it for us? If you've tried, it didn't stick. :wink2:

  • Author

whoops!

it's on there now.

Can you explain in a few words what this is about? Your table names don't mean anything to me, so it's hard to understand what you're trying to do here - in general, and why data is to be copied in this case.

  • Author

Sure.

Commissions in our sales database are set depending on the customer. For example, a job for customer A might give commissions to three different people. That is set in the CustomerCommissions database.

When someone creates a new Load (like a new order), these customer commissions need to be copied over to the LoadCommissions table so we have the ability to override the customer commissions on a per order basis without affecting the basic commission rate that we usually offer for that customer.

This script will automatically run when someone creates a new record in the Load database. It searches for all the records matching the new Load's customerID in the CustomerCommissions database and copies over the relevant fields to new records in the LoadCommission database.

Hope that makes sense...

I hope I understand this correctly.

Well, you COULD do it this way (see attached), but it seems simpler to find the relevant customer commisions (by GTRR), then import them into load commisions (with LoadID auto-entered from a global field).

Forgive me for changing your names - I get distracted otherwise.

test.fp7.zip

  • Author

Thanks a million comment, I think we're almost there...

I can't figure out what the difference is between your database and mine that yours works. In mine, it continuosly overwrites the first OrderCommission record in the portal; it never adds more than one. I made the changes to my file trying to mimic your techniques... can you please tell me what I'm still missing? By the way, do you need to have the "Customer" table in there? Seemed like I could have done without it... and why did you match cCreator in "Customers" to "OrderID"? That one blows my mind.

Thanks so much

orders.fp7.zip

Edited by Guest

cCreator is the trick here. It creates records thru a "dummy relationship", sets them to the values of the current parent record, and "releases" them by setting OrderID to what it really should be. That way, the dummy relationship always has only one, new, record in OrderCommissions.

(Since cCreator = AccountName, in a multi-user scenario each user has their own "scratch record", and no conflict arises.)

You don't NEED the Customer table, but I had to see some order in front of me to understand what I was doing. Seems to me that where there are customer commisions, there must be customers - don't you think?

  • Author

very clever!!

So you can't really scroll through portal rows, adding new ones, in a normal way, eh?

It takes a bit of thinking to get your head around this method, but it works. Thanks, you saved my life!

you can't really scroll through portal rows, adding new ones, in a normal way, eh?

Yes, you can - only not thru THIS relationship (the dummy one). You'd be creating orphaned records. You can still add records in the "normal" relationship portal (if you have enabled creation in the relationship's definition).

Go to Layout [ "Load" (Load) ]

Go to Related Record [ From table: "CustomerCommission"; Using layout: "CustomerCommission" (CustomerCommission) ]

[ Show only related records ]

Loop

Set Field [ LoadCommission::LoadID; Load::LoadID ]

Set Field [ LoadCommission::Type; CustomerCommission::Type ]

Set Field [ LoadCommission::Amount; CustomerCommission::Amount ]

Set Field [ LoadCommission::AccountName; CustomerCommission::AccountName ]

Commit Records/Requests

[ Skip data entry validation; No dialog ]

Go to Record/Request/Page

[ Next ]

End Loop

Go to Layout [ "Load" (Load) ]

You're not getting any new records because you're not forcing FMP to go to the last record in the portal. This is the only place to add new data in a portal. Insert it immediately after the loop step.

If you're going into scripting then you might want to get FM7 Developer for the script debugger.

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.