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
Sumesh ChandranSumesh Chandran 

For loop not looping through every single record on the list

I have a for loop running through a list which add items from one list to another list. The source list has 8 records, and the new list will get only 7 records.
for(sumchans__Address_Master__c city: addressList) {
            sumchans__City_Master__c theCity = new sumchans__City_Master__c();
            theCity.Name = city.sumchans__City_Name__c;
            theCity.sumchans__City_Name_Ext_Id__c = city.sumchans__City_Name__c;related data.
            theCity.sumchans__Province_Code__c = city.sumchans__Province_Code__c;
            cityList.add(theCity);
        }

Please help

THanks
Suraj MakandarSuraj Makandar
Hi  Sumesh Chandran,

As per the code snippet you have provided, both lists should get same number of records. I dont see any issue in above code snippet.

Can you please share entire code.

Thanks,
Suraj
Sumesh ChandranSumesh Chandran
Thanks Suraj, I figured out that it was the not the issue of the for loop. I tried put in the same code inside an if clause at the bottom and I am still getting the same results. Here is the entirety of the code. I am sorry if it is too long.
global void execute(Database.BatchableContext BC, List<MDU_Squad_Data_min__c> citiesAddresses) {    
        List<sumchans__Address_Master__c> addressList = new List<sumchans__Address_Master__c>();//unique address list
        List<sumchans__ADDRESS_STATS__c> addressStats = new List<sumchans__ADDRESS_STATS__c>();//Saving city stats
        List<sumchans__City_Master__c> cityList = new List<sumchans__City_Master__c>();//unique city list
        List<sumchans__City_Stats__c> cityStats = new List<sumchans__City_Stats__c>();//Saving city stats
        Set<String> cities = new Set<String>();
        Set<String> addresses = new Set<String>(); 
        Set<String> totBldgs = new Set<String>();
        for(MDU_Squad_Data_min__c cityAddress: citiesAddresses)
        {   
            sumchans__Address_Master__c address = new sumchans__Address_Master__c();
            fullAddress = cityAddress.sumchans__STREET_ADDRESS__c+' '+cityAddress.sumchans__CITY_NAME__c+' '+cityAddress.sumchans__PROVINCE_CODE__c+' '+cityAddress.sumchans__POSTAL_CODE__c;
            address.Name = cityAddress.sumchans__STREET_ADDRESS__c;
            address.sumchans__City_Name__c = cityAddress.sumchans__CITY_NAME__c;
            address.sumchans__Province_Code__c = cityAddress.sumchans__PROVINCE_CODE__c;
            address.sumchans__Postal_Code__c = cityAddress.sumchans__POSTAL_CODE__c;
            address.sumchans__Full_Address_Ext_Id__c = fullAddress;			
            if(!addresses.contains(fullAddress.substringBeforeLast(' '))) {                
                onBillings = 0.0;
                offBillings = 0;
                internetOfferings = 0;
                videoOfferings = 0;
                phoneOfferings = 0;
                totalUnits = 0.0;
                penetration = 0.0;
                for(MDU_Squad_Data_min__c addressStat: citiesAddresses) {
                    if((addressStat.sumchans__STREET_ADDRESS__c+' '+addressStat.sumchans__CITY_NAME__c+' '+addressStat.sumchans__PROVINCE_CODE__c+' '+cityAddress.sumchans__POSTAL_CODE__c) == address.sumchans__Full_Address_Ext_Id__c) {                    
                        String status = (String)addressStat.get('sumchans__STATUS__c');
                        if(status.toLowerCase() == 'on-billing') {
                            onBillings++;
                            if (addressStat.get('sumchans__INTERNET_SERVICE__c') != null && addressStat.get('sumchans__INTERNET_SERVICE__c') != 'None') { internetOfferings++; } 
                            if (addressStat.get('sumchans__VIDEO_SERVICE__c') != null && addressStat.get('sumchans__VIDEO_SERVICE__c') != 'None') { videoOfferings++; }
                            if (addressStat.get('sumchans__PHONE_SERVICE__c') != null && addressStat.get('sumchans__PHONE_SERVICE__c') != 'None') { phoneOfferings++; }
                        }                      
                        if(status.toLowerCase() == 'off-billing') { offBillings++; }  
                    }
                } 
                totalUnits = onBillings + offBillings;
                penetration = ((onBillings / totalUnits) * 100);
                if((totalUnits>50) && (penetration<50)) {
                    sumchans__ADDRESS_STATS__c stat = new sumchans__ADDRESS_STATS__c();
                    stat.Name = fullAddress;// Full Address column in the Address Master table
                    stat.sumchans__Address_Master_Ext_Id__r = new sumchans__Address_Master__c(sumchans__Full_Address_Ext_Id__c = fullAddress);
                    stat.sumchans__On_Billings__c = onBillings;
                    stat.sumchans__Off_Billings__c = offBillings;
                    stat.sumchans__Internet_Offerings__c = internetOfferings;
                    stat.sumchans__Video_Offerings__c = videoOfferings;
                    stat.sumchans__Phone_Offerings__c = phoneOfferings;
                    
                    addresses.add(fullAddress.substringBeforeLast(' '));
                    addressList.add(address);
                    addressStats.add(stat);
                    
                    sumchans__City_Master__c theCity = new sumchans__City_Master__c();
                    theCity.Name = cityAddress.sumchans__CITY_NAME__c;// Normal city name column
                    theCity.sumchans__City_Name_Ext_Id__c = cityAddress.sumchans__CITY_NAME__c;//Populating the external id field with city name to pull related data.
                    theCity.sumchans__Province_Code__c = cityAddress.sumchans__PROVINCE_CODE__c;
                    if(!cities.contains(theCity.Name)) {
                        cities.add(theCity.Name);
                        cityList.add(theCity);
                    }
                }              
            }	      
        }

        Database.SaveResult[] saveCityMaster = Database.Insert(cityList, false);
        Database.SaveResult[] saveAddressMaster = Database.Insert(addressList, false);  
        Database.SaveResult[] saveAddressStats = Database.Insert(addressStats, false);
    }