2011-07-15 17 views
0

我有以下代碼,它在Salesforce中創建一個任務,然後跟蹤用戶的瀏覽歷史並將其存儲在SalesForce中。目前,它將用戶瀏覽過的每個頁面顯示爲一個單獨的條目。我想在Browsing_History__c對象中將所有這些條目組合在一起,而不是每次用戶訪問頁面時創建的任務。使用C#SalesForce中的Upsert函數在自定義對象中綁定鏈接?

任何幫助將不勝感激..我不太熟悉SF。 :)

private void CreateTaskInSF(string id, string type, string details, string description) 
    { 
     // if there's a similar Event in the past 2 hours, don't add it 
     QueryResult qr = null; 
     try // get events from past 2 hours 
     { 
      qr = Binding.query("Select Details__c from Task WHERE WhoId='" + id + "' and Type__c='" + type + "' and CreatedDate > " + DateTime.UtcNow.AddHours(-2).ToString("s") + "Z"); 
     } 
     catch (Exception e) 
     { 
      return; 
     } 
     bool logged = false; 
     if (qr != null) // if there are Tasks in past 2 hours 
     { 
      sforce.sObject[] browsing = qr.records; 
      if (browsing != null) 
      { 
       // iterate through events to make sure the new Task isn't logged 
       for (int i = 0; i < browsing.Length; i++) 
       { 
        Task currTask = (Task)browsing[i]; 
        if (currTask.Details__c == details) 
        { 
         if (description != "") // is there a description to check for? 
         { 
          string oldTaskDescription = ""; 
          if (currTask.Description != null) 
           oldTaskDescription = currTask.Description; 
          if (oldTaskDescription == description) // if there is a description match 
           logged = true; 
         } 
         else 
          logged = true; // there's no description, so check only on details field 
        } 
       } 
      } 
     } 
     if (logged == true) 
     { 
      return; // if Activity is already logged, don't log it again 
     } 

     else if (type == "Browsing") 
     { 
      QueryResult browsingQuery = null; 
      try // get events from past 2 hours 
      { 
       browsingQuery = Binding.query("Select Web_Browsing__c from Task WHERE WhoId='" + id + "' and Subject='" + type + "' and Details__c='" + details + "' and CreatedDate > " + DateTime.UtcNow.AddHours(-2).ToString("s") + "Z"); 
      } 
      catch 
      { 

      } 

      Boolean createNewBrowsing = false; 
      if (browsingQuery != null) // if there are Tasks in past 2 hours 
      { 
       sforce.sObject[] webBrowsing = browsingQuery.records; 
       if (webBrowsing != null) 
       { 
        //find correct object and update Browsing_History__c 
        //Binding.update 

       } 

       else 
       { 
        createNewBrowsing = true; 
       } 

      } 
      else 
      { 
       createNewBrowsing = true; 
      } 

      if (createNewBrowsing) 
      { 

       Web_Browsing__c newTask = new Web_Browsing__c(); 
       newTask.Lead__c = id; 
       newTask.Browsing_History_255__c = details; 
       newTask.Type__c = type; 

       newTask.Browsing_History__c = details; 
       newTask.CreatedDate = DateTime.Now; 
       //if(type == "Browsing") newTask. = details; 
       //SaveResult[] createResult = Binding.create(new sObject[] { newTask }); 

      try 
      { 
       SaveResult[] createResult = Binding.create(new sObject[] { newTask }); 
      } 
      catch (Exception e) 
      { 
       return; 
      } 

      } 

     } 

     else 
     { 
      // if this new Activity isn't logged, then create a new Activity Task 
      sforce.Task newTask = new sforce.Task(); 
      newTask.WhoId = id; 
      newTask.Subject = type; 
      newTask.Details__c = details; 
      if (description != "") newTask.Description = description; 
      newTask.Status = "Completed"; 
      newTask.Priority = "Normal"; 
      newTask.ActivityDate = DateTime.Now; 
      newTask.ActivityDateSpecified = true; 

      // insert it 
      try 
      { 
       SaveResult[] createResult = Binding.create(new sforce.sObject[] { newTask }); 

      } 
      catch (Exception e) 
      { 
       return; 
      } 
     } 


    } 

回答

1

您需要更新查詢以詢問瀏覽歷史記錄對象並更新代碼以創建瀏覽歷史記錄對象而不是任務。

如果您還沒有閱讀Web Services API文檔,它在java/c#中有queryingcreating的示例。

相關問題