Skip to content

Commit 494f851

Browse files
committed
gca-create-relation.php: Added snippete demonstrating how to create a relation with GCA.
1 parent 117ba4e commit 494f851

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Gravity Connect // Airtable // Create Phone Number Relation
4+
*
5+
* This snippet demonstrates how to create a relation between two tables in Airtable
6+
* when a GC Airtable feed is being processed. It uses an example assuming the following:
7+
*
8+
* 1. There are a "People" table and "Phone Numbers" table in Airtable.
9+
* 2. The "Phone Numbers" table has, at minimum, a phone number field and a link field.
10+
* 3. There is a GCA feed that creates a record in the "People" table.
11+
* 4. The snippet adds a phone number to the "Phone Numbers" table and creates a relation
12+
* between the newly created "People" record and the "Phone Numbers" record.
13+
*
14+
* Installation:
15+
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
16+
*
17+
*/
18+
19+
add_action( 'gca_entry_added_to_airtable', function( $entry, $create_record_resp, $gca_connection_instance ) {
20+
$gf_phone_number_field_id = '1'; // The ID of the form field which contains the value you want to use to create the relation.
21+
$table_id = 'TODO'; // The ID of the Phone number
22+
$base_id = $gca_connection_instance->get_base_id();
23+
24+
$phone_number = rgar( $entry, $gf_phone_number_field_id );
25+
26+
if ( empty( $phone_number ) ) {
27+
return;
28+
}
29+
30+
/**
31+
* TIP: you can easily find the following by creating a new GC Airtable feed, connecting
32+
* it to the "Phone Numbers" table and saving. If you open the developer console in your
33+
* browser and refresh the page, a table of all the fields in the table will be logged.
34+
*/
35+
$phone_field_id = 'TODO'; // The ID of the phone number field in the Phone Numbers table.
36+
$link_field_id = 'TODO'; // The ID of the link field in the Phone Numbers table.
37+
38+
$records = array(
39+
array(
40+
'fields' => array(
41+
$phone_field_id => $phone_number,
42+
$link_field_id => array( $create_record_resp['id'] ),
43+
),
44+
),
45+
);
46+
47+
try {
48+
$airtable_api = $gca_connection_instance->get_airtable_api();
49+
$create_record_resp = $airtable_api->create_records( $base_id, $table_id, $records );
50+
} catch ( Exception $e ) {
51+
$msg = gca_get_exception_message( $e );
52+
gc_airtable()->log_error( $msg );
53+
}
54+
}, 10, 3 );

0 commit comments

Comments
 (0)