I'm trying trying to create a user, which work like a charm. But I cannot assign he/she a userlicense. I get the error message. Field ObjectId has not been loaded or set. What Field ObjectId has not been set?
User user = new User(_sessionHelper.getSession());
user.setName(importResourcesData.getSsoID());
user.setPersonalName(importResourcesData.getFirstName() + " " + importResourcesData.getLastName());
User.setEmailAddress(importResourcesData.getEmailAdress());
user.setPassword(importResourcesData.getSsoID(), importResourcesData.getSsoID());
user.setAllResourceAccessFlag(false);
user.setGlobalProfileObjectId(getDefaultGlobalSecurityProfile());
ObjectId userId = user.create(); //WORKS!!!
BOIterator<UserLicense> loadResources = _sessionHelper.getElm().loadUserLicenses(new String[]{"LicenseType"}, "LicenseType = 'Timesheets'", null);
Userlicense ul = loadResources.next();
//This Fails!
user.createUserLicense(ul);
Generally speaking, the integration API is structured such that once you call create or update on an object, you should not continue to use the object. Instead, you should read a new object from the API and use that new object going forward. In your case you would add something like this after the call to user.create()
final User user2 = User.load(_sessionHelper.getSession(), null, userId);
Then you would use user2
in place of user
in the call to createUserLicense
user2.createUserLicense(ul);