Amreen
Amreen

Reputation: 69

how to iterate through a list in salesforce

I am new to salesforce, I want to iterate the department images, can anyone help me?

Here is the code:

 public class Hospital_Controller {
 public List<attachment> HodImages {get; set;}
 string recid;
 string recId1 ;
 public Hospital_Controller(ApexPages.StandardController controller) {
      recId = controller.getId();   
      HodImages  = new List<attachment>();
      List<Department__c> depIds =[select id from Department__c];
      HodImages = [select id,name from attachment where parentId =:depIds];

  }
}

Upvotes: 2

Views: 30829

Answers (1)

JF-Mechs
JF-Mechs

Reputation: 1081

You can do two ways to iterate a List in apex.

Here is an advance way to loop on a list.

List<string> stringList = new List<String>{'sample1', 'sample2'};
for (String str : stringList)
    system.debug(str);

Or you could do it on usual for loop.

List<string> stringList = new List<String>{'sample1', 'sample2'};
for(Integer i = 0; i < stringList.size(); i++)
    system.debug(stringList[i]);

For more example of Force.com List class you can check their docs here

Upvotes: 5

Related Questions