MinnPost / object-sync-for-salesforce

WordPress plugin that maps and syncs data between Salesforce objects and WordPress objects.
https://wordpress.org/plugins/object-sync-for-salesforce/
GNU General Public License v2.0
95 stars 51 forks source link

Date/Time to ACF Syncing Issue? #519

Closed tealmedia-joey closed 8 months ago

tealmedia-joey commented 1 year ago

Describe the bug When attempting to sync a date/time field from Salesforce into an ACF field, the best I can get is the month.

I confirmed that the format coming from Salesforce is the following: [Start_Day_Time__c] => 2020-12-17T00:00:00.000+0000

I've tried several ACF field types to try to get this to work:

Wondering if there's a way to just get the date/time info as is (using object_sync_for_salesforce_pull_params_modify from https://github.com/MinnPost/object-sync-for-salesforce/blob/master/docs/extending-parameters.md#code-example-1)?

Expected behavior The date and/or time value from Salesforce

Environment (please complete the following information):

jonathanstegall commented 1 year ago

As far as I know, no one is currently working on this plugin regularly (I left MinnPost, though), but I think what you'll want to do is run it locally and see what the value is after the plugin attempts to handle the date formats, but right before the plugin attempts to save it. I can't remember exactly where that happens, but I'd start here and do some error logging. It's definitely going to be in that class.

If it's ACF-specific and it is a bug that you can fix without affecting how the plugin handles any other date destinations in WordPress, you can submit it as a pull request. I can't promise I'll get to it quickly, but will try to get to it. If it's not ACF-specific but is a bug in something different that Salesforce is doing, it's going to be more complicated since I don't have the testing setup I used to have, or the capacity to work on it. You might check with MinnPost about that and see what their plans are.

You could also try using that pull params modify hook and see what the value is at that point. That would be the easiest place to fix it, unless there's already a problem of some kind by the time the value gets to that method. In that case it would seem to be an ACF issue that would have to be caught earlier

tealmedia-joey commented 1 year ago

Thanks @jonathanstegall - I ended up using the object_sync_for_salesforce_pull_success action to update my post meta with the day/time value from Salesforce (using the error_log function calls really helped to show me what was happening and where:

function tm_sf_pull_success( $op, $result, $synced_object ) {
    // error_log( print_r( 'here', true ) ); // array
    // error_log( print_r( $result, true ) ); // array
    // error_log( print_r( $synced_object, true ) ); // int

    if($synced_object['salesforce_object']['Start_Day_Time__c']) {
        $start_date_obj = new DateTime($synced_object['salesforce_object']['Start_Day_Time__c']);

        update_post_meta($result['parent'], 'details_start_date', $synced_object['salesforce_object']['Start_Day_Time__c']);
        update_post_meta($result['parent'], 'details_event_timezone', $synced_object['salesforce_object']['Event_Time_Zone__c']);
        update_post_meta($result['parent'], 'details_start_date_edit', $start_date_obj->format('U'));
        update_post_meta($result['parent'], 'details_start_time_edit', $start_date_obj->format('H:i:s'));

        // set start date text taxonomy
        $date_text = $start_date_obj->format('l');
        wp_set_object_terms($result['parent'], strtolower($date_text), 'meeting_day_tax');

        // publish the event
        $synced_post = array(
            'ID' => $result['parent'],
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s', $start_date_obj->format('U')),
            'post_date_gmt' => gmdate('Y-m-d H:i:s', $start_date_obj->format('U')),
            'edit_date' => true,
        );
        wp_update_post($synced_post);
    }

    if($synced_object['salesforce_object']['End_Date_Time__c']) {
        $end_date_obj = new DateTime($synced_object['salesforce_object']['End_Date_Time__c']);
        $end_time = $end_date_obj->format('H:i:s');

        update_post_meta($result['parent'], 'details_end_date', $synced_object['salesforce_object']['End_Date_Time__c']);
        update_post_meta($result['parent'], 'details_end_time_edit', $end_time);
    }

    // set meeting type taxonomy
    if($synced_object['salesforce_object']['Event_Meeting_Type__c']) {
        $type_array = explode(';', $synced_object['salesforce_object']['Event_Meeting_Type__c']);

        wp_set_object_terms($result['parent'], $type_array, 'meeting_type_tax');
    }
}
add_action('object_sync_for_salesforce_pull_success', 'tm_sf_pull_success', 10, 3);