Workflow Action: Copy value from another field
-
It would be useful in the workflow configurations to be able to copy a value from one field to another. My current use case is that there is a connections field for who is the "Digital Discipler" of a contact. When that is changed, we want to add those contact connections to the sub-assigned field. I could see this also being useful for other connections fields being copied to the sub-assigned field as well.
-
I was able to implement my own custom action that does this specifically for the sub-assigned field. The UI only allows the selection of a single field to perform an action on, so I used that as the source of where I want to copy a value from. This action will always copy it to the subassigned field but could be modified to alter other fields:
add_filter( 'dt_workflows_custom_actions', function ( $actions ) { $actions[] = (object) [ 'id' => 'mtm_copy_to_subassigned', 'name' => 'Copy Relation Field to Sub-assigned', 'displayed' => true // Within admin workflow builder view? ]; return $actions; }, 10, 1 ); add_action( 'mtm_copy_to_subassigned', function ( $post, $field, $value ) { if ( !empty( $post ) && $post['post_type'] === 'contacts' ) { $updated_post = []; $updated_post['subassigned']['values'] = []; if ( !empty( $post[$field] ) ) { foreach ( $post[$field] as $connection ) { $updated_post['subassigned']['values'][] = [ 'value' => $connection['ID'], ]; } } // Assuming we have updated fields, proceed with post update! if ( !empty( $updated_post['subassigned']['values'] ) ) { $updated_post['subassigned']['force_values'] = true; DT_Posts::update_post($post['post_type'], $post['ID'], $updated_post, false, false); } } }, 10, 3 );