[SalesForce] DML not allowed on User

I created some test users using the following apex code which worked perfectly fine:

list<User> abc = new list<user>();
for(integer i=1; i<3;i++) {
    abc.add(
        new User(
            lastname = 'E' + i,
            Alias = 'E' + i,
            Email = 'validemail@gmail.com',
            Username = 'E' + i + '@EM' + i + '.com',
            CommunityNickname = 'E' + i,
            ProfileId = '00ej000000120yU',
            TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'ISO-8859-1',
            LanguageLocaleKey = 'en_US'
        )
    );
}
insert abc;

After Inserting 3 records, I am trying to upsert using the following code: ( I am getting the same error even if using this code for the first time i.e., even before inserting records)

list<User> abc = new list<user>();
for(integer i=1; i<11;i++) {
    abc.add(
        new User(
            lastname = 'E' + i,
            Alias = 'E' + i,
            Email = 'validemail@gmail.com',
            Username = 'E' + i + '@EM' + i + '.com',
            CommunityNickname = 'E' + i,
            ProfileId = '00ej000000120yU',
            TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'ISO-8859-1',
            LanguageLocaleKey = 'en_US',
            Division = 'abdc'
        )
    );
}
upsert abc;
  • "insert abc" is working perfectly if the record doesnot exist
  • "update abc" on already inserted records throws the following error — "MISSING_ARGUMENT, Id not specified in an update call: []"
  • "upsert abc" – no matter if the record already exists or updating or creating new, always errors out — "DML not allowed on user"

Also, I am adding users to ChatterFree which has 400 count. So license, I believe is not a problem

Can someone please help me in understanding this issue. Thanks

Edit: Update issue resolved. I am getting that error because after inserting, I am trying to reuse the same code just by changing few fields(not all) and adding update to the bottom. But the following code works perfectly.

list<User> abc = [SELECT Username FROM User WHERE LastName = 'h1'];
for(User a : abc) {
    a.ProfileId = '00ej00000012hVk';
}
update abc;

Best Answer

You have added the elements in the list and updating it without being inserted. I believe this is what you are trying to do:

list<User> abc = new list<user>();
for(integer i=1; i<3;i++) {
    abc.add(
        new User(
            lastname = 'E' + i,
            Alias = 'E' + i,
            Email = 'validemail@gmail.com',
            Username = 'E' + i + '@EM' + i + '.com',
            CommunityNickname = 'E' + i,
            ProfileId = '00ej000000120yU',
            TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'ISO-8859-1',
            LanguageLocaleKey = 'en_US'
        )
    );
}
insert abc;

for(User userSo: abc){
abc.title='Hello';
}
update abc;
Related Topic