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
Nagendra Singh 12Nagendra Singh 12 

For loop to iterate all record returned in the Query

Hi there, need help, I wrote a query that returns 3 records. and I'm reading those records in the FOR loop to update record that matchs criteria, however this record iteration no happening, anything I'm missing in following code:

my question is, I put a debug before if, I epxcet 3 entries in the log but it just one.

List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Email__c,Id
                                             From Sales_Team__c
                                             Where Sales_Team_Member__c IN: opportunityIdSet and  Producer__c = null];

for (Sales_Team__c OpptyGST : gst){
                     system.debug('Test GST Memebr# : ' + OpptyGST.Sales_Team_Member__c);                            
                if (OpptyGST.Sales_Team_Member__c == Oppty.Sales_Rep__c){
                    OpptyGST.Primary__c = true;
                    OpptyGST.Email__c = 'primary@gst.com';
                   
                } 
}
 
surasura
As far as I can see there is nothing wrong in you code. if you debug only prints only once that means your query returns one record , update you code as below to verify number of records return by your query 
 
List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Email__c,Id
                                             From Sales_Team__c
                                             Where Sales_Team_Member__c IN: opportunityIdSet and  Producer__c = null];


system.debug('Number of records returned -----------------'+gst.size() );

for (Sales_Team__c OpptyGST : gst){
                     system.debug('Test GST Memebr# : ' + OpptyGST.Sales_Team_Member__c);                            
                if (OpptyGST.Sales_Team_Member__c == Oppty.Sales_Rep__c){
                    OpptyGST.Primary__c = true;
                    OpptyGST.Email__c = 'primary@gst.com';
                   
                } 
}

in your debug log check the line 
Number of records returned ----------------- it will show the actual no of records returned
 
ManojjenaManojjena
Hi Narendra,

I don't think you have any error in your code .Please chec once more .Check in debug log .

Your debug statement should appear .
AshishyadavAshishyadav
If I were you , I will check the query in Developer console to see how many records are being retured 
DEveloper console >> Query section
Amit Chaudhary 8Amit Chaudhary 8
Hi Naerndra,

Please share your full code.