• Resolved danatpi

    (@danatpi)


    I’m using the object_sync_for_salesforce_push_success hook to run a custom query and then update some user meta after a successful contact/lead sync. This works as intended, but for some reason creates a pending object_sync_for_salesforce_push_record action in the scheduled action that remains indefinitely until it is cancelled manually.

    Any idea what would be causing this additional action or how it could be prevented?

    Thanks,
    Dan

    /**
     * SF: Update user meta
     */
    function update_user_meta( $op, $response, $synced_object, $object_id, $wordpress_id_field_name ) {
        // do things if the save succeeded
        // $op is what the plugin did - Create, Update, Upsert, Delete
        // $response is what was returned by the $salesforce class. sfapi->response
        // $synced_object is an array like this:
        /*
        $synced_object = array(
            'wordpress_object' => $object,
            'mapping_object' => $mapping_object,
            'queue_item' => false,
            'mapping' => $mapping,
        );
        */
        // $object_id is the salesforce object id
        // $wordpress_id_field_name is the name of the ID field in WordPress
    
        if($synced_object['mapping']['wordpress_object'] === 'user') {
    
            $user_id = $synced_object['wordpress_object']['ID'];
    
            $salesforce = Object_Sync_Salesforce::get_instance();
            $salesforce_api = $salesforce->salesforce['sfapi'];
    
            if ( is_object( $salesforce_api ) ) {
                $query = "SELECT Contact_s_Account_Type__c, Account.Name, FirstName, LastName FROM Contact WHERE ID = '$object_id'";
                $result = $salesforce_api->query( $query );
    
                if ( $result['data']['totalSize'] > 0 ) {
                    $account_type = $result['data']['records'][0]['Contact_s_Account_Type__c'];
                    $account_name = $result['data']['records'][0]['Account']['Name'];
                    $first_name = $result['data']['records'][0]['FirstName'];
                    $last_name = $result['data']['records'][0]['LastName'];
                    if(isset($account_type)) update_user_meta( $user_id, 'user_account_type', $account_type);
                    if(isset($account_name)) update_user_meta( $user_id, 'user_company', $account_name);
                    if(isset($first_name)) update_user_meta( $user_id, 'first_name', $first_name);
                    if(isset($last_name)) update_user_meta( $user_id, 'last_name', $last_name);
                    if(isset($first_name) && isset($last_name)) wp_update_user( array( 'ID' => $user_id, 'display_name' => $first_name . ' ' . $last_name) );
                } else {
                    update_user_meta( $user_id, 'user_account_type', 'Lead');
                }
            }
    
        }
    
    }
    add_action( 'object_sync_for_salesforce_push_success', __NAMESPACE__ . '\\update_user_meta', 10, 5 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jonathan Stegall

    (@jonathanstegall)

    I’m not sure why this is happening. My first question is does it happen when it runs both wp_update_user and when it runs update_user_meta?

    If it only happens with wp_update_user, I think that is because that method in WordPress Core eventually calls wp_insert_user, which itself runs the profile_update action. When the profile_update action runs, this plugin runs its push trigger operations. I would expect it to clear out on its own eventually, so that’s why I’m not sure what is happening.

    If it also happens with update_user_meta, I’m even less sure. That method causes some other actions to run but this plugin does not directly hook into those.

    Thread Starter danatpi

    (@danatpi)

    Thanks for the reply Jonathan. It occurs with any instance of update_user_meta.

    I was able to circumvent this issue by hooking into an adjacent function (acf/update_value/) that was updating some user meta via Advanced Custom Fields.

    I never was able to determine what was causing items to be queued indefinitely but moving that portion of the code elsewhere solved the problem.

    Thanks again for the reply and for all the time you contribute to this plugin, it’s a lifesaver.

    -Dan

    • This reply was modified 4 years, 5 months ago by danatpi.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom query within hook creates duplicate action’ is closed to new replies.