So, you have decided to commence your journey as a Salesforce developer and put your career in the fast-paced lane. In this session, we will learn about What is Apex in Salesforce? and How to use Apex. In this module, we will Introduction to Apex and Salesforce platform.

What is Apex in Salesforce?

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.

Join us as you embark on this wonderful journey to become a Salesforce developer in 2023.

How Apex Works in Salesforce

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 in Salesforce

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. It 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:​ It 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 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

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

Data TypeDescription 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
BooleanLogical value True, False, null
DateIndicates 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
DecimalA number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type Decimal.27.0

Variables

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

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

5 Types of Procedural Loops

  • 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

  • If Else…
  • Switch Statements
  • Ternary Operators

Collection

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

  1. List
  2. Set
  3. Map

Demo 3 – Using Collections

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

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

What is Apex Video

YouTube video

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 Salesforce Development is easy, Please check our full Salesforce Developer Training. So, learn at your pace and free will and ace your journey to Salesforce!

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.

Articles: 461

156 Comments

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

  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

  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

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

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

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

  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!

  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.

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

  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.

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

    Completed Assignment 1.

  11. 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);

  12. 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);

  13. 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);

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

  15. 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);

  16. 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);

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

  18. 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!

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

  20. 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);

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

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

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

  24. 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);

  25. 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);

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

  27. 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 🙂

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

  29. 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);

  30. 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);
    }
    }

  31. 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);

  32. 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);
    }
    }

  33. 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);

  34. 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);

  35. 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);

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

  37. 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);

  38. 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);

  39. 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);

  40. 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);

  41. 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);
    }
    }

  42. 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]);
    }
    }

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

  44. list listOfNumber = new list();
    integer totalListSum = 0;
    integer i = 0;
    while (i<=20){
    listOfNumber.add(i);
    if(math.mod(listOfNumber[i],2)==0){
    totalListSum = totalListSum + i;
    }
    i++;
    }
    System.debug('Total = '+ totalListSum);

  45. Map globalDescribe = Schema.getGlobalDescribe();
    List objectNames = new List(globalDescribe.keySet());

    List objectsWithMoreThan25Lookups = new List();

    for(String objName : objectNames) {
    try {
    Schema.DescribeSObjectResult objDescribe = globalDescribe.get(objName).getDescribe();
    List childRelationships = objDescribe.getChildRelationships();

    Integer lookupCount = 0;
    for(Schema.ChildRelationship relationship : childRelationships) {
    if(relationship.getField().getDescribe().getType() == Schema.DisplayType.Reference) {
    lookupCount++;
    }
    }

    if(lookupCount > 25) {
    objectsWithMoreThan25Lookups.add(objName);
    }
    } catch(Exception e) {
    // Catch any exceptions related to object access
    }
    }

    System.debug(‘Objects with more than 25 lookup relationships: ‘ + objectsWithMoreThan25Lookups);

  46. Map globalDescribe = Schema.getGlobalDescribe();
    List objectNames = new List(globalDescribe.keySet());

    List objectsWithMoreThan25Lookups = new List();

    for(String objName : objectNames) {
    try {
    Schema.DescribeSObjectResult objDescribe = globalDescribe.get(objName).getDescribe();
    List childRelationships = objDescribe.getChildRelationships();

    Integer lookupCount = 0;
    for(Schema.ChildRelationship relationship : childRelationships) {
    if(relationship.getField().getDescribe().getType() == Schema.DisplayType.Reference) {
    lookupCount++;
    }
    }

    if(lookupCount > 25) {
    objectsWithMoreThan25Lookups.add(objName);
    }
    } catch(Exception e) {
    // Catch any exceptions related to object access
    }
    }

    System.debug(‘Objects with more than 25 lookup relationships: ‘ + objectsWithMoreThan25Lookups);

  47. Integer totalSum = 0, i = 0, n= 0;
    List listOfOddNumbers = new List();

    While (i <= 20){
    if (Math.mod(i,2) != 0)
    {
    listOfOddNumbers.add(i);
    }
    i++;
    }

    While (n < listOfOddNumbers.size()){
    System.debug('Odd number is ' + listOfOddNumbers[n]);
    totalSum = totalSum + listOfOddNumbers[n];
    n++;
    }
    System.debug('total sum of odd number ' + totalSum);

  48. List ls = new List();
    Integer totalEvenValues =0;

    for(Integer i = 0;i <= 20; i++){
    ls.add(i);
    if(Math.mod(i,2)==0){
    totalEvenValues = totalEvenValues + i;
    }

    }
    System.debug('Total of Even numbers in the list is -'+totalEvenValues);

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

    Solution:
    Integer sum=0;
    List listOfIntegers = new List();
    For(Integer i=1;i<=20;i++)
    {
    listOfIntegers.add(i);
    }
    System.debug('Integers in the list:'+ listOfIntegers);

    For(Integer i=1; i < listOfIntegers.size(); i++){
    If(Math.mod(listOfIntegers[i],2)==0){
    sum = sum + listOfIntegers[i];
    }
    }
    System.debug('Total Sum:'+ sum);

  50. Integer sum=0;
    List listOfIntegers = new List();
    For(Integer i=1;i<=20;i++)
    {
    listOfIntegers.add(i);
    }
    System.debug('Integers in the list:'+ listOfIntegers);

    For(Integer i=1; i < listOfIntegers.size(); i++){
    If(Math.mod(listOfIntegers[i],2)==0){
    sum = sum + listOfIntegers[i];
    }
    }
    System.debug('Total Sum:'+ sum);

  51. List accList = new List{1,3,5,7,9,11,13,15,17,19};
    for(Integer count : accList) {
    if(count == 1 ) {
    Integer SUM = 1;
    system.debug(SUM);
    } else if(count == 5) {
    Integer SUM = 6;
    system.debug(SUM);
    } else if (count == 9){
    Integer SUM = 15;
    system.debug(SUM);
    } else if (count == 13) {
    Integer SUM = 28;
    system.debug(SUM);
    } else if (count == 17) {
    Integer SUM = 45;
    system.debug(SUM);
    }
    }

  52. trigger oddNumber on Account (before insert) {
    List numberedList = new List();
    Integer SUM =0;
    for(Integer i=0; i<21; i++) {
    numberedList.add(i);
    if(math.mod(numberedList[i],2) == 0){
    SUM = SUM + numberedList[i];
    System.debug(SUM);
    }
    }
    }

    FINAL SUM = 110

Leave a Reply

Your email address will not be published. Required fields are marked *