2013-04-08 82 views
0

该函数将自定义帖子“事件”数据添加到Salesforce数据库中。我已经测试了Wordpress以外的功能,它的工作完美无瑕。当我在Wordpress中通过添加一个新事件来测试它时,没有生成错误,并且数据没有插入到SF数据库中。我也通过打印出$ _POST来测试,并看到数据正在被收集。我怎样才能让这个显示出现一些错误,以便我能够解决这个问题?将wordpress自定义帖子类型数据添加到外部数据库

function add_campaign_to_SF($post_id) { 
    global $SF_USERNAME; 
    global $SF_PASSWORD; 

    if ('event' == $_POST['post-type']) { 
     try { 
       $mySforceConnection = new SforceEnterpriseClient(); 
       $mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml'); 
       $mySFlogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD); 

       $sObject = new stdclass(); 
       $sObject->Name = get_the_title($post_id); 
       $sObject->StartDate = date("Y-m-d", strtotime($_POST["events_startdate"])); 
       $sObject->EndDate = date("Y-m-d", strtotime($_POST["events_enddate"])); 
       $sObject->IsActive = '1'; 

       $createResponse = $mySforceConnection->create(array($sObject), 'Campaign'); 

       $ids = array(); 
        foreach ($createResponse as $createResult) { 
         error_log($createResult); 
         array_push($ids, $createResult->id); 
        } 

       } catch (Exception $e) { 
         error_log($mySforceConnection->getLastRequest()); 
         error_log($e->faultstring); 
         die; 
        } 
    } 
} 

add_action('save_post', 'add_campaign_to_SF'); 

回答

1

我会用get_post_type()检查“事件”的帖子。使用error_log()写入PHP错误日志中的其他调试 - 检查您的Salesforce登录的状态等

记住save_post运行时间后保存 - 创建或更新 - 所以你可能想在Salesforce中创建新的Campaign之前进行一些额外的检查(如设置元值),否则最终会出现重复项。

function add_campaign_to_SF($post_id) { 
    $debug = true; 
    if ($debug) error_log("Running save_post function add_campaign_to_SF($post_id)"); 
    if ('event' == get_post_type($post_id)){ 
     if ($debug) error_log("The post type is 'event'"); 
     if (false === get_post_meta($post_id, 'sfdc_id', true)){ 
      if ($debug) error_log("There is no meta value for 'sfdc_id'"); 
      // add to Salesforce, get back the ID of the new Campaign object 
      if ($debug) error_log("The new object ID is $sfdc_id"); 
      update_post_meta($post_id, 'sfdc_id', $sfdc_id); 
     } 
    } 
} 
add_action('save_post', 'add_campaign_to_SF'); 
+0

使用此wp_insert_post_data过滤器是一个更好的选择吗? – MG1 2013-04-09 00:19:39

+0

这也得到更新调用...我会设置一个元值,指示您是否已经成功创建了一个对象在SFDC中,ID可能是 – doublesharp 2013-04-09 00:20:52

+0

您可以展示这将如何完成? – MG1 2013-04-09 00:23:01

相关问题