Salesforce Pull Conditional
-
Is there a way to pull a Contact from Salesforce only if a field has a specific value?
Eg, there are 500 Contacts, but only 50 of the have the pull_to_wordpress field set to ‘true’, so skip all the rest.
-
@amddtim yes, there’s a
object_sync_for_salesforce_pull_object_allowed
hook you can use.Without knowing exactly how your Salesforce works, it would be something like this if you have a Pull_to_wordpress__c field in Salesforce with a string value of true (as long as the object has a fieldmap).
add_filter( 'object_sync_for_salesforce_pull_object_allowed', 'is_pull_allowed', 10, 5 ); // can always reduce this number if all the arguments are not necessary function is_pull_allowed( $pull_allowed, $object_type, $object, $sf_sync_trigger, $salesforce_mapping ) { if ( 'true' === $object['Pull_to_wordpress__c'] ) { return true; } else { return false; } }
Okay, got it working. But now I’m thinking there may be an issue. If I’m using that field to determine whether a post should exist on the WordPress end, it’s not going to delete it if it already exists and ‘Pull_to_wordpress’ eventually gets set to ‘false’ in Salesforce, right?
Yes, that’s true. This plugin will only delete things from WordPress if they are also deleted from Salesforce.
But there is a hook you could maybe use for that, the
object_sync_for_salesforce_pull_success
. You can’t use the pull allowed hook with this one, you’d have to choose. But this one works like this:add_action( 'object_sync_for_salesforce_pull_success', 'pull_success', 10, 3 ); function pull_success( $op, $result, $synced_object ) { // do things if the save succeeded // $op is what the plugin did - create, update, upsert, delete // $result is what was returned by the $wordpress class // $synced_object is an array like this: /* $synced_object = array( 'salesforce_object' => $object, 'mapping_object' => $mapping_object, 'mapping' => $mapping, ); */ }
You’d also probably want to delete the mapping object itself, since the plugin can’t map to WordPress objects that don’t exist. This is a big more complicated, as you’d have to get yourself an instance of this plugin and call it. So you could say something like this, maybe:
add_action( 'object_sync_for_salesforce_pull_success', 'pull_success', 10, 3 ); function pull_success( $op, $result, $synced_object ) { if ( 'false' === $synced_object['salesforce_object']['Pull_to_wordpress__c'] ) { wp_delete_post( $synced_object['mapping_object']['wordpress_id'] ); // get the base class if ( ! function_exists( 'is_plugin_active' ) ) { require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); } if ( is_plugin_active( 'object-sync-for-salesforce/object-sync-for-salesforce.php' ) ) { require_once plugin_dir_path( __FILE__ ) . '../object-sync-for-salesforce/object-sync-for-salesforce.php'; $salesforce = Object_Sync_Salesforce::get_instance(); $this->mappings->delete_object_map( $synced_object['mapping_object']['id'] ); } } }
This is untested, so you’d want to make sure it does what you want it to do, as it has a lot of pieces.
Hmm, maybe I need to go about this another way. Instead of using that field to determine whether a WordPress post should exist, can I publish or unpublish the post depending on the Salesforce value? I noticed that when creating a new post in WordPress, it’s set to ‘draft’ by default. At the very least, I need new posts to be published so that I don’t need to manually publish them.
Yes, you can use part of the code above to do that, if you want. You would then not need to delete the Salesforce field map.
It would be something like this, I suppose:
add_action( 'object_sync_for_salesforce_pull_success', 'pull_success', 10, 3 ); function pull_success( $op, $result, $synced_object ) { if ( 'false' === $synced_object['salesforce_object']['Pull_to_wordpress__c'] ) { $post_id = $synced_object['mapping_object']['wordpress_id']; $current_post = get_post( $post_id, 'ARRAY_A' ); $current_post['post_status'] = 'draft'; // or whatever status wp_update_post($current_post); } }
Okay, cool. I’ll roll with this and see how it goes.
I’m realizing that $synced_object[‘mapping_object’] is empty when a WordPress post has been created for the first time (as opposed to being updated), which sort of defeats my purpose for attempting to publish the WordPress post on this successful pull filter. Does that sound correct?
Yes. That item is created before data is added to the pull queue. The mapping object for new data isn’t created until later, when the queue is being processed.
So if I’m trying to get the WordPress post id to either publish it or set to draft, is it best to use $result[‘data’][‘ID’] instead for the object_sync_for_salesforce_pull_success filter? Will that always be available?
I think so, yes. I think it’s good to run some logs to see what all is happening before you act on it.
Something like:
add_action( 'object_sync_for_salesforce_pull_success', 'show_me_stuff', 10, 3 ); function show_me_stuff( $op, $result, $synced_object ) { error_log( 'op is ' . $op . ' and result is ' . print_r( $result, true ) . ' and synced object is ' . print_r( $synced_object, true ) ); }
Thanks, I’ll go with that. I’ve been printing the results in a debug.log all along, which is how I saw what was going on.
- The topic ‘Salesforce Pull Conditional’ is closed to new replies.