Create Relation
This snippet demonstrates how to create a relation between two tables in Airtable when a GC Airtable feed is being processed. It uses an example assuming the following:
- There is an Airtable Base with at least two tables.
- There is a Gravity Forms feed that is connected to one of the tables in the Airtable Base.
Optionally, the snippet can search the linked table for an existing record and link it instead of creating a duplicate. If no matching record is found, a new linked record is created as usual.
TIP: you can easily find the following by creating a new GC Airtable feed, connecting it to the Table which you want to create the relation in and then saving. If you open the developer console in your browser and refresh the page, a table of all the fields in the table will be logged.
- $args['linked_table_id']
- $args['link_field_id']
- $args['value_mappings'] (Airtable field IDs mapped to Gravity Forms field IDs or merge-tag templates)
- $args['match_field_id'] (the Airtable field ID to populate when a new linked record is created)
- $args['match_field_name'] (the name of the same Airtable field, used to find an existing linked record)
- $args['match_value'] (a Gravity Forms merge-tag template used to build the value to match)
Installation:
References:
- https://gravitywiz.com/documentation/gravity-connect-airtable
- https://gravitywiz.com/documentation/gca_entry_added_to_airtable/
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gca-create-relation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Gravity Connect // Airtable // Create Relation
*
* This snippet demonstrates how to create a relation between two tables in Airtable
* when a GC Airtable feed is being processed. It uses an example assuming the following:
*
* 1. There is an Airtable Base with at least two tables.
* 2. There is a Gravity Forms feed that is connected to one of the tables in the Airtable Base.
*
* Optionally, the snippet can search the linked table for an existing record and
* link it instead of creating a duplicate. If no matching record is found, a new
* linked record is created as usual.
*
* TIP: you can easily find the following by creating a new GC Airtable feed, connecting
* it to the Table which you want to create the relation in and then saving.
* If you open the developer console in your browser and refresh the page, a table of all
* the fields in the table will be logged.
*
* - $args['linked_table_id']
* - $args['link_field_id']
* - $args['value_mappings'] (Airtable field IDs mapped to Gravity Forms field IDs or merge-tag templates)
* - $args['match_field_id'] (the Airtable field ID to populate when a new linked record is created)
* - $args['match_field_name'] (the name of the same Airtable field, used to find an existing linked record)
* - $args['match_value'] (a Gravity Forms merge-tag template used to build the value to match)
*
* Installation:
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*
* References:
* * https://gravitywiz.com/documentation/gravity-connect-airtable
* * https://gravitywiz.com/documentation/gca_entry_added_to_airtable/
*/
/**
* @param mixed $args
* @param? array $args['form_id'] The ID of the form to which this relation applies.
* @param? array $args['feed_id'] The ID of the feed to which this relation applies. (Only used if form_id is also provided)
* @param string $args['linked_table_id'] The ID of the linked table in Airtable.
* @param string $args['link_field_id'] The ID of the field in the linked table that links to table connected to the feed.
* @param? array $args['value_mappings'] Maps Airtable field IDs to Gravity Forms fields or merge-tag templates.
* @param? string $args['match_field_id'] The ID of the Airtable field populated when a new linked record is created.
* @param? string $args['match_field_name'] The name of the Airtable field used to match an existing linked record.
* @param? string $args['match_value'] A Gravity Forms merge-tag template that resolves to the value to match.
*
* @return void
*/
function gca_create_relation( $args = array() ) {
$args = wp_parse_args(
$args,
array(
'form_id' => null, // include form ID
'feed_id' => null,
'linked_table_id' => null, // The ID of the Phone Numbers table.
'link_field_id' => null, // The ID of the field in the Phone Numbers table that links to the People table.
'value_mappings' => array(), // Map Airtable fields to Gravity Forms fields or merge-tag templates.
'match_field_id' => null,
'match_field_name' => null,
'match_value' => null,
)
);
if ( ! $args['linked_table_id'] || ! $args['link_field_id'] ) {
return;
}
$has_match_value = $args['match_value'] !== null && $args['match_value'] !== '';
$has_match_config = $args['match_field_id'] || $args['match_field_name'] || $has_match_value;
$can_match = $args['match_field_id'] && $args['match_field_name'] && $has_match_value;
if ( $has_match_config && ! $can_match ) {
gc_airtable()->log_error( 'gca_create_relation(): All three matching settings are required.' );
return;
}
$filter_name_pieces = array( 'gca_entry_added_to_airtable' );
if ( $args['form_id'] ) {
$filter_name_pieces[] = $args['form_id'];
}
if ( $args['form_id'] && $args['feed_id'] ) {
$filter_name_pieces[] = $args['feed_id'];
}
$filter_name = implode( '_', $filter_name_pieces );
add_action(
$filter_name,
$sync_relation = function ( $entry, $create_record_resp, $gca_connection_instance ) use ( $args, $can_match ) {
if ( empty( $args['linked_table_id'] ) ) {
return;
}
$main_record_id = rgar( $create_record_resp, 'id' );
if ( empty( $main_record_id ) ) {
return;
}
$base_id = $gca_connection_instance->get_base_id();
$value_mappings = $args['value_mappings'];
$mappings = array();
foreach ( $value_mappings as $airtable_field_id => $field_id_or_template ) {
$is_merge_tag_template = is_string( $field_id_or_template ) &&
strpos( $field_id_or_template, '{' ) !== false &&
strpos( $field_id_or_template, '}' ) !== false;
if ( $is_merge_tag_template ) {
$value = GFCommon::replace_variables(
$field_id_or_template,
$gca_connection_instance->get_form(),
$entry,
false,
false,
false,
'text'
);
} else {
$value = rgar( $entry, $field_id_or_template );
}
if ( $value === '' || $value === null ) {
// do not use empty() here so that the values 0 and 0.0 are allowed.
continue;
}
$mappings[ $airtable_field_id ] = $value;
}
try {
$airtable_api = $gca_connection_instance->get_airtable_api();
if ( $can_match ) {
$match_value = trim( (string) GFCommon::replace_variables(
$args['match_value'],
$gca_connection_instance->get_form(),
$entry,
false,
false,
false,
'text'
) );
if ( $match_value === '' ) {
gc_airtable()->log_error( 'gca_create_relation(): The configured match value resolved to an empty value.' );
return;
}
$field_name = str_replace(
array( '\\', '}' ),
array( '\\\\', '\\}' ),
$args['match_field_name']
);
$formula_value = str_replace(
array( '\\', '"' ),
array( '\\\\', '\\"' ),
(string) $match_value
);
$response = $airtable_api->list_records(
$base_id,
$args['linked_table_id'],
array(
'filterByFormula' => sprintf( '{%s} = "%s"', $field_name, $formula_value ),
'returnFieldsByFieldId' => true,
'maxRecords' => 2,
'pageSize' => 2,
)
);
$matches = (array) rgar( $response, 'records', array() );
if ( count( $matches ) > 1 ) {
gc_airtable()->log_error( 'gca_create_relation(): More than one linked record matched; no relation was created.' );
return;
}
if ( count( $matches ) === 1 ) {
$matched_record_id = rgar( $matches[0], 'id' );
$linked_record_ids = rgars(
$matches[0],
'fields/' . $args['link_field_id'],
array()
);
if ( empty( $matched_record_id ) ) {
gc_airtable()->log_error( 'gca_create_relation(): Airtable did not return the matched record ID.' );
return;
}
if ( ! is_array( $linked_record_ids ) ) {
$linked_record_ids = array();
}
$linked_record_ids[] = $main_record_id;
$airtable_api->patch_record(
$base_id,
$args['linked_table_id'],
$matched_record_id,
array(
$args['link_field_id'] => array_values( array_unique( $linked_record_ids ) ),
)
);
return;
}
$mappings[ $args['match_field_id'] ] = $match_value;
}
$records = array(
array(
'fields' => array_merge(
array(
$args['link_field_id'] => array( $main_record_id ),
),
$mappings
),
),
);
$create_record_resp = $airtable_api->create_records(
$base_id,
$args['linked_table_id'],
$records
);
} catch ( Exception $e ) {
$msg = gca_get_exception_message( $e );
gc_airtable()->log_error( $msg );
}
},
10,
3
);
add_action(
'gform_post_process_feed',
function ( $feed, $entry, $form, $addon ) use ( $args, $sync_relation ) {
if ( rgar( $feed, 'addon_slug' ) !== 'gc-airtable' ) {
return;
}
if ( $args['form_id'] && (int) rgar( $form, 'id' ) !== (int) $args['form_id'] ) {
return;
}
if (
$args['form_id'] &&
$args['feed_id'] &&
(int) rgar( $feed, 'id' ) !== (int) $args['feed_id']
) {
return;
}
$main_record_id = gc_airtable()->get_resource_id( $entry, $feed );
if ( empty( $main_record_id ) ) {
return;
}
try {
$connection = \GC_Airtable\Airtable_Connection::create_from_feed( $feed );
$sync_relation(
$entry,
array( 'id' => $main_record_id ),
$connection
);
} catch ( Exception $e ) {
$msg = gca_get_exception_message( $e );
gc_airtable()->log_error( $msg );
}
},
10,
4
);
}
/**
* Usage Example:
*/
gca_create_relation(
array(
/**
* Change this to your form ID.
*/
'form_id' => 1,
/**
* Change this to the ID of the feed you want to use.
*/
'feed_id' => 2,
/**
* Change this to the ID of the linked table in Airtable.
*/
'linked_table_id' => 'tblXXXXXXXXXXXXXX',
/**
* Change this to the ID of the link field in Airtable.
*/
'link_field_id' => 'fldXXXXXXXXXXXXXX',
/**
* Optional. To link an existing record when one matches, provide all three settings:
*
* - The ID of the Airtable field to populate when a new linked record is created.
* - The name of that same Airtable field, used by Airtable's matching formula.
* - A Gravity Forms merge-tag template that resolves to the value to match.
*
* Omit all three settings to retain the original always-create behavior.
*/
'match_field_id' => 'fldXXXXXXXXXXXXXX',
'match_field_name' => 'Name',
'match_value' => '{Name (First):3.3} {Name (Last):3.6}',
/**
* Change this to an array of value mappings.
* The keys are Airtable field IDs. Values can be Gravity Forms field IDs
* or merge-tag templates.
* These values are only written when a new linked record is created.
*/
'value_mappings' => array(
'fldXXXXXXXXXXXXXX' => 3, // Map to the raw value of Gravity Forms field 3.
'fldYYYYYYYYYYYYYY' => '{Booking time:4:startDate,format[yyyy-MM-dd]}',
// Add more mappings as needed
),
)
);