2012-09-20 57 views
2

我想通過使用SOAP API來編程禁用用戶。我怎樣才能做到這一點?我使用的是合作伙伴API,我有開發者版。我管理用戶保留設置。我已經通過this鏈接。我正在尋找可以幫助我禁用/停用用戶的代碼。如何通過SOAP API禁用/停用SalesForce用戶?

這是我的代碼:

import com.sforce.soap.partner.Connector; 
import com.sforce.soap.partner.PartnerConnection; 
import com.sforce.soap.partner.QueryResult; 
import com.sforce.soap.partner.sobject.SObject; 
import com.sforce.ws.ConnectionException; 
import com.sforce.ws.ConnectorConfig; 

public class DeactivateUser { 
    public static void main(String[] args) { 
     ConnectorConfig config = new ConnectorConfig(); 

     config.setUsername("[email protected]"); 
     config.setPassword("sjjhggrhgfhgffjdgj"); 

     PartnerConnection connection = null; 

     try { 
      connection = Connector.newConnection(config); 
      QueryResult queryResults = connection.query("SELECT Username, IsActive from User"); 

     if (queryResults.getSize() > 0) { 
      for (SObject s : queryResults.getRecords()) { 
       if(s.getField("Username").equals("[email protected]")){ 
        System.out.println("Username: " + s.getField("Username")); 
        s.setField("IsActive", false); 
       } 
       System.out.println("Username: " + s.getField("Username") + " IsActive: " + s.getField("IsActive")); 
      } 
     } 
     } catch (ConnectionException ce) { 
      ce.printStackTrace(); 
     } 
    } 
} 

這是輸出:

Username: [email protected] IsActive: true 
Username: [email protected] IsActive: false 
Username: [email protected] 
Username: [email protected] IsActive: false 

然而,在UI,當我去我的名字>設置>管理用戶>用戶,它總是表現出 '主動'複選框爲用戶[email protected]選擇:-(

+0

我覺得你忘了在** set **'IsActive'之後調用'connection.update',閱讀這個> http://www.salesforce.com/us/developer/docs/api/index_Left。 htm#StartTopic = Content/sforce_api_calls_update.htm –

+0

感謝Martin Borthiry指出缺少的內容。我會仔細看看的。 – waprau

回答

7

它看起來並不像你實際上將更新發送回Salesforce--你只是將IsActive設置爲false,你將n EED用來PartnerConnection.update(SObject[] sObjects)一個電話,以便Salesforce來反映你的變化,像這樣:

try { 
    connection = Connector.newConnection(config); 
    QueryResult queryResults = connection.query("SELECT Id, Username, IsActive from User"); 

    if (queryResults.getSize() > 0) { 
     // keep track of which records you want to update with an ArrayList 
     ArrayList<SObject> updateObjects = new ArrayList<SObject>(); 
     for (SObject s : queryResults.getRecords()) { 
      if (s.getField("Username").equals("[email protected]")){ 
       System.out.println("Username: " + s.getField("Username")); 
       s.setField("Id", null); 
       s.setField("IsActive", false); 
      } 
      updateObjects.add(s); // if you want to update all records...if not, put this in a conditional statement 
      System.out.println("Username: " + s.getField("Username") + " IsActive: " + s.getField("IsActive")); 
     } 
     // make the update call to Salesforce and then process the SaveResults returned 
     SaveResult[] saveResults = connection.update(updateObjects.toArray(new SObject[updateObjects.size()])); 
     for (int i = 0; i < saveResults.length; i++) { 
      if (saveResults[i].isSuccess()) 
       System.out.println("record " + saveResults[i].getId() + " was updated successfully"); 
      else {       
       // There were errors during the update call, so loop through and print them out 
       System.out.println("record " + saveResults[i].getId() + " failed to save"); 
       for (int j = 0; j < saveResults[i].getErrors().length; j++) { 
        Error err = saveResults[i].getErrors()[j]; 
        System.out.println("error code: " + err.getStatusCode().toString()); 
        System.out.println("error message: " + err.getMessage()); 
       } 
      } 
     } 
    } 
} catch (ConnectionException ce) { 
     ce.printStackTrace(); 
} 
+0

感謝JCD與代碼的詳細答覆。我會盡力實施它。再次感謝。 – waprau

+0

你好JCD,我收到以下錯誤從別的塊: 記錄空未能挽回, 錯誤代碼:MISSING_ARGUMENT, 錯誤信息:身份證在更新呼叫 – waprau

+0

看到我更新的答案沒有指定,則需要修改查詢包括Id字段。這會讓Salesforce知道您在更改時要更新哪些記錄:'QueryResult queryResults = connection.query(「SELECT Id,Username,IsActive from User」);' – JCD

0

它可以直接與用戶記錄工作沒有SOQL查詢,如果你已經知道的ID。

SalesforceSession session = ...; 

sObject userSObject = new sObject(); 
userSObject.Id = "00570000001V9NA"; 
userSObject.type = "User"; 
userSObject.Any = new System.Xml.XmlElement[1]; 

XmlDocument xmlDocument = new XmlDocument(); 
XmlElement fieldXmlElement = xmlDocument.CreateElement("IsActive"); 
fieldXmlElement.InnerText = bool.FalseString; 
userSObject.Any[0] = fieldXmlElement; 

SaveResult[] result = session.Binding.update(new sObject[] { userSObject }); 
foreach(SaveResult sr in result) 
{ 
    System.Diagnostics.Debug.WriteLine(sr.success + " " + sr.id); 
    if(!sr.success) 
    { 
     foreach(Error error in sr.errors) 
     { 
      System.Diagnostics.Debug.WriteLine(error.statusCode + " " + error.message); 
     } 
    } 
} 
相關問題