'Dynamics 365 Field Service', 'tasks' => array( 'create_work_order' => __( 'Create Work Order', 'advanced-form-integration' ), 'create_service_request' => __( 'Create Service Request (Work Order)', 'advanced-form-integration' ), ), ); return $actions; } public function register_settings_tab( $providers ) { $providers['dynamics365fieldservice'] = 'Dynamics 365 Field Service'; return $providers; } public function settings_view( $current_tab ) { if ( 'dynamics365fieldservice' !== $current_tab ) { return; } $crm_url = admin_url( 'admin.php?page=advanced-form-integration-settings&tab=dynamics365' ); ?>

Dynamics 365 CRM tab.', 'advanced-form-integration' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $crm_url ) ); ?>


__( 'Security check failed', 'advanced-form-integration' ) ) ); } $task = isset( $_POST['task'] ) ? sanitize_text_field( wp_unslash( $_POST['task'] ) ) : 'create_work_order'; wp_send_json_success( $this->get_fields_for_task( $task ) ); } protected function get_fields_for_task( $task ) { // Work Order and Service Request share most fields. The difference // is conceptual (worktype) — kept as separate tasks so users get // a clean menu option for each flow. return array( // Customer (Service Account is required on Work Orders) array( 'key' => 'account_name', 'value' => __( 'Service Account Name (required — looks up Account)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'contact_email', 'value' => __( 'Primary Contact Email (optional — looks up Contact)', 'advanced-form-integration' ) ), // Work order fields array( 'key' => 'msdyn_workordersummary', 'value' => __( 'Work Order Summary', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'msdyn_primaryincidentdescription', 'value' => __( 'Primary Incident Description', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_incidenttype_name', 'value' => __( 'Incident Type Name (looks up msdyn_incidenttype)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_workordertype_name', 'value' => __( 'Work Order Type Name (looks up msdyn_workordertype)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_priority_name', 'value' => __( 'Priority Name (looks up msdyn_priority)', 'advanced-form-integration' ) ), // Scheduling array( 'key' => 'msdyn_timefrompromised', 'value' => __( 'Promised Window From (ISO datetime)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_timetopromised', 'value' => __( 'Promised Window To (ISO datetime)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_datewindowstart', 'value' => __( 'Service Window Start (YYYY-MM-DD)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_datewindowend', 'value' => __( 'Service Window End (YYYY-MM-DD)', 'advanced-form-integration' ) ), // Address (the work site, separate from the Account address) array( 'key' => 'msdyn_address1', 'value' => __( 'Address Line 1', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_address2', 'value' => __( 'Address Line 2', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_city', 'value' => __( 'City', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_stateorprovince', 'value' => __( 'State / Province', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_postalcode', 'value' => __( 'Postal Code', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_country', 'value' => __( 'Country', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_latitude', 'value' => __( 'Latitude (decimal)', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_longitude', 'value' => __( 'Longitude (decimal)', 'advanced-form-integration' ) ), // Misc array( 'key' => 'msdyn_instructions', 'value' => __( 'Work Order Instructions', 'advanced-form-integration' ) ), array( 'key' => 'msdyn_totalestimatedduration', 'value' => __( 'Estimated Duration (minutes)', 'advanced-form-integration' ) ), array( 'key' => 'ownerid', 'value' => __( 'Owner ID (systemuser or team GUID)', 'advanced-form-integration' ) ), ); } /** * Resolve a Field Service related entity by name on a `_name` column. * Used for incident type, work order type, priority — all of which are * Field Service catalog entities the user references by name in forms. */ public function find_by_name( $d, $entity_set, $name, $record, $pk = '' ) { if ( ! $name ) { return array(); } // The primary key is the singular table name + "id" (e.g. // msdyn_priorities → msdyn_priorityid), not the entity set + "id". if ( ! $pk ) { $pk = preg_replace( '/ies$/', 'y', $entity_set ); $pk = ( $pk === $entity_set ? preg_replace( '/s$/', '', $entity_set ) : $pk ) . 'id'; } $endpoint = $entity_set . '?' . http_build_query( array( '$select' => $pk . ',msdyn_name', '$filter' => "msdyn_name eq '" . str_replace( "'", "''", $name ) . "'", '$top' => 1, ) ); // Try msdyn_name first (the FS convention). Fallback to `name`. $resp = $d->dynamics365_request( $endpoint, 'GET', array(), $record ); if ( ! is_wp_error( $resp ) ) { $body = json_decode( wp_remote_retrieve_body( $resp ), true ); if ( ! empty( $body['value'][0] ) ) { return $body['value'][0]; } } return array(); } } ADFOIN_Dynamics365FieldService::get_instance(); /* ---------- Dispatch ---------- */ function adfoin_dynamics365fieldservice_job_queue( $data ) { if ( ! isset( $data['record'], $data['posted_data'] ) ) { return; } adfoin_dynamics365fieldservice_send_data( $data['record'], $data['posted_data'] ); } add_action( 'adfoin_dynamics365fieldservice_job_queue', 'adfoin_dynamics365fieldservice_job_queue', 10, 1 ); add_action( 'adfoin_job_queue', 'adfoin_dynamics365fieldservice_job_queue', 10, 1 ); function adfoin_dynamics365fieldservice_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( isset( $record_data['action_data']['cl'] ) && function_exists( 'adfoin_check_conditional_logic' ) && adfoin_check_conditional_logic( $record_data['action_data']['cl'], $posted_data ) ) { return; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : $record_data; $cred_id = $field_data['credId'] ?? ( $record['cred_id'] ?? '' ); $task = $record['task'] ?? ''; if ( ! $cred_id || ! $task ) { return; } if ( ! class_exists( 'ADFOIN_Dynamics365' ) ) { return; } $d = ADFOIN_Dynamics365::get_instance(); $d->set_credentials( $cred_id ); if ( ! $d->ensure_token() ) { return; } $values = array(); foreach ( $field_data as $key => $value ) { if ( in_array( $key, array( 'credId', 'cl' ), true ) ) { continue; } $parsed = adfoin_get_parsed_values( $value, $posted_data ); if ( $parsed !== '' && $parsed !== null ) { $values[ $key ] = $parsed; } } $fs = ADFOIN_Dynamics365FieldService::get_instance(); // Service Account is required on Work Orders. // Integrations saved before these keys were corrected used column names // that don't exist on msdyn_workorder; send them to the real columns. $legacy_keys = array( 'msdyn_subject' => 'msdyn_workordersummary', 'msdyn_description' => 'msdyn_primaryincidentdescription', 'msdyn_addressline1' => 'msdyn_address1', 'msdyn_addressline2' => 'msdyn_address2', 'msdyn_workorderinstructions' => 'msdyn_instructions', ); foreach ( $legacy_keys as $old_key => $new_key ) { if ( isset( $values[ $old_key ] ) ) { if ( ! isset( $values[ $new_key ] ) ) { $values[ $new_key ] = $values[ $old_key ]; } unset( $values[ $old_key ] ); } } $account_name = $values['account_name'] ?? ''; $account = $account_name ? $d->find_account_by_name( $account_name, $record ) : array(); if ( empty( $account['accountid'] ) ) { return; } // Optional Primary Contact $contact_id = ''; if ( ! empty( $values['contact_email'] ) ) { $existing = $d->find_record_by_email( 'contacts', $values['contact_email'], $record ); $contact_id = $existing['contactid'] ?? ''; } // Strip resolution helpers + the *_name lookups from the payload before // building it — we'll convert them into @odata.bind references below. $lookup_inputs = array( 'account_name', 'contact_email', 'msdyn_incidenttype_name', 'msdyn_workordertype_name', 'msdyn_priority_name', ); $name_lookups = array( // form key => array( entity set, lookup column on msdyn_workorder, primary key ) 'msdyn_incidenttype_name' => array( 'msdyn_incidenttypes', 'msdyn_primaryincidenttype', 'msdyn_incidenttypeid' ), 'msdyn_workordertype_name' => array( 'msdyn_workordertypes', 'msdyn_workordertype', 'msdyn_workordertypeid' ), 'msdyn_priority_name' => array( 'msdyn_priorities', 'msdyn_priority', 'msdyn_priorityid' ), ); $bindings = array(); foreach ( $name_lookups as $form_key => $info ) { list( $entity_set, $field_prefix, $pk ) = $info; if ( empty( $values[ $form_key ] ) ) { continue; } $matched = $fs->find_by_name( $d, $entity_set, $values[ $form_key ], $record, $pk ); if ( ! empty( $matched[ $pk ] ) ) { $bindings[ $field_prefix . '@odata.bind' ] = '/' . $entity_set . '(' . $matched[ $pk ] . ')'; } } foreach ( $lookup_inputs as $k ) { unset( $values[ $k ] ); } // Build payload with general coercion + Field-Service-specific cleanups. $payload = $d->finalize_payload( 'contact', $values ); foreach ( array( 'msdyn_latitude', 'msdyn_longitude' ) as $float_key ) { if ( isset( $payload[ $float_key ] ) ) { $payload[ $float_key ] = (float) $payload[ $float_key ]; } } if ( isset( $payload['msdyn_totalestimatedduration'] ) ) { $payload['msdyn_totalestimatedduration'] = (int) $payload['msdyn_totalestimatedduration']; } // Required service account binding. $payload['msdyn_serviceaccount@odata.bind'] = '/accounts(' . $account['accountid'] . ')'; if ( $contact_id ) { $payload['msdyn_reportedbycontact@odata.bind'] = '/contacts(' . $contact_id . ')'; } // Apply optionset / type bindings resolved above. foreach ( $bindings as $bind_key => $bind_value ) { $payload[ $bind_key ] = $bind_value; } // msdyn_workordertype is required — if the user didn't pick one and // none was auto-bound, Dynamics will reject the create. Surface that // via the standard error logging, don't gate locally. if ( empty( $payload['msdyn_workordersummary'] ) ) { return; // work order summary is required } $d->dynamics365_request( 'msdyn_workorders', 'POST', $payload, $record ); }