Introduction to Apex
Amit Chaudhary

Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates. He is a active blogger and founder of Apex Hours.

Introduction to Apex

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.

In this session we will learn about what is Apex and How to use Apex? All Apex Hours for Salesforce Developer Training will be recorded and will be available on our YouTube channel. Please Subscribe our YouTube Channel.

What is Apex?

Apex is a programming language developed by Salesforce. It is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform.

How Apex Works

Using syntax that looks like Java and acts like database stored procedures

What is Apex? and How Apex Works
How Apex Works

Features of Apex

Following are the features of the Apex is given below:

  1. Easy to Use: ​Apex is easy to use as it uses Java-like syntax, which is quite easy to understand and code. Salesforce Apex uses simple loop syntax, block and conditional statement syntax, object and array annotation, similar to Java.
  2. Integrated: ​Apex provide support of DML operations like Insert, Delete, Update and DML exception handling. Furthermore, it supports looping that allows the processing of multiple records at a time. In addition, it has support for SOQL and SOSL query handling, which return a set of subject records.
  3. Strongly Typed and Rigorous:​ Apex is a strongly typed language that means that the user needs to define every variable’s data type, which does not create any confusion at compile time.
  4. Multitenant Environment: ​Apex runs in a multitenant environment like other lighting platforms, which means only a single instance runs on the server and serves multiple tenants.
  5. Versioned: You can save your Apex code against different versions of the API. This enables you to maintain behavior.
  6. Easy to Test: ​Apex provides built-in support to create and run the unit test. It includes the test result so that the user can get to know how much code is covered. In addition, it ensures the executing of the whole code before any platform upgrades

When we should use Apex Code?

  • Customized applications
  • Tailored organization processes
  • Collaboration with external systems
  • Custom logic
  • Complex validation
When should I use Apex?

Salesforce Application Anatomy

 Salesforce Application Anatomy

Apex Data Types in Salesforce

Apex is a strongly typed language i.e we must declare the datatype of a variable when we first refer it.

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.  VGhpcyBEb2N1bWVudCBj
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 Apex

  • 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);

Recording

Here is link of recording

Agenda

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

Further Learning

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 check our next session. Make sure to subscribe our YouTube channel to get notification for video upload.

Summary

Learning Apex is easy, Please check our full Salesforce Developer Training. So, learn at your pace and free will and ace your journey to Salesforce!

Share this article

142 Comments

  • Is the link to join the live session if so I am unable to see it if you could share would be wonderful.

    • Say Connected we will share the registration link soon

    • I have completed Assignment `

  • Ok 👌look forward to 👍

  • please share the link

  • Link to join the session

    • Click on registration link in the post

  • Are you available on Udemy?

    • This is totally free, We will upload on YouTube.

      • Thanks for your videos. Waiting for upcoming videos.

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

  • ApexHours Completed my first Assignment of Apex. Looking forward to explore more

  • 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

    • Uploaded all content and Demo code as well

      • where ? 🙁

  • 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

    • integer sum=0;
      list listnumber=new list();
      integer i=1;
      while(i<=20)
      {
      listnumber.add(i);
      i++;
      }
      System.debug('listnumber 1 to 20:'+listnumber);
      for(integer i=1;i<listnumber.size();i++)

      {
      if(Math.mod(listnumber[i], 2)==0)
      {
      sum=sum+listnumber[i];
      System.debug('totalsum value:'+sum);
      }

      }
      System.debug('final totalsum::'+sum);

      out put:istnumber 1 to 20:1,2,3,4,5,6,7,8,9,10……
      inal totalsum::110

      • integer totalssum=0;
        list listofnumbers=new list();
        for( integer i=1; i<=20 ; i++)
        {
        listofnumbers.add(i);
        }
        System.debug('listnumbers 1 to 20:'+listofnumbers);
        for(integer i=1;i<listofnumbers.size();i++)

        {
        if(Math.mod(listofnumbers[i], 2)==0)
        {
        totalssum=totalssum+listofnumbers[i];
        System.debug('totalsum value:'+totalssum);
        }

        }
        System.debug('final totalsum::'+totalssum);

        • listofnumbers ko integer define karo

      • totalsum::210

  • Completed the assignment

  • Completed the assignment for day 1

  • Completed the first assignment

  • Completed the first Assignment

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

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

  • Completed Assignment for Day 1

  • Competed Assignment for day 1

  • 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!

    • We will upload all recording by 6 MARCH

      • Thanks

        Assignment Complete-Day 1

  • Wohoo, my assignment 1 is complete 🙂 Thank you so much for running these sessions, guys!

  • Completed the assignment of Day 1.

  • Day 1 -Assignment Complete

    Thank you for hosting these sessions.

  • My assignment for Day 1 is complete

  • Day 1- Assignment Completed.

    Thank you for hosting these session.

  • Assignment Complete

  • Thank you Jigar/Amit for this amazing Initiative.
    Completed my assignment for Day#1

  • Done with assignment. Thanks

  • Assignment 1 completed. Thank you deeply for this learning opportunity.

  • Assignment 1 completed. Thank you!

  • Assignment 1 completed. Thank you!

  • 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.

  • Completed Assignment for Day 1. Great first session. Excited to learn more!

  • Completed Day 1 Assignment

  • Assignment for Day 1 completed!

  • Completed the assignment for day 1

  • Completed the assignment for day 1.

  • Completed the assignment for day 1.

  • Completed the assignment for day 1

  • First Assignment completed

  • Completed Day 1 Assignment. Waiting eagerly for the next session.

  • Completed assignment for session 1 ..thank you for your efforts..

  • Completed assignment 1..thank you.

  • Completed the assignment

  • Thannks for the valuable session.
    Introduction to Apex Part 1 : Assignment completed

  • Completed Assignment 1

  • completed

  • COmpleted assignment 1 .:)

  • the assignment is complete

  • successfully completed assignment1 🙂

  • 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);

  • Thank you so much for wonderful session.

    Assignment is completed.

  • Competed Assignment for day 1

  • Completed first assignment for day 1

  • Completed assignment for day1.

  • 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.

  • It’s a great platform to interact with all great leaders who initiated this program to help people learn code.

    Completed Assignment 1.

  • Hi ..Thanks for the really useful and knowledgeable session..Completed Assignment for Day1.

  • Thanks for the wonderful session.Completed Assignment 1 of Day 1

  • Assignment Completed..

  • Assignment 1 completed.

  • 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);

  • 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);

  • assignment is completed

  • Assignment done

  • Day1 Assignment done

  • 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);

  • Day 1 Assignment is completed!

  • Day 1 Assignment is completed.

  • Assignment 1 completed

  • Completed Day 1 Assignment

  • complete Assigment Day 1

  • Completed Day 1 assignment

  • Completed the Assignment

  • Day 1 Assignment Done
    Thanks

  • Day 1 Assignment Done
    Thanks

  • Day 1 Assignment is completed.

  • Day1 Assignment Completed

  • Thanks for hosting this awesome session.
    Really looking forward my SalesForce Dev Career.
    Day 1- Assignment Completed.

  • Assignment 1: Completed.
    Thanks for the upskilling opportunity.

  • Day 1 Assignment Complete

  • Assignment is complete

  • #ApexHoursDev @ApexHours
    Day1 Assignment Completed

  • 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);

  • Day1 Assignment Completed

  • 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);

  • 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.

  • 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!

  • Assignment 1 Complete

  • Assignment 1 complete

  • 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 .

  • 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);

  • Day 1 Assignment Completed.

  • Day 1 Assignment Completed.

  • Done with Day 1 assignment

  • #ApexHoursDev @ApexHours
    Day1 Assignment Completed

  • Day 1 Assignment Completed.

  • Day 1 assignment completed

  • 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.

  • 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);

    • 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

  • 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.

    • Just check the other comment in this post only

  • 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);

  • 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);

  • 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

  • Hi i am Rahul… I love the way you guys delivery the session. Now i am preparing for developer role. But i can see the developer playlist is not completed. Can you update the playlist with all the video.
    Thank you in advance 🙂

  • Can you please upload the full ppt showed in videos for every session in .ppt format same as video

  • Integer listSize = 20;
    Integer sum=0;
    list testList = new list();
    for (Integer i =1; i<=listSize; i++)
    {
    testList.add(i);
    }
    System.debug('list' + testlist);
    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

  • List oddNumList=new List();
    for(Integer i=1;i<=20;i++)
    {
    if(Math.mod(i,2)!=0)
    {
    oddNumList.add(i); //List will contain all odd numbers from 1 t 20
    }
    }
    System.debug('List of Odd numbers : '+oddNumList);
    Integer sum=0;
    Integer count = 1;
    while(count< oddNumList.size())
    {
    System.debug('count is :'+(count+1)+' Number at count is : '+oddNumList[count]);
    /*
    List index starts from 0 so to add numbers at even position we need to consider odd index
    list -1,3,5,7,9,11,13,15,17,19 for us even position numbers are 3,7,11,15 & 19
    */
    if(math.mod(count, 2) != 0)
    {
    sum = sum + oddNumList[count];
    }
    count++;
    }
    System.debug('Total Sum is ' +sum);

  • list myEvenList = new list();
    integer sum = 0;
    for(integer i=1; i<=20; i++)
    {
    Integer reminder = math.mod(i, 2);
    if(reminder == 0){
    system.debug('Even: '+i);
    sum = sum + i;
    system.debug('Sum '+sum);
    }
    }

  • integer totalSum=0;
    List listOfNumbers = new List();
    for(integer i=1; i<=20; i++){
    listOfNumbers.add(i);
    }
    System.debug('listOfNumbers: ' + listOfNumbers);

    integer i=1;
    While(i < listOfNumbers.size()){
    if(Math.mod(listOfNumbers[i],2)==0){
    totalSum = totalSum + listOfNumbers[i];
    System.debug('totalSum Value: ' + totalsum);
    }

    i++;
    }
    System.debug('Final sumation: ' + totalSum);

  • public class ApexHours1 {
    public static void main(){
    Integer sum = 0;
    List iList = new List();
    for(Integer i = 0; i < 20; i++){
    iList.add(i);
    if(math.mod(i, 2) == 0){
    sum = sum + i;
    }
    }
    System.debug(sum);
    }
    }

  • Integer Sum = 0;
    Integer A= 20; //First 20 Numbers
    List NumList= New List();
    For (Integer i=0; i<=A; i++){
    Numlist.add(i);
    }
    System.debug(NumList);
    For(Integer i=0; i<Numlist.size(); i++){
    If(Math.mod(NumList[i], 2)==0){
    Sum= sum+i;
    }}
    System.debug(Sum);

  • integer sum=0;
    integer listsize=20;
    listnum = new list();
    integer i=1;
    while(i<=20){
    num.add(i);
    i++;
    }
    system.debug('num 1 to 20:' +num);
    for(integer i=1;i<num.size();i++){
    if(Math.mod(num[i],2)==0){
    sum=sum+num[i];
    system.debug('totalsum value:'+sum);
    }
    }
    system.debug('final totalsum:'+sum);

    • integer sum=0;
      integer listsize=20;
      listnum = new list();
      integer i=1;
      while(i<=20){
      num.add(i);
      i++;
      }
      system.debug('num 1 to 20:' +num);
      for(integer i=0;i<num.size();i++){
      if(Math.mod(i,2)==0){
      sum=sum+num[i];
      system.debug('totalsum value:'+sum);
      }
      }
      system.debug('final totalsum:'+sum);

  • integer sum=0;
    integer listsize=20;
    listnum = new list();
    integer i=1;
    while(i<=20){
    num.add(i);
    i++;
    }
    system.debug('num 1 to 20:' +num);
    for(integer i=0;i<num.size();i++){
    if(Math.mod(i,2)==0){
    sum=sum+num[i];
    system.debug('totalsum value:'+sum);
    }
    }
    system.debug('final totalsum:'+sum);

  • Assignment is complete!

    Integer MaxListSize = 20;
    Integer EvenSum = 0;
    List OddListOfNo = new List();

    for(Integer i= 1; i<= MaxListSize; i=i+2 )
    {
    OddListOfNo.add(i);
    }
    System.debug('List of Odd Numbers: ' + OddListOfNo);
    //List of Odd Numbers: 1,3,5,7,9,11,13,15,17,19
    System.debug('Size of Odd Numbers: ' + OddListOfNo.size());
    //Size of Odd Numbers: 10

    for(Integer i = 0; i < 10; i++)
    {
    if(Math.mod(OddListOfNo[i],2) != 0)
    {
    EvenSum = EvenSum + OddListOfNo[i];
    System.debug('Sum of Even Position Numbers: ' + EvenSum);
    i++;
    }
    }
    System.debug('Sum of Even Position Numbers: ' + EvenSum);
    //Sum of Even Position Numbers: 45

    • Updated Answer

      Integer MaxListSize = 20;
      Integer EvenSum = 0;
      List OddListOfNo = new List();

      for(Integer i= 1; i<= MaxListSize; i=i+2 )
      {
      OddListOfNo.add(i);
      }
      System.debug('List of Odd Numbers: ' + OddListOfNo);
      //List of Odd Numbers: 1,3,5,7,9,11,13,15,17,19
      System.debug('Size of Odd Numbers: ' + OddListOfNo.size());
      //Size of Odd Numbers: 10

      for(Integer i = 0; i < OddListOfNo.size(); i++)
      {
      EvenSum = EvenSum + OddListOfNo[i];
      System.debug('Sum of Even Position Numbers: ' + EvenSum);
      i++;
      }
      System.debug('Sum of Even Position Numbers: ' + EvenSum);

      //Sum of Even Position Numbers: 45

  • List listOfNumbers = new List();
    integer i=1;
    while(i<=20){
    listOfNumbers.add(i);
    i++;
    }
    System.debug('The list of Numbers Are:-'+listOfNumbers);
    integer Sum=0;
    for(integer i=0; i<20; i++){
    if(math.mod(listOfNumbers[i],2)!=0){
    sum=sum+listOfNumbers[i];
    System.debug('The addition is:'+sum);
    }
    }
    System.debug('The addition is:'+sum);

  • List listOfNumbers = new List();
    integer i=1;
    while(i<=20){
    listOfNumbers.add(i);
    i++;
    }
    System.debug('The list of Numbers Are:-'+listOfNumbers);
    integer Sum=0;
    for(integer i=0; i<20; i++){
    if(math.mod(listOfNumbers[i],2)==0){
    sum=sum+listOfNumbers[i];
    System.debug('The addition is:'+sum);
    }
    }
    System.debug('The addition is:'+sum);

  • List listofNumbers = new List();
    Integer maxSize = 20;
    Integer sum = 0;
    Integer x = 1;
    while(x <= maxSize)
    {
    listofNumbers.add(x);
    if(Math.mod(x,2)==0)
    sum = sum + x;
    x++;
    }
    System.debug('Sum of digits at even numbers is : ' + sum);

  • Integer totalSum = 0;
    List listOfNumbers = new List();
    for(Integer i = 1; i <= 20; i++){
    listOfNumbers.add(i);
    }

    system.debug('listOfNumbers: '+ listOfNumbers);

    for(Integer i = 1; i < listOfNumbers.size(); i++){
    if(Math.mod(listOfNumbers[i],2) == 0){
    totalSum = totalSum + listOfNumbers[i];
    system.debug('totalSum Value: ' + totalSum);

    }
    }
    system.debug('Final sum: '+ totalSum);

  • integer totalsum = 0;

    list Numberslist = new list();
    integer i = 0;
    while(i<=20){
    Numberslist.add(i);
    i++;
    }
    system.debug(Numberslist);

    for(integer i=0; i<Numberslist.size();i++){
    if(math.mod(i,2)==0){
    totalsum = totalsum+i;
    system.debug(i);
    }
    else{
    system.debug('This is an odd number'+i);
    }
    }

  • integer totalsum = 0;

    list Numberslist = new list();
    integer i = 0;
    while(i<=20){
    Numberslist.add(i);
    i++;
    }
    system.debug('The numbers list is:'+Numberslist);
    for(integer i=0; i<Numberslist.size();i++){
    if(math.mod(Numberslist[i],2)==0){
    totalsum = totalsum+Numberslist[i];
    system.debug('The total sum is:' +totalsum);
    }
    else{
    system.debug('This is an odd number'+Numberslist[i]);
    }
    }

  • list listnum = new list();
    integer totalsum = 0;
    integer i=0;
    while (i<=20){
    listnum.add(i);
    totalsum = totalsum + i;
    i++;
    }
    system.Debug('sum = '+ totalsum);

Leave a reply

Keep in Touch

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,230 other subscribers

Search

Our Supporter

RECENT POSTS

Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!