Changing a field type without losing existing entry data

Hi,

I have a form with a dropdown field that I’ve used for years. It’s collected lots of valuable sign-up data.

However, from now on, I’d like to switch the field type to ‘multiple select’ so users can select more than one option.

PROBLEM: I don’t want to lose my existing entry data.

Researching this forum, I found this query and response from @david

BUT…having followed the tutorial using a dummy form and fields…YES, the field IDs were swapped perfectly…

BUT, I lost the dummy data.

Here’s what I did:

  1. On a dummy form, I created 2 fields: Dropdown (ID 1), Multiselect (ID2) - both with identical choices.

  2. Created a dummy entry by selecting a dropdown value in the Dropdown field and clicking submit button (I checked the entry, and the field value was recorded as expected).

  3. Changed Multiselect field ID from 2 > 1 using function: gwChangeFieldId( 2, 1 ); (*by following the tutorial copied earlier in this message)

  4. Deleted the Dropdown field (original ID 1), leaving only the ‘Multi-select’ field behind (now ID 1)

  5. Checked entries - the entry is visible, but the associated field values are wiped clean - no record

…How would I perform this whilst STILL RETAINING the original entry field values?

Please help…

Thank you.

Hi Temi,

The data was lost when you deleted the Drop Down field. Gravity Forms deletes all entry values saved under that field ID. After the ID swap, both fields had ID 1, so the old values were removed.

A better approach would be to keep the Drop Down field and change only its type to Multi Select. The field ID would remain the same, so your old entry values would be preserved.

Please take a database backup first and test on a copy if you can.

  1. Add this snippet to your theme functions.php or a snippets plugin.
  2. Change $form_id and $field_id to your values.
  3. Log in as admin and open /wp-admin/?gf_convert_field=1 one time.
  4. Remove the snippet.
add_action( 'admin_init', function () {
	if ( rgget( 'gf_convert_field' ) !== '1' || ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
		return;
	}

	$form_id  = 1; // Your form ID.
	$field_id = 1; // Your Drop Down field ID.

	$form  = GFAPI::get_form( $form_id );
	$field = GFAPI::get_field( $form, $field_id );
	if ( ! $field || $field->type !== 'select' ) {
		wp_die( 'Field not found or not a Drop Down.' );
	}

	// Change type, keep same field ID.
	$field->type        = 'multiselect';
	$field->storageType = 'json';
	$result             = GFAPI::update_form( $form );
	if ( is_wp_error( $result ) ) {
		wp_die( $result->get_error_message() );
	}

	// Save old values in Multi Select format.
	global $wpdb;
	$table   = GFFormsModel::get_entry_meta_table_name();
	$rows    = $wpdb->get_results( $wpdb->prepare( "SELECT id, meta_value FROM {$table} WHERE form_id = %d AND meta_key = %s", $form_id, $field_id ) );
	$updated = 0;
	foreach ( $rows as $row ) {
		if ( $row->meta_value === '' || $row->meta_value[0] === '[' ) {
			continue;
		}
		$wpdb->update( $table, array( 'meta_value' => json_encode( array( $row->meta_value ) ) ), array( 'id' => $row->id ) );
		$updated++;
	}

	wp_die( sprintf( 'Done. %d entry values updated. Please remove this snippet.', $updated ) );
} );

After this, please check any conditional logic, notifications or feeds that use this field. It can have more than one value now.

Hope this helps.

Dear, Dear, Dearest Faisal!

Thank you for the help you’ve given. I appreciate it deeply.

You lent your professional assistance and that without persuasion or any hope of returns.

Thank you once again.

Your script worked perfectly!

(And the confirmation screen at the end was a lovely touch too!)

Months of agonising circling around the issue, but never quite fixing it, have come to an end.

One small thing, the resulting stored entry data is now wrapped in [“…”] rather than just the text.

i.e. [“New York”] , rather than just: New York.

Any idea why?

But no issue, I can live with it.

Like I said - this is a great help, Faisal. Thank you!

Temi

Hi Faisal,

I just tried it in my main form and it came up with a peculiar issue (though this worked perfectly in the test form).

In the editor, the field is a ‘Multi-select’ - BUT on the front end it is a ‘Dropdown’ still.

Please can you help??

Thank you

Hi Temi,

Happy it helped!

About [“New York”]: this is normal. Gravity Forms saves every Multi Select value like this, so new entries will look the same. Gravity Forms screens, merge tags and exports show it as plain text (New York). You only see the brackets if you look in the database directly, or in another plugin that reads the raw value.

About the front end: your main form field is older and has one more setting saved (inputType) that still says “select”. The front end reads this setting first, so it still shows a Drop Down. The first snippet did not change it, sorry about that.

This small snippet fixes it. Your entry data is already converted, so this only changes the field setting.

  1. Take a database backup.
  2. Add the snippet and change $form_id and $field_id.
  3. Log in as admin and open /wp-admin/?gf_fix_field=1 one time.
  4. Remove the snippet.
add_action( 'admin_init', function () {
	if ( rgget( 'gf_fix_field' ) !== '1' || ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
		return;
	}

	$form_id  = 1; // Your form ID.
	$field_id = 1; // Your field ID.

	$form  = GFAPI::get_form( $form_id );
	$field = GFAPI::get_field( $form, $field_id );
	if ( ! $field || $field->type !== 'multiselect' ) {
		wp_die( 'Field not found or not a Multi Select.' );
	}

	$field->inputType = 'multiselect';
	$result           = GFAPI::update_form( $form );

	wp_die( is_wp_error( $result ) ? $result->get_error_message() : 'Done. Please remove this snippet.' );
} );

If you use a cache plugin or CDN, please clear the cache after this.

If it still shows a Drop Down, please share a form export (Forms > Import/Export > Export Forms) and I will check.