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
SFDummySFDummy 

error on Database.SaveResult - Error: Compile Error: Variable does not exist: srList at line 25

Database.SaveResult[] srList = Database.insert(alltktReqLst, false);
       
       // Iterate through each returned result
       for (Database.SaveResult sr : srList) {}
I am getting error  when I try to iterate through result set. I tested that the insert statement is working and records are inserted. HELP please, why is variable does not exists for srList?
 
SandhyaSandhya (Salesforce Developers) 
Hi ,

please check what  alltktReqLst is returning in your code by system.debug(alltktReqLst);
 
Please refer below code if you are doing the same thing.
// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
Database.SaveResult[] srList = Database.insert(accts, false);

// Iterate through each returned result
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 account. Account 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('Account fields that affected this error: ' + err.getFields());
        }
    }
}
For more information please refer below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm


Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
 
 
Nagendra ChinchinadaNagendra Chinchinada
Hi,

It seems like the line Database.SaveResult[] srList = Database.insert(alltktReqLst, false); is in some if condition which is not beeing executed all times, eventually variable srList not initiating and it is throwing variable doesn't exist error.
Declare List  at the starting of class/trigger .
List<Database.SaveResult> updateResults;// Decalre at the starting of class

//
//Code block

updateResults = Database.insert(alltktReqLst, false);// Assign results to list here
   
   // Iterate through each returned result
   for (Database.SaveResult sr : updateResults) {
   }
Let me know if it works.

Thanks,
Nagendra