Saturday, September 22, 2012

Apex Examples


    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public with sharing class CreateContactFromCan {

  //Declare a method that does not return anything and takes one input parameter of a Candidate list object called candsFromTrigger
  public void createContact (List<Candidate__c> candsFromTrigger){

    //Select the Recruiting record from the database and assign to an object called candAcct from the sObject Account class
    Account candAcct = [Select a.Name, a.Id From Account a Where a.Name = 'Recruiting'];

    //Instantiate a Contact list object from the List class called conList
    List<Contact> conList = new List<Contact>();

    //Declare a For list loop to loop through the input parameter list candsFromTrigger with an iterationvariable called currentCandidate
    for(Candidate__c currentCandidate:candsFromTrigger){

      //Instantiate an object called con from the sObject class contact
      Contact con = new Contact();
       
      //TODO: Set the attribute AccountID of the con object to the value of the Id attribute of the candAcct object
      con.AccountID = candAcct.Id;

      //Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object
      con.FirstName = currentCandidate.First_Name__c; 

      //Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object
      con.LastName = currentCandidate.Last_Name__c;

      //Set the attribute Email of the con object to the value of the Email__c attribute of the currentCandidate object
      con.Email = currentCandidate.Email__c;

      //Add the con object to the conList
      conList.add(con);            
    }
    
    //Persist the conList to the database
    Database.insert(conList);                
  }
}

    
1
2
3
4
5
6
7
8
9
trigger CreateContact on Candidate__c (after insert) {
        
        //TODO: Instantiate an object called cc from the class CreateContactFromCan
        CreateContactFromCan cc = new CreateContactFromCan ();

        //TODO: Inovke the method createContact and send a List of Candidates as an input parameter
        cc.createContact(Trigger.new);

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class BankAcct {
 //TODO: Declare a private integer attribute called balance initalize to zero
 private integer balance = 0;

 // TODO: Declare a public string attribute called acctName
 public string acctName;


 // Declare a public string attribute called accttype
 public string accttype;


 //TODO: Declare a method named makeDeposit that does not return anything and one input parameter of type integer called deposit
 
 public void makeDeposit (integer deposit){
   
   balance = balance + deposit;

         //TODO: Add the deposit input parameter to the balance attribute
  
 }

 //Declare a method named getBalance that returns an integer and no input parameter  
 public integer getBalance(){
          //Return the balance attribute
    return balance;
 }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.