Support » Plugin: Object Sync for Salesforce » Mapping to Categories from Salesforce

  • Resolved bswift

    (@bswift)


    I set up a Salesforce -> WordPress mapping from Account to a custom post type called “Store”. Ideally, I’d like to be able to set a Category for each record in WordPress based on a SF picklist field. Is that possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Jonathan Stegall

    (@jonathanstegall)

    @bswift I think it is possible, but not without using developer hooks.

    For example, we have a hook that runs 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,
        );
        */
    }
    

    I think you could use the salesforce_object attribute of that array to assign categories, perhaps with the WordPress method wp_set_post_categories.

    In any case, this plugin can’t do that within the interface because categories aren’t stored in the post or post meta table. It’s not a bad idea for a feature, but it would be rather complicated and it wouldn’t happen anytime soon.

    Howdy! I ended up helping out with this request and wanted to share the code we worked up and a bit of feedback. @jonathanstegall, thanks a ton for sending us in the right direction.

    We needed to sync a picklist field on an account with the Store Locator Plus Store Category custom taxonomy. We chose to only sync a category if the term already existed in WordPress.

    <?php
    add_action( 'object_sync_for_salesforce_pull_success', 'mrw_sf_cat_sync', 10, 3 );
    /**
     * sync salesforce pick list with store categories
     *
     * Only syncs if a term exists in WordPress already.
     * Therefore: Old term remains new picklist value is unrecognized or empty.
     * 
     * @param  string $operation     sync action (Create, Update, Upsert, Delete)
     * @param  array $result        data for post, status, and errors
     * @param  array $synced_object array containing salesforce_object, mapping_object, and mapping
     *
     * @see  https://github.com/MinnPost/object-sync-for-salesforce/blob/master/docs/extending-before-and-after-saving.md
     * @see https://wordpress.org/support/topic/mapping-to-categories-from-salesforce/#post-10761549
     */
    function mrw_sf_cat_sync( $operation, $result, $synced_object ) {
    
    	// really unsure of this part still. need to see 
    	$store_id = $result['data']['ID'];
    	$store_type = $synced_object['salesforce_object']['Our_Picklist__c'];
    	$term = term_exists( $store_type, 'wpsl_store_category' );
    
    	// return early if the $store_type is not a valid term in the Store Category taxonomy
    	if( empty( $term ) ) {
    		return;
    	}
    
    	if( in_array( strtolower( $operation ), array( 'create', 'update', 'upsert' ) ) ) {
    
    		// set the term to the post
    		// 4rd argument (false) ensures that we overwrite terms, not append them
    		wp_set_object_terms( $store_id, intval( $term['term_id'] ), 'wpsl_store_category', false );
    
    	}
    
    }

    I hope that code can help some folks!

    In doing this, I ran into a couple small issues:

    1. The $op values were capitalized (e.g. “Update”, not “update”), so I had to lowercase the value to make sure I got a strict match. I’d either update the documentation or (better!) just pass along lowercase values as the documentation applies.
    2. I had a hard time making sense of the $result array. I couldn’t find any information about what I expected it to be and it also included some stuff I didn’t understand. Here’s the start of $result following a successful update:
    array(2) {
    	["data"]=> array(2) {
    		["ID"]=> int(10715)
    		["success"]=> bool(false)
    	}
    	["errors"]=> array(13) {
    		[0]=> array(2) {
    			["key"]=> string(12) "SF_Record_ID"
    			["value"]=> array(3) {
    				["value"]=> string(18) "{valid_id}"
    				["method_modify"]=> string(16) "update_post_meta"
    				["method_read"]=> string(13) "get_post_meta"
    			}
    		}
       // etc...
    

    I wasn’t sure why success is false or what the errors array was.

    That said, I seriously appreciated the amount of documentation and hooks. I hope to use this plugin again!

    • This reply was modified 7 months, 3 weeks ago by  mrwweb.
    stupidchief

    (@stupidchief)

    The above solution works apart from this line:
    $store_id = $result['data']['ID'];

    I am mapping to a custom post type so I changed the line to:

    $store_id = $result['parent'];

    I’m using Version 1.7.0

    • This reply was modified 4 months ago by  stupidchief.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.