Disciple.Tools Community D.T Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Disicple.Tools
    • Register
    • Login

    Workflow Action: Copy value from another field

    Scheduled Pinned Locked Moved Feature Requests
    2 Posts 1 Posters 216 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • CairoCoderC Offline
      CairoCoder
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • CairoCoderC Offline
        CairoCoder
        last edited by CairoCoder

        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 );
        
        1 Reply Last reply Reply Quote 1
        • First post
          Last post