function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfpsfp 

Process Selected Users Records catch the exception still process another records.

Hi All,

 In my Visual force page, I have selected 10 records, I have process in apex class, in between 5th records is having some exception. Its not procesing below records. Please help out this issue. 

try {
            for(User user: selectedUsers) {                        
                User__c pbxUser = new User__c();
                
                if (user.FirstName.length() > 32) {
                    pbxUser.FirstName__c = user.FirstName.substring(0, 32); //(String) user.get('FirstName');
                } else {
                    pbxUser.FirstName__c = user.FirstName;
                }
                
                if (user.LastName.length() > 32) {
                    pbxUser.LastName__c = user.LastName.substring(0, 32); //(String) user.get('LastName');
                } else {
                    pbxUser.LastName__c = user.LastName;    
                }
                if (user.Username.length() > 64) {
                    pbxUser.Username__c = user.Username.right(64);
                } else {                
                    pbxUser.Username__c = user.Username; //(String) user.get('Username');
                }
                
                pbxUser.User__c = user.Id; //(String) user.get('Id');
                String mobilePhone = user.MobilePhone; //(String) user.get('MobilePhone');
                if(mobilePhone == null) {
                    pbxUser.MobilePhone__c = '0000'; 
                } else {
                    if (mobilePhone.length() > 24) {                    
                        pbxUser.MobilePhone__c = mobilePhone.substring(0, 24);
                    } else {
                        pbxUser.MobilePhone__c = mobilePhone;
                    }    
                }
                pbxUser.Status__c = 'ACTIVE';
                //system.debug(pbxUser);
                pbxUsers.add(pbxUser);
            }        
            addUsers(pbxUsers);
            sfUserList=null; // we need this line if we performed a write operation  because getUsers gets a fresh list now
            
        } catch (Exception e) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage());           
            ApexPages.addMessage(msg);
                        
        }     
 
Shashikant SharmaShashikant Sharma
You could put a try catch block insde for loop
 
for(User user: selectedUsers) { 

   try {
   }
   catch(exception e ) {
   // do actions 
   }
}

this way your exception in any record will not block processing of items after it.

Thanks
Shashikant