3

我想通过使用 SOAP API 以编程方式禁用用户。我怎样才能做到这一点?我正在使用合作伙伴 API,并且我有开发人员版。我已经设置了管理用户权限。我已经通过这个链接。我正在寻找可以帮助我禁用/停用用户的代码。

这是我的代码:

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("waprau@waprau.com");
        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("abcd@pqrs.com")){
                    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: waprau@waprau.com IsActive: true
Username: jsmith@ymail.net IsActive: false
Username: abcd@pqrs.com
Username: abcd@pqrs.com IsActive: false

但是,在 UI 中,当我转到“我的姓名”>“设置”>“管理用户”>“用户”时,它始终显示用户 abcd@pqrs.com 的“活动”复选框已选中:-(

4

2 回答 2

8

看起来您实际上并未将更新发送回 Salesforce - 您只是IsActive在本地设置为 false。您需要使用调用来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("abcd@pqrs.com") ){
                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();
}
于 2012-09-20T13:13:53.127 回答
1

如果您已经知道 Id,则可以直接使用用户记录而无需 SOQL 查询。

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);
        }
    }
}
于 2016-03-10T19:26:04.463 回答