3
votes

I want to create a User in SalesForce programmatically by using SOAP API Partner WSDL. This is my code:

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("[email protected]");
        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", "[email protected]");
            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", "[email protected]");
            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();
        }
    }
}

However, when I execute this Java program it gives following output which shows 'Created user: null' :-(

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

Administrative Permissions when I go to MyName > Setup > Manage Users (in Administration Setup) > Profiles

enter image description here

Can you tell me whats wrong?

Thanks,

Wap Rau

1

1 Answers

4
votes

The create call is returning an error, but you don't check for it, the returned SaveResult will tell you why it didn't create the user, you want something like

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