So, you have decided to commence your journey as a Salesforce developer and put your career in the fast-paced lane. In this session/post, we will learn about What is Apex in Salesforce? and How to use Apex. In this module, we will understand the core fundamentals of Apex for Salesforce Developer and Admin. Let’s start with a complete guide for Salesforce Apex.
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
Features of Apex in Salesforce
The features of the Apex are given below:
- 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, and object and array annotation, similar to Java.
- 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 supports SOQL and SOSL query handling, which return a set of subject records.
- 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.
- 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.
- Versioned: You can save your code against different versions of the API. This enables you to maintain behavior.
- 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 should we use the Apex Code?
- Customized applications
- Tailored organization processes
- Collaboration with external systems
- Custom logic
- Complex validation
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 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
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 the below collection options in apex. Please check this post to learn more about the collection.
- List
- Set
- Map
Learn Master in Salesforce Apex collection.
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);
Learn more Fundamentals of Apex for Salesforce: A Starter’s Tutorial.
What is Apex 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!
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 `
can you help with the syntax of the assignment
Solution is posted in comment, Feel free to post your solution we will help
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
Thanks for helping
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 Day 1 Assignment.
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 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 🙂
Here is all recording https://www.youtube.com/playlist?list=PLaGX-30v1lh1e8roeCUumUEel5ukdPubj
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
Keep learning
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);
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);
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);
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);
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);
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);
Keep up the good work.
Is there any official solution for the assignment?
We did not posted the solution. Solution are in comment.
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);
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);
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);
}
}
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
list numl = new list();
integer n= 0;
for(integer i=1;i<=20;i++){
numl.add(i);
if(math.mod(i,2) == 0){
n=n+i;
}
}
system.debug(n);
Keep Up the good work.
Integer totalsum=0;
Integer i=0;
List as1 = new List();
while(i<=20)
{
as1.add(i);
i++;
}
System.debug('The values of list are '+as1);
for(Integer i=0;i<=as1.size();i++)
{
if(Math.mod(as1[i],2) == 0)
{
totalsum = totalsum + as1[i];
System.debug('the values are '+totalsum);
}
}
System.debug('final sum is'+totalsum);
Help me, my code shows this error : List index out of bounds:21
List numbers = new List();
for (int i = 1; i <= 20; i++) {
numList.add(i);
}
for (int i = 0; i < numList.size(); i++) {
if (i % 2 == 0) {
evenSum += numList.get(i);
}
}
System.debug(evenSum);