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
Aman BishtAman Bisht 

Apex programming error while iterating a collection

Hi
I am newbie in salesforce.I am getting an error in this code..
Please help me.

Thanks.....
public class UpdateRecord {
    public static void getUpdate()
    {
        List<FixedDeposit__c> li =new List<FixedDeposit__c>([SELECT FD_Amount__c  from FixedDeposit__c ]);
            for(FixedDeposit__c a : li)
            {
                 if(a.FD_Amount__c > 20000)
                 {
                     a.Quantity__c=200;
                      System.debug('done');  
                 }
                li.add(a);
            }	
      update li;  	
    }

}

please help me out
Best Answer chosen by Aman Bisht
Amit Chaudhary 8Amit Chaudhary 8

Please try below code.
public class UpdateRecord 
{


    public static void getUpdate()
    {

		List<FixedDeposit__c> li = [ SELECT id,FD_Amount__c,Quantity__c  from FixedDeposit__c limit 1000 ] ;
		List<FixedDeposit__c> lstToUpdate = new List<FixedDeposit__c>();
			for(FixedDeposit__c a : li)
            {
                if(a.FD_Amount__c > 20000)
                {
                    a.Quantity__c=200;
                    System.debug('done');  
					lstToUpdate.add(a);
                }
            }
		
		if(lstToUpdate.size()>0)
		{		
			update lstToUpdate;  	
		}	
    }

}
Let us know if this will help you

Thanks
Amit Chaudhary

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your query like below
List<FixedDeposit__c> li = [ SELECT id,FD_Amount__c,Quantity__c  from FixedDeposit__c limit 1000 ] ;
Let us know if this will help you

Thanks
Amit Chaudhary
 
Aman BishtAman Bisht
Still getting same error
Pankaj_GanwaniPankaj_Ganwani
public class UpdateRecord {
    public static void getUpdate()
    {
       List<FixedDeposit__c> lstToUpdate = new List<FixedDeposit__c>();
        List<FixedDeposit__c> li =new List<FixedDeposit__c>([SELECT Id, FD_Amount__c  from FixedDeposit__c ]);
            for(FixedDeposit__c a : li)
            {
                 if(a.FD_Amount__c > 20000)
                 {
                     a.Quantity__c=200;
                      System.debug('done');  
                 }
                lstToUpdate.add(a);
            }	
      update lstToUpdate ;  	
    }

}
Please use above code.
 
Amit Chaudhary 8Amit Chaudhary 8

Please try below code.
public class UpdateRecord 
{


    public static void getUpdate()
    {

		List<FixedDeposit__c> li = [ SELECT id,FD_Amount__c,Quantity__c  from FixedDeposit__c limit 1000 ] ;
		List<FixedDeposit__c> lstToUpdate = new List<FixedDeposit__c>();
			for(FixedDeposit__c a : li)
            {
                if(a.FD_Amount__c > 20000)
                {
                    a.Quantity__c=200;
                    System.debug('done');  
					lstToUpdate.add(a);
                }
            }
		
		if(lstToUpdate.size()>0)
		{		
			update lstToUpdate;  	
		}	
    }

}
Let us know if this will help you

Thanks
Amit Chaudhary

 
This was selected as the best answer
Aman BishtAman Bisht
Its is running but not updating the quantity in frontend
Aman BishtAman Bisht
Thanks guyz....I made a silly mistake....
This code works fine now..