List Examples Part2

  1. List of Salaries
List<Decimal> alaries = new List<Decimal>();
Decimal intialSalary = 1000;
for(integer i=0; i<30; i++){ 
    Salaries.add(initialSalary); 
    initialSalary = initialSalary + 100;
}

Execute in Anonymous window in Developer console to verify the output

System.debug(Salaries);

2. Create  a list of  marks and print their total

List<Integer> marks = new List<Integer>();
marks.add(10);
marks.add(20);
marks.add(30);

Decimal total=0;

for(Integer i=0; i < marks.size(); i++){
    total = total + marks[i];
}
System.debug('Total Marks: ' + total);

3. Create a list of account and add three records into it.

List<Account> accs = new List<Account>();    
Account a1 = new Account();
a1.Name = 'aaa';
a1.phone = '123';
accs.add(a1);

Account a2 = new Account();
a2.Name = 'bbb';
a2.Industry = 'banking';
accs.add(a2);

Account a3 = new Account();
a3.Name = 'ccc';
a3.Phone = '123';
accs.add(a3);

4. Create a List to Store Opportunity records and insert  elements into it with fields Name, Stage, Closed Date and Amount for two different deals on your own.

List<Opportuntiy> optyList  = new List<Opportunity>();
	
Opportunity op1 = new Opportunity();
op1.Name = 'Salesforce';
op1.Amount = 10000;
op1.StageName = 'Closed Won';
optyList.add(op1);

Opportunity op2 = new Opportunity();
op2.Name = 'Admin';
op2.Amount = 9000;
op2.StageName = 'Closed Lost';
op2.closeDate = System.today() + 5;  //using date methods belong to System class
optyList.add(op2);

5. Create list of contacts and insert three contacts

List<Contact> cons = new List<Contact>();

Contact c1 = new Contact();
c1.LastName = 'Kri';
c1.FirstName = 'Veda';
cons.add(c1);

Contact c2 = new Contact();
c2.LastName = 'Naredla';
c2.FirstName = 'Natesh';
cons.add(c2);

6. List of Cases

List<Case> cases = new List<Case>();

Case c1 = new Case();
c1.Status = 'New';
c1.priority = 'Normal';
c1.Subject = 'Test';
c1.origin = 'Web';
cases.add(c1);

Case c2 = new Case();
c2.Status = 'New';
c2.priority = 'High';
c2.origin = 'Email';
cases.add(c2);

7.  Create a list of Student records (Assuming Student__c is a custom object with custom fields – Name, Emai , and Phone )

List<Student__c> students = new List<Student__c>();

Student__c std1 = new Student__c();
std1.Name__c = 'krishnan';
std1.Email__c = 'sfdcmeet@gmail.com';
std1.phone = '123';
students.add(std1);

Student__c std2 = new Student__c();
std2.Name__c = 'Kiran';
std2.Email__c = 'Kirankumar@gmail.com';
students.add(std2);