2013-04-08 84 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

相關問題