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
Michael_ETMichael_ET 

simple for loop

Hello,

 

i'm looking for an example of a simple for loop that will change the value of a field, taken from a query, from true to false.

 

thanks,

Best Answer chosen by Admin (Salesforce Developers) 
Ronen.ax767Ronen.ax767

Hi,

 

This code should make the work:

 

public List<Obj> ObjList {get;set;}

ObjList= [select ID,Active  from Obj where Active='true'];

for (Obj o : ObjList )
{
   o.Active  = 'false';
} 
 update ObjList;

 hope this help!

 

All Answers

Ronen.ax767Ronen.ax767

Hi,

 

This code should make the work:

 

public List<Obj> ObjList {get;set;}

ObjList= [select ID,Active  from Obj where Active='true'];

for (Obj o : ObjList )
{
   o.Active  = 'false';
} 
 update ObjList;

 hope this help!

 

This was selected as the best answer
_Prasu__Prasu_

update statement should be out of loop else you will hit governor limits.

public List<Obj> ObjList {get;set;}

ObjList= [select ID,Active  from Obj where Active='true'];

for (Obj o : ObjList )
   o.Active  = 'false';
 
  update ObjList;
Michael_ETMichael_ET

This will get me started. Thank you!