如何使用SOAP Partner API创建SalesForce“用户”?

时间:2012-09-19 10:49:33

标签: soap salesforce

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

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程序时,它会给出以下输出,显示'Created user:null': - (

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

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

enter image description here

你告诉我什么错了吗?

谢谢,

Wap Rau

1 个答案:

答案 0 :(得分:4)

创建调用返回错误,但是你没有检查它,返回的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());