3

我想使用 SOAP API Partner WSDL 以编程方式在 SalesForce 中创建一个用户。这是我的代码:

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

import com.sforce.soap.partner.sobject.*;
import com.sforce.soap.partner.*;
import com.sforce.soap.*;
import com.sforce.*;

public class PartnerAPICreateUser {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ConnectorConfig config = new ConnectorConfig();
        config.setUsername("waprau@waprau.com");
        config.setPassword("dhskjhkjgfkjsdhkfjg");

        PartnerConnection connection = null;

        try {
            SObject user = new SObject();

            user.setType("user");

            user.setField("Alias", "abcd");
            user.setField("DefaultGroupNotificationFrequency", "P");
            user.setField("DigestFrequency", "D");
            user.setField("Email", "abcd@pqrs.com");
            user.setField("EmailEncodingKey", "ISO-8859-1");
            user.setField("LanguageLocaleKey", "English");
            user.setField("LastName", "Rau");
            user.setField("LocaleSidKey", "En");
            user.setField("TimeZoneSidKey", "America/Los_Angeles");
            user.setField("Username", "abcd@pqrs.com");
            user.setField("UserPermissionsCallCenterAutoLogin", "true");
            user.setField("UserPermissionsMarketingUser", "true");
            user.setField("UserPermissionsOfflineUser", "true");


            connection = Connector.newConnection(config);
            SaveResult[] results = connection.create(new SObject[] { user });
            System.out.println("Created user: " + results[0].getId());

            QueryResult queryResults = connection
                    .query("SELECT Id, Name from User "
                            + "ORDER BY CreatedDate DESC LIMIT 5");
            if (queryResults.getSize() > 0) {
                for (SObject s : queryResults.getRecords()) {
                    System.out.println("Id: " + s.getField("Id") + " - Name: "
                            + s.getField("Name"));
                }
            }

        } catch (ConnectionException ce) {
            ce.printStackTrace();
        }
    }
}

但是,当我执行这个 Java 程序时,它会给出以下输出,显示“创建的用户:null”:-(

Created user: null
Id: 005E0000001fb3vIAA - Name: Rau
Id: 005E0000001fVTTIA2 - Name: Chatter Expert
Id: 005E0000001fVU1IAM - Name: Wap Rau

转到 MyName > 设置 > 管理用户(在管理设置中)> 配置文件时的管理权限

在此处输入图像描述

你能告诉我有什么问题吗?

谢谢,

瓦普劳

4

1 回答 1

4

create 调用返回一个错误,但你不检查它,返回的 SaveResult 会告诉你为什么它没有创建用户,你想要类似的东西

SaveResult[] results = connection.create(new SObject[] { user });
if (results[0].isSuccess())
    System.out.println("Created user: " + results[0].getId());
else
    System.out.println("Error: " + results[0].getErrors()[0].getStatusCode() + 
                       ":" + results[0].getErrors()[0].getMessage());
于 2012-09-19T15:09:46.503 回答