Skip to content

Commit 3c7f9f1

Browse files
committed
gca-create-relation.php: Added snippete demonstrating how to create a relation with GCA.
1 parent ddeeeb3 commit 3c7f9f1

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
* https://gravitywiz.com/documentation/gravity-connect-airtable
18+
*/
19+
20+
add_action( 'gca_entry_added_to_airtable', function( $entry, $create_record_resp, $gca_connection_instance ) {
21+
$gf_phone_number_field_id = '1'; // The ID of the form field which contains the value you want to use to create the relation.
22+
$table_id = 'TODO'; // The ID of the Phone number
23+
$base_id = $gca_connection_instance->get_base_id();
24+
25+
$phone_number = rgar( $entry, $gf_phone_number_field_id );
26+
27+
if ( empty( $phone_number ) ) {
28+
return;
29+
}
30+
31+
/**
32+
* TIP: you can easily find the following by creating a new GC Airtable feed, connecting
33+
* it to the "Phone Numbers" table and saving. If you open the developer console in your
34+
* browser and refresh the page, a table of all the fields in the table will be logged.
35+
*/
36+
$phone_field_id = 'TODO'; // The ID of the phone number field in the Phone Numbers table.
37+
$link_field_id = 'TODO'; // The ID of the link field in the Phone Numbers table.
38+
39+
$records = array(
40+
array(
41+
'fields' => array(
42+
$phone_field_id => $phone_number,
43+
$link_field_id => array( $create_record_resp['id'] ),
44+
),
45+
),
46+
);
47+
48+
try {
49+
$airtable_api = $gca_connection_instance->get_airtable_api();
50+
$create_record_resp = $airtable_api->create_records( $base_id, $table_id, $records );
51+
} catch ( Exception $e ) {
52+
$msg = gca_get_exception_message( $e );
53+
gc_airtable()->log_error( $msg );
54+
}
55+
}, 10, 3 );

0 commit comments

Comments
 (0)