QueryResult profileQuery = connection.query("select Id from Profile where Name = 'Chatter Free User' limit 1");
SObject chatterFreeProfile;
if ( profileQuery.getSize() > 0 ) {
chatterFreeProfile = profileQuery.getRecords()[0];
}
ArrayList<SObject> users = new ArrayList<SObject>();
while (rs.next()) {
SObject user = new SObject();
user.setType("User");
user.setField("Alias", rs.getString("Alias"));
user.setField("Email", rs.getString("Email"));
user.setField("EmailEncodingKey", rs.getString("EmailEncodingKey"));
user.setField("LanguageLocaleKey", "En_US");
user.setField("LastName", rs.getString("LastName"));
user.setField("LocaleSidKey", rs.getString("LocaleSidKey"));
user.setField("TimeZoneSidKey", "America/Los_Angeles");
user.setField("Username", rs.getString("Username"));
user.setField("UserPermissionsCallCenterAutoLogin", "false");
user.setField("UserPermissionsMarketingUser", "false");
user.setField("UserPermissionsOfflineUser", "false");
if ( chatterFreeProfile != null )
user.setField("ProfileId", chatterFreeProfile.getField("Id"));
else
user.setField("ProfileId", connection.getUserInfo().getProfileId());
users.add(user);
}
if ( users.size() > 0 ) {
SaveResult[] results = connection.create(users.toArray(new SObject[users.size()]));
for ( int i = 0; i < saveResults.length; i++ ) {
if (results[i].isSuccess())
out.println("Created user: " + results[i].getId());
else
out.println("Error: " + results[i].getErrors()[0].getStatusCode() + ":" + results[i].getErrors()[0].getMessage());
}
}
QueryResult queryResults = connection.query("SELECT Id, Name from User ORDER BY CreatedDate DESC LIMIT 5");
if ( queryResults.getSize() > 0 ) {
for ( SObject s : queryResults.getRecords() ) {
out.println("Id: " + s.getField("Id") + " - Name: " + s.getField("Name"));
}
}