I have a try catch block to handle any problems when inserting a list of users, however, if an exception is encountered, how can I output which user in the list this exception is occurring for? I'm trying to identify this in the debug log. I can only seem to output the exception message itself but I have no way of identify what user in the list the exception occurred for.


if(portalUsers.size() > 0){
    insert portalUsers; //Insert list of users
   } catch (System.DmlException e){
    for(Integer k = 0; k < e.getNumDml(); k++){
        System.debug('Portal User DmlException Encountered on Insert: ' + e.getDmlMessage(k));

Can anyone help me out please?


Please check the below links for SaveResult class.



Here is the code that I executed in the Developer Console.

This code is trying to insert two users without any mandatory information being populated.

List<User> userList = new List<User>();
User usr1 = new User();
User usr2 = new User();

Database.SaveResult[] srList = Database.insert(userList,false);
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted record. Record ID: ' + sr.getId());
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('User fields that affected this error: ' + err.getFields());

And the debug log shows :

    04:11:35.077 (77119261)|USER_DEBUG|[16]|DEBUG|The following error has occurred.
04:11:35.077 (77130186)|SYSTEM_METHOD_EXIT|[16]|System.debug(ANY)
04:11:35.077 (77140071)|STATEMENT_EXECUTE|[17]
04:11:35.108 (108619368)|HEAP_ALLOCATE|[17]|Bytes:12
04:11:35.108 (108715273)|SYSTEM_METHOD_ENTRY|[17]|String.valueOf(Object)
04:11:35.108 (108772014)|HEAP_ALLOCATE|[17]|Bytes:22
04:11:35.108 (108800383)|SYSTEM_METHOD_EXIT|[17]|String.valueOf(Object)
04:11:35.108 (108812589)|HEAP_ALLOCATE|[17]|Bytes:2
04:11:35.108 (108848157)|HEAP_ALLOCATE|[17]|Bytes:160
04:11:35.108 (108870279)|HEAP_ALLOCATE|[17]|Bytes:184
04:11:35.108 (108891551)|SYSTEM_METHOD_ENTRY|[17]|System.debug(ANY)
04:11:35.108 (108901794)|USER_DEBUG|[17]|DEBUG|REQUIRED_FIELD_MISSING: Required fields are missing: [Username, LastName, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey]
04:11:35.108 (108910963)|SYSTEM_METHOD_EXIT|[17]|System.debug(ANY)
04:11:35.108 (108922676)|STATEMENT_EXECUTE|[18]
04:11:35.108 (108932423)|HEAP_ALLOCATE|[18]|Bytes:41
04:11:35.108 (108963459)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:44
04:11:35.108 (108998846)|HEAP_ALLOCATE|[18]|Bytes:44
04:11:35.109 (109033987)|SYSTEM_METHOD_ENTRY|[18]|String.valueOf(Object)
04:11:35.109 (109072186)|HEAP_ALLOCATE|[18]|Bytes:131
04:11:35.109 (109098377)|SYSTEM_METHOD_EXIT|[18]|String.valueOf(Object)
04:11:35.109 (109117485)|HEAP_ALLOCATE|[18]|Bytes:172
04:11:35.109 (109136721)|SYSTEM_METHOD_ENTRY|[18]|System.debug(ANY)
04:11:35.109 (109145242)|USER_DEBUG|[18]|DEBUG|User fields that affected this error: (Username, LastName, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey)
|improve this answer|||||
  • 1
    Chetan, link-only answers are discouraged on this site. Can you start your answer with a direct and specific answer to the question (perhaps including code) and leave the links for extra information? – Keith C Jun 24 '14 at 12:26
  • Hi Chetan. Can you add more detail to your answer please? Just linking to two references doesn't work well for our site, specifically because if those two links no longer work (say 6 months from now), this answer will have very little value. Typically, if you want to link to another resource, we encourage it but provide a useful snippet that relates directly to the question for future use. – Jesse Altman Jun 24 '14 at 12:27
  • Thanks Chetan but I didn't find out anything I didn't already know in those links. My ultimate goal here is to be able to trap the users that don't get inserted and find out why they didn't get inserted. I was hoping to identify these and email the list of them to myself. – Brian Casey Jun 24 '14 at 16:16
  • That still doesn't tell me what user the insert failed on. I'm trying to capture at minimum the email address that it fails on. I'm passing a list of users with all the necessary fields to the insert call, and if this bulk insert fails I want to catch what user in the list it is failing on. – Brian Casey Jun 26 '14 at 15:23

Try this ... (assumes you want to allow for partial successes)

List<Database.SaveResult> srList = Database.insert(portalUsers,false);
for (Integer i = 0; i < portalUsers.size(); i++) // srList and portalUsers are indexed/ordered equivalently
    if (!srList[i].isSuccess())
       System.debug('Insert failure on row ['+i+'] for user: ' + portalUsers[i].email + 
           'errmsg =' + srList[i].getErrors[0].getMessage());

Note that the above displays only the first error encountered for any portalUser[i] - if you want to show all, see Chetan's answer.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.