我想使用 SOAP API 以编程方式禁用用户。我怎样才能做到这一点?我正在使用合作伙伴 API,并且拥有开发人员版本。我已设置管理用户权限。我已经经历过this http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_user.htm关联。我正在寻找可以帮助我禁用/停用用户的代码。
这是我的代码:
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] /cdn-cgi/l/email-protection");
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] /cdn-cgi/l/email-protection")){
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] /cdn-cgi/l/email-protection IsActive: true
Username: [email protected] /cdn-cgi/l/email-protection IsActive: false
Username: [email protected] /cdn-cgi/l/email-protection
Username: [email protected] /cdn-cgi/l/email-protection IsActive: false
但是,在用户界面中,当我转到“我的姓名”>“设置”>“管理用户”>“用户”时,它始终显示用户的“活动”复选框[电子邮件受保护] /cdn-cgi/l/email-protection已选择:-(
看起来您实际上并没有将更新发送回 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("[email protected] /cdn-cgi/l/email-protection") ){
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)