3

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 = '[email protected]',
            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 = '[email protected]',
            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;
5
  • can you please share the update code which is throwing the error? Commented Apr 15, 2015 at 20:12
  • in place of "insert abc" from the code I posted, I am using "update abc"
    – sf_user
    Commented Apr 15, 2015 at 20:13
  • well, if you are using update instead of insert, it will obviously fail coz the records are not yet inserted. update will work only after insert or on a result which you have fetched using soql Commented Apr 15, 2015 at 20:20
  • I am using update after inserting the records, say if I try to update another field like company = 'abc' and then update, even then getting the same error
    – sf_user
    Commented Apr 15, 2015 at 20:21
  • please share that piece of code. Commented Apr 15, 2015 at 20:23

2 Answers 2

1

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 = '[email protected]',
            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;
3
  • update issue resolved Rajiv. Can you throw some inputs on upsert which gives me error "DML not allowed on user"
    – sf_user
    Commented Apr 15, 2015 at 20:49
  • please share the exact piece of code as it is which throws this error Commented Apr 15, 2015 at 20:52
  • updated with the actual "upsert" code I am using. Thanks
    – sf_user
    Commented Apr 15, 2015 at 20:59
3

Use the Database.upsert static method instead of the upsert DML keyword:

//failure (DML not allowed on User)
upsert user ExternalId__c;

//success
Database.upsert(new User(), User.ExternalId__c, true);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .