Jump to content

Create New Related Record in Portal Problem


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

Recommended Posts

Hi,

I am trying to created a group of related records in a portal based on a JSON array I have parsed.  I have a bug where it only creates one related record in the portal and then creates a new parent record for the remaining # in the array.  If the array has 14 items I get in the portal for the related records and then get 13 new parent records.  Any idea what im doing wrong?

Set Variable [ $$json ; Value: Bid_Requests::AirbnbJSONRESPONSE ] 
Set Variable [ $fkey ; Value: Bid_Requests::PrimaryKey ] 
Set Variable [ $ItemCount ; Value: ValueCount ( JSONListKeys ( Bid_Requests::AirbnbJSONRESPONSE ; "object.response.response_json.explore_tabs[0].sections[0].listings" ) ) ] 
Set Variable [ $i ; Value: 0 ] 
If [ $ItemCount>0 ] 
Loop
Go to Layout [ “BidDetails” (Bid_Requests) ; Animation: None ]
Go to Object [ Object Name: "Airbnbgrid" ] 
Go to Portal Row [ Select: On ; Next ; Exit after last: On ]
New Record/Request
Set Field [ AirbnbSCRAPE::FK_BidRFH ; $fkey ] 
Set Field [ AirbnbSCRAPE::id ; JSONGetElement ( $$json ; "object.response.response_json.explore_tabs[0].sections[0].listings[" & $i & "].listing.id" ) ] 
Set Field [ AirbnbSCRAPE::city ; JSONGetElement ( $$json ; "object.response.response_json.explore_tabs[0].sections[0].listings[" & $i & "].listing.city" ) ] 
Set Field [ AirbnbSCRAPE::guest_label ; JSONGetElement ( $$json ; "object.response.response_json.explore_tabs[0].sections[0].listings[" & $i & "].listing.guest_label" ) ] 
Set Field [ AirbnbSCRAPE::localized_neighborhood ; JSONGetElement ( $$json ; "object.response.response_json.explore_tabs[0].sections[0].listings[" & $i & "].listing.localized_neighborhood" ) ] 
Set Field [ AirbnbSCRAPE::name ; JSONGetElement ( $$json ; "object.response.response_json.explore_tabs[0].sections[0].listings[" & $i & "].listing.name" ) ] Commit Records/Requests [ With dialog: Off ] 
Set Variable [ $i ; Value: $i+1 ] 
Exit Loop If [ $i ≥ $ItemCount ] 
End Loop
End If

 

Thanks,

In advance.

 

Tim

 

Link to comment
Share on other sites

I only glanced at your script. But I see that it contains a New Record step within a loop. The New Record step will always create a new record in the active layout's table - which I believe is the parent table in your case. To create a child record via a portal, you only need to enter some data in the "ghost" row of the portal. However, when scripting the creation of child records, it is much better practice to work strictly at the data level and not rely on the presence of any layout objects such as a portal. That means going to a layout of a child table and looping there.

An alternative method of creating child records from the layout of parent is described here:
https://filemakerhacks.com/2011/07/31/magic-key-and-check-box-reporting/

 

Link to comment
Share on other sites

Not sure what you mean by "ghost" row? I just want to find the easiest way to add several new records to a portal via script.  Is there an easy way to do this without the magic key approach?

Link to comment
Share on other sites

5 hours ago, TimJ said:

Not sure what you mean by "ghost" row?

I mean the first empty row in a portal based on a relationship that allows creation of related records in the child table. Entering data in this row will create a new child record and populate its foreign key field/s with the values from the current parent's match field/s - see: 
https://fmhelp.filemaker.com/help/18/fmp/en/index.html#page/FMP_Help%2Fcreating-relationships.html%23ww1142949
https://fmhelp.filemaker.com/help/18/fmp/en/index.html#page/FMP_Help/adding-duplicating-deleting-records.html

 

5 hours ago, TimJ said:

I just want to find the easiest way to add several new records to a portal via script. 

The easiest way is to add new records directly to the child table, not to the portal. As I tried to say earlier, it is also the better method (with one exception*). I am attaching a simple demo that shows how it can be done (you did not post an example of your JSON, so I had to make up my own).

ParseJSONtoChild.fmp12

 

---
(*) Records added through a portal remain uncommitted, allowing you to roll back the entire operation by reverting the parent record. This is known as being "transactional".

 

Link to comment
Share on other sites

This topic is 1507 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.