• Resolved kocosh

    (@kocosh)


    Hello guys,

    is there a way to create a connection between two SF objects?

    In my case, I’m syncing vouchers from WP (WooCommerce) and I need to create SF “Contact” object first and then the “Voucher” object itself, that would have a field “Customer”, refering to an actual “Contact” object. Documentation didn’t help… :/

    Is that even possible? Any piece of code that would make it easier? πŸ™‚

    Thank you very much!

    PS: Thanks a lot for the plugin! Really great job πŸ™‚

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jonathan Stegall

    (@jonathanstegall)

    Hi,

    The plugin doesn’t currently have the ability to do this. I think it’s possible that you could use the developer hooks to build such a thing – there are abundant hooks, and you can see them here: https://github.com/MinnPost/object-sync-for-salesforce/blob/master/docs/all-developer-hooks.md.

    Certainly this would be complicated, but I think it’s possible. We’d be happy to consider pull requests, and/or to link to plugin add-ons that do this, if you achieve it, but there are (at this time) no plans to add such a feature ourselves.

    Thread Starter kocosh

    (@kocosh)

    Thanks for such a prompt response… I thought so πŸ™‚

    One more question then, could I use the object_sync_for_salesforce_push_success action after the “Contact” was successfully created and before the “Voucher” will be created?

    …Is the action triggered separately for each mapping? Can I make sure that the mapping happens in the “right” order?

    So that I could grab the object id returned from the first fieldmapping and modify the data for the second..?

    Thank you once again!

    • This reply was modified 8 years, 1 month ago by kocosh.
    Plugin Author Jonathan Stegall

    (@jonathanstegall)

    I haven’t tried anything like this, but maybe? Here’s how a push trigger works, basically:

    1. An event happens in WordPress that fires one of the built-in hooks (save_post, user_register, profile_update, etc)
    2. The plugin tries to get all of the available data it can for that specific item (so all the core fields and meta fields).
    3. If it is async, it puts it into the queue.
    4. Either immediately, or when cron runs, the plugin gets the fieldmap for that WordPress object ID, and it tries to map the WordPress field values to the Salesforce field IDs that are in the mapping.
    5. The plugin creates or updates an object map between the two objects.
    6. The plugin runs the query to send the data to the Salesforce API.
    7. If the API call is successful, it runs the object_sync_for_salesforce_push_success action.

    However that success action doesn’t return anything; it’s just an action, so you couldn’t use it to get the ID, but you maybe could get it some other way.

    The mapping order is triggered by the order of the WordPress event that causes it. So if you only have one object in WordPress, it’s only going to run one time, when that one object is saved. So maybe you could use the success action to get data from another fieldmap. I’ve certainly not used it to do anything like this, and I think you might have to use multiple hooks to achieve it.

    Thread Starter kocosh

    (@kocosh)

    So I solved it by deleting the Account mapping (keeping the Voucher) and creating the Account object manually instead.

    For the future reference, here’s my quick and dirty solution:

    Using object_sync_for_salesforce_push_params_modify filter…

    add_filter('object_sync_for_salesforce_push_params_modify', 'before_push_to_sf', 10, 6);
    
    function before_push_to_sf($params, $mapping, $object, $sf_sync_trigger, $use_soap, $is_new) {

    Getting Voucher ID, WC Order ID…

      $wp_id = $object['ID'];
      $wc_id = $object['_order_id'];
      $sf_id = '';

    Manually pushing Account…

      $salesforce = Object_Sync_Salesforce::get_instance();
      $salesforce_api = $salesforce->salesforce['sfapi'];
    
    	if (is_object($salesforce_api)) {
        $accountParams = array(
          'LastName' => get_post_meta($wc_id, '_billing_last_name', true),
          'FirstName' => get_post_meta($wc_id, '_billing_first_name', true),
          'PersonEmail' => get_post_meta($wp_id, '_purchaser_email', true),
          'PersonMobilePhone' => get_post_meta($wc_id, '_billing_phone', true),
    
          'BillingStreet' => get_post_meta($wc_id, '_billing_address_1', true),
          'BillingCity' => get_post_meta($wc_id, '_billing_city', true),
          'BillingPostalCode' => get_post_meta($wc_id, '_billing_postcode', true)
        );
    
        if(!get_post_meta($wp_id, '_sf_account_id', true)) {
          $result = $salesforce_api->object_create('Account', $accountParams);
    
          if($result['data']['id']) {
            $sf_id = $result['data']['id'];
            // SAVE PURCHASER ACCOUNT'S ID AS META
            add_post_meta($wp_id, '_sf_account_id', $sf_id, true);
          } else {
            // SOMETHING WENT WRONG
          }
    
        } else {
          $sf_id = get_post_meta($wp_id, '_sf_account_id', true);
          $result = $salesforce_api->object_upsert('Account', 'Id', $sf_id, $accountParams);
    
          if($result['data']['success'] == 1) {
            // ALL GOOD
          } else {
            // SOMETHING WENT WRONG
          }
        }
    	}

    And using ID of the newly created SF object for the relationship field like this…

    $params['Customer__c'] = $sf_id;

    • This reply was modified 8 years, 1 month ago by kocosh.
    • This reply was modified 8 years, 1 month ago by kocosh.
    Plugin Author Jonathan Stegall

    (@jonathanstegall)

    I think this seems reasonable, though I do think it would be good for us to try to facilitate this kind of thing within the plugin at some point as well.

    It gets a good bit more complicated for the plugin when dealing with the bigger systems like WooCommerce that have their own ways of handling data.

    In any case, I’ve filed a GitHub issue to try to articulate what the need might be. https://github.com/MinnPost/object-sync-for-salesforce/issues/171

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Relationship between SF objects’ is closed to new replies.