0 votes
1 view
in Salesforce by (11.9k points)

I want to create 5-6 SalesForce users programmatically in one go by using SOAP API. I am using Partner API and I have a Developer edition sandbox. I am retrieving users information from a database table and then by using API I want to create these 5-6 users at once. I can create one user however after creating another user it is throwing error LICENSE_LIMIT_EXCEEDED:License Limit Exceeded.

This is snapshot of my code which retrieves users information from database and then creates a user programmatically:

SObject user = new SObject();

user.setType("User");

while (rs.next()) {

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

    user.setField("ProfileId", connection.getUserInfo().getProfileId());

    SaveResult[] results = connection.create(new SObject[] { user });

    System.out.println("Created user: " + results[0].getId());

    if (results[0].isSuccess())

        out.println("Created user: " + results[0].getId());

    else

        out.println("Error: " + results[0].getErrors()[0].getStatusCode() + 

                           ":" + results[0].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"));

    }

}

Here I can create one user however after creating one user it is throwing error LICENSE_LIMIT_EXCEEDED:License Limit Exceeded.

These are the number of licenses available:enter image description here

I can see that licenses remaining for SalesForce is 0. However, I just want to create some users that don't necessarily have to be SalesForce users. It can be any type of users which just should be able to login to my org. When I am trying to create some different type of users such as 'Chatter Free' which has 5000 licences by using user.setField("UserType", "CsnOnly") it is giving error INVALID_FIELD_FOR_INSERT_UPDATE:Unable to create/update fields: UserType

These are users in my SalesForce: enter image description here

How to solve this problem? I just want to create some users from database which can then login to SalesForce. Its not necessary that they should be administrators. They can have minimum privileges but they should be able to login to SalesForce.

1 Answer

0 votes
by (31.6k points)

In order to obtain the Chatter Free User's profile ID, without much coding, you'll have to query it. The modified code in order to include this query, along with reworking it so that only a single callout to PartnerConnection.create is made is given below:

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

    }

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...