Introduction to Apex

Episode 1 – Introduction to Apex (Part 1)

So, you have decided to commence your journey as a Salesforce developer and put your career in the fast-paced lane. Join us as you embark on this wonderful journey to become a champion Salesforce developer. In this module we will introduce the Salesforce platform and acquaint the attendees with Apex.

Agenda

  • Declarative Vs Programmatic Approach of Development
  • Datatypes in Apex
  • Operators in Apex
  • System.Debug()
  • Familiarizing to Dev Tools – VS Code, Anonymous Block
  • Q & A

Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Apex Hours for Student sessions will be recorded and will be available on our YouTube channel. Please Subscribe our YouTube Channel.

Here is Keynote from our session

Why code?

  • Customized applications
  • Tailored organization processes
  • Collaboration with external systems
  • Custom logic
  • Complex validation

What is Apex?

Strongly typed, object-oriented programming language that allows developers to execute flow & transaction control statements on Salesforce servers in conjunction with calls to the API.

How Apex Works
How Apex Works


Salesforce Application Anatomy

Data Types in Apex

Data Type Description Sample Value
Blob (Binary Large Object) Binary data stored as a single object. You can convert this data type to String or from String using the toString() and valueOf() methods, respectively.  VGhpcyBEb2N1bWVudCBjb250YWlucyBzYW1wbGUgdGV4dCBmb3IgdGhlIFZpc3VhbGZvcmNlIA0KRG9jdW1l
Boolean Logical value True, False, null
Date Indicates a particular day. Unlike Datetime values, Date values contain no information about time.  2020-02-10
Datetime A value that indicates a particular day and time, such as a timestamp. 2020-02-10 07:29:09
Time A value that indicates a particular time.  22:40:36.003Z
Decimal A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type Decimal. 27.0

Variables in Apex

Variables are in-memory containers that can hold data belonging to a specific data-types during processing and can be reused based on their scope.

Integer inValue = 27;
System.debug(‘ID :’ +inValue);

Demo 2: Using Variables & Operators in Apex

String o = ‘Hello World’; //String
System.debug(‘Blob: ‘ + Blob.valueOf(o)); //Blob

Boolean isActive = true;
System.debug(‘Boolean: ‘ + isActive); //Boolean
System.debug(‘Date: ‘ + Date.newInstance(2020, 01, 18)); //Date

Datetime currDateTime = Datetime.now();
System.debug(‘Date: ‘ + currDateTime); //DateTime
System.debug(‘Time: ‘ + Time.newInstance(22, 40, 36, 03)); //Time

Decimal decValue = 27.01;
System.debug(‘Decimal: ‘ + decValue); //Decimal

Id recordId = [Select Id From Account Limit 1][0].Id;
System.debug(‘ID: ‘ + recordId); //ID

Integer intValue = 27;
System.debug(‘Integer: ‘ + intValue); //Integer

Long longValue = 2147483648L;
System.debug(‘Long: ‘ + longValue); //Long

Object objValue = o;
System.debug(‘Object: ‘ + objValue); //Object

Iterating Collections in Apex

5 Types of Procedural Loops in Apex

  • Do {Code Block} While (Boolean Exit Condition)
  • While (Boolean Exit Condition)
  • For
    • For (Initialization; Boolean Exit Condition; Increment) {Code Block}
    • For (Variable : Array or Set) {Code Block}
    • For (Variable : [Inline SOQL Query]) {Code Block}

Conditional Statements in A

  • If Else…
  • Switch Statements
  • Ternary Operators

Collection in Apex

We have all below collection option in apex. Please check this post to learn more about collection in Apex.

  1. List
  2. Set
  3. Map

Demo 3 – Using Collections in Apex

List<Integer> listOfNumbers = new List <Integer> ();
listOfNumbers.add(1);
listOfNumbers.add(2);
listOfNumbers.add(3);
listOfNumbers.add(3);
System.debug(‘listOfNumbers: ‘ + listOfNumbers);

Set<Integer> setOfNumbers = new Set<Integer>();
setOfNumbers.add(3);
setOfNumbers.add(2);
setOfNumbers.add(2);
setOfNumbers.add(1);
System.debug(‘setOfNumbers: ‘ + setOfNumbers);

Map <Integer,String> mapOfIntegerString = new Map <Integer,String> ();
mapOfIntegerString.put(1, ‘one’);
mapOfIntegerString.put(2, ‘two’);
mapOfIntegerString.put(3, ‘three’);
mapOfIntegerString.put(3, ‘four’);
System.debug(‘mapOfIntegerString: ‘ + mapOfIntegerString);
System.debug(‘Value at 3: ‘ + mapOfIntegerString.get(3));

Demo 4 – Using Loops in Apex

List<integer> listOfNumbers = new List <integer> ();
Integer maxListSize = 5;
for(Integer i = 1; i <= maxListSize; i++){
listOfNumbers.add(i);
}
System.debug(‘listOfNumbers: ‘ + listOfNumbers);

When should I use Apex?

Recording

Here is link of recording

Episode 1 will be presented by Jigar Shah on Feb 10, 2020 at 6 PM Indian Standard Time.

Further Learning

Apex Hours TrailMixes

Assignment

Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.

Write an Apex program to store all the numbers in a List starting from 1 until 20. Only numbers stored at even positions in the List should be added to get their sum which should be output to the Debug Log.

(Hint: Use a while loop and if else along with System. debug()

Don’t forget to register for our next session. Check this post for all other session details.

Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

So, learn at your pace and free will and ace your journey to Salesforce!

Thanks,

20,851 total views, 3 views today

116 thoughts on “Episode 1 – Introduction to Apex (Part 1)”

  1. Does this entire Developer Program cover the Salesforce Developer Certification modules Or Does it help in Developer Certification?

    Reply
  2. Hello Jigar/Apex Team – Thanks for a good starting session for Apex development today. Appreciate your team efforts on the same. I am aware it will take some time and efforts to upload daily recording on youtube and this site, however could you please upload daily ppt used and trailhead links immediately after each session so that we can refer them before next day’s session. Also seen that ppt is not uploaded for last year’s admin sessions and trailhead links for also few. Could you please check that once and upload ppt and trailhead links for all admin session? It will be very useful. – Amit

    Reply
  3. Assignment Answer :

    Integer listSize = 20;
    Integer sum=0;
    for (Integer i =1; i<=listSize; i++)
    {
    testList.add(i);
    }
    Integer count = 0;
    while(count < 20 )
    {
    if(math.mod(count, 2) == 0)
    {
    sum = sum + testList[count];
    }
    count++;
    }
    System.debug('Total Sum is ' +sum);

    // Total Sum is 100

    Reply
  4. Many thanks for arranging sessions even in busy schedules. Really appreciate your team efforts.
    I have completed first assignment.

    Reply
  5. Many thanks for arranging sessions even in your busy schedules. Really appreciate your team efforts.
    I have completed first assignment given today.

    Reply
  6. I realize that it takes time to accomplish this, but the sooner you could post the video to Youtube the better. For me, it is 4:30am for the sessions. I made it today and fell asleep during it. It had nothing to do with you guys! It was just too early in the morning to be awake. I’d love to be able to schedule watching the video the same day it was done and do the work.

    Thanks for being awesone!

    Reply
  7. Thank you guys so much for motivating other to learn with your good work. This is really exciting and definitely a staring point for people like me to achieve goals to become a salesforce developer. Assignment is completed for day1 #Apexhours session. Looking forward to learn and experience the awesomeness with in this journey.

    Reply
  8. List mylist=new list();

    Integer sum=0;
    for (Integer i =1; i<=20; i++)
    {
    mylist.add(i);
    //system.debug(mylist);
    }
    Integer j = 0;
    while(j < 20 )
    {
    integer check=math.mod(j, 2);
    if(check == 0)
    {
    sum = sum + mylist[j];
    }
    j++;
    }
    System.debug('Sum is ' +sum);

    Reply
  9. ApexHours is the best platform that i have seen so far. Thanks for helping people to code. Its is good initiative by the great people working on Salesforce Platform to help people code.

    Thanks for all you hard work to keep this running!! πŸ™‚

    I have completed Assignment 1.

    Reply
  10. Assignment day#1
    —————————-

    Integer totalsum=0;
    List numbers = new List();
    for (Integer i = 1; i <= 20; i ++)
    {
    numbers.add(i);
    }

    //System.debug('listofnumbers'+ numbers);

    for (Integer i=1;i<numbers.size();i++)
    {
    if (Math.mod(numbers[i],2)==0)
    {
    totalsum= totalsum + numbers[i];

    }
    }

    System.debug('total sum is'+totalsum);

    Reply
  11. Assignment#1
    ————————
    Integer totalsum=0;
    List numbers = new List();
    for (Integer i = 1; i <= 20; i ++)
    {
    numbers.add(i);
    }

    //System.debug('listofnumbers'+ numbers);

    for (Integer i=1;i<numbers.size();i++)
    {
    if (Math.mod(numbers[i],2)==0)
    {
    totalsum= totalsum + numbers[i];

    }
    }

    System.debug('total sum is'+totalsum);

    Reply
  12. Hello,
    Assignment Day 1 completed.
    Integer Totalsum = 0;
    List listOfNumbers = New List ();
    For(Integer i=0; i<20; i++){
    listOfNumbers.add(i+1);
    }
    System.debug('listOfNumbers:' +listOfNumbers);
    For(Integer j=0; j<20; j++){
    if(Math.mod(j,2)==0){
    Totalsum = Totalsum +listOfNumbers[j];
    System.debug('Totalsum value:' +Totalsum);
    }
    }
    System.debug('Final summation:' +Totalsum);

    Reply
  13. Integer Totalsum = 0;
    List listOfNumbers = New List ();
    For(Integer i=0; i<20; i++){
    listOfNumbers.add(i+1);
    //System.debug (listOfNumbers[i]);
    }
    System.debug('listOfNumbers:' +listOfNumbers);
    For(Integer j=0; j<20; j++){
    if(Math.mod(j+1,2)==0){
    Totalsum = Totalsum +listOfNumbers[j];
    //System.debug('Totalsum value:' +Totalsum);
    }
    }
    //Final Summation: 110
    System.debug('Final Summation:' +Totalsum);

    Reply
  14. Integer totalsum=0;
    List numbers = new List();
    for (Integer i = 1; i <= 20; i ++)
    {
    numbers.add(i);
    }

    //System.debug('listofnumbers'+ numbers);

    for (Integer i=1;i<numbers.size();i++)
    {
    if (Math.mod(numbers[i],2)==0)
    {
    totalsum= totalsum + numbers[i];

    }
    }

    System.debug('total sum is'+totalsum);

    Reply
  15. I am new to Salesforce ecosystem and interested in development .Luckily I got to know about this program and started my SalesforceDevelopment journey with ApexHours.Thank you for the great content and helping the community.

    Reply
  16. Great platform and it help people to learn code. i am new to the world however, looks with your support i would be able to learn much!

    Reply
  17. DAY 1 Assignment Complete

    This is a great initiative and love the great content .
    Being a Salesforce Admin , trying to transition to being a Developer .Hope I get there .

    Reply
  18. I just went through the Apex hours DAY 1 Video. Its very usefull and I have completed the assignment 1:

    Integer sumofnum = 0;
    List listofn = new List();
    for (Integer i=1; i<=20; i++){
    listofn.add(i);
    }
    System.debug('listofn: '+listofn);

    for (Integer i=1; i < listofn.size(); i++){
    if(Math.mod(listofn[i],2) ==0){
    sumofnum = sumofnum + listofn[i];
    system.debug('sumofnum: ' + sumofnum);
    }
    }
    system.debug('Finalsumofnumbers: ' + sumofnum);

    Reply
  19. Thank you so much for these sessions. It really means a lot.
    completed assignment one.

    I just encountered an issue when going through part one of basic loops.
    Program which you have created at 1:18:43, the second loop to add the even numbers.
    The loop you started was from index 1 but it should be from 0.

    Correct me if i am wrong.

    Reply
  20. Code for Assignment Day 1
    ——————————–
    Integer countSize = 20;
    Integer counter = 0;
    Integer sum=0;
    List listOfNumbers = new List ();
    for (Integer i =1; i<=countSize; i++){
    listOfNumbers.add(i);
    }
    System.debug('Size of list:' +listOfNumbers.size());
    while(counter < listOfNumbers.size() ){
    if(math.mod(counter, 2) == 0){
    sum = sum + listOfNumbers[counter];
    }
    counter++;
    }
    System.debug('Total Sum for numbers which stored at even positions:' +sum);

    Reply
    • Hi Ankit,
      Integer countSize = 20;
      Integer counter = 0;
      Integer sum=0;
      List listOfNumbers = new List ();
      for (Integer i =1; i<=countSize; i++){
      listOfNumbers.add(i);
      system.debug('the list'+i);
      }
      System.debug('Size of list:' +listOfNumbers.size());
      while(counter<listOfNumbers.size() ){
      if(math.mod(listOfNumbers[counter], 2) == 0){
      sum = sum + listOfNumbers[counter];
      system.debug('the value of sum' +sum);
      }
      counter++;
      }

      System.debug('Total Sum for numbers which stored at even positions:' +sum);
      The sun should come as 110

      Reply
  21. I realize this training has ended and I attempted the assigments, firstly for Assignment 1; Where can i find the answers to see if I am correct.

    Reply
  22. integer Sum=0;
    List listOfNumbers = new List();
    for(integer i=1; i<=20; i++){
    listOfNumbers.add(i);
    }
    System.debug('The list of Numbers Are:-'+listOfNumbers);

    for(integer i=0; i<20; i++){
    if(math.mod(i,2)==0){
    sum=sum+listOfNumbers[i];
    }
    }
    System.debug('The addition is:-'+sum);

    Reply
  23. List listOfNumbers = new List ();
    Integer maxListSize = 20;
    integer j=0;
    for(Integer i = 1; i <= maxListSize; i++){
    listOfNumbers.add(i);
    j=listOfNumbers.size();
    }
    integer i=0;
    integer k=0;
    for(i=0;i<=j;i+=2)
    {
    k=k+i;
    }
    System.debug(k);

    Reply
  24. Integer sum=0;
    List numList= new List();

    for (Integer i=0; i<=20; i++)
    {
    numList.add(i);
    }

    system.debug('numbers added' +numList);

    for(Integer count=1;count<numList.size();count++)
    {
    if(Math.mod(numList[count], 2)==0)
    {
    sum= sum + numList[count];

    }

    }
    system.debug('numbers added in even list' +sum);

    Answer is 110

    Reply

Leave a Comment