gpt4 book ai didi

api - 如何通过 SOAP API 禁用/停用 SalesForce 用户?

转载 作者:行者123 更新时间:2023-12-01 09:03:29 24 4
gpt4 key购买 nike

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

这是我的代码:

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 显示“事件”复选框:-(

最佳答案

看起来您实际上并未将更新发送回 Salesforce - 您只是在设置 IsActive在本地为假。您需要调用 PartnerConnection.update(SObject[] sObjects)为了让 Salesforce 反射(reflect)您的更改,如下所示:

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();
}

关于api - 如何通过 SOAP API 禁用/停用 SalesForce 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com