1. Apex練習問題

Exercise 1: Variable Declaration and Manipulation|Salesforce Apex Programming

Declare variables and perform operations according to the following conditions

  • Declare a variable a of type Integer and assign 10 to it.
  • Declare a variable b of type Integer and assign 20 to it.
  • Add a and b together and assign the result to the Integer variable sum.
  • Finally, output the value of sum to the console using System.debug().

Solution Example

        // Declare a variable a of type Integer and assign 10
        Integer a = 10;.
        
        // Declare variable b of type Integer and assign 20
        Integer b = 20;
        
        // Add a and b together and assign the result to variable sum of type Integer
        Integer sum = a + b;
        
        // Output the value of sum to the console using System.debug()
        System.debug('Sum of a and b: ' + sum);

Explanation

1. Declaring Variables

First, let us explain what a “variable” is.
A variable is like a “box” used to temporarily store data in a program. In this example, we will prepare a “box” to store numbers.

Declare a variable a of type Integer and assign 10

Integer a = 10;
  • Integer means “type” for integers, and in Apex, when you create a variable, you need to specify what kind of data (type) you want to handle.
  • a is the name of the “box”. You can choose this name yourself, but in this case I chose a.
  • = does not mean “equal,” but means to put (assign) the value written on the right side into the box on the left.
  • 10 is the number to put into the variable a.

Declare a variable b of type Integer and assign 20

Integer b = 20;

As with a above, now put the number 20 into the variable named b.

2. Adding Variables a and b

Next, add a and b together and put the result into another variable sum.

Integer sum = a + b;
  • a + b adds the numbers in a (10) and b (20).
  • The result of the addition is 30, so the result is put into a variable named sum.

3. Displaying the result on the console

Finally, the result of the addition (sum) is output to a place called the console.

System.debug('Sum of a and b: ' + sum);
  • System.debug() is a useful tool for checking the contents of variables in the middle of a program and for debugging.
  • ‘Sum of a and b: ‘ is part of the message, meaning “The sum of a and b is”. This string is attached to the value (30) contained in the sum and output.

Result displayed in the console

When you actually run this program, you will see the following message.

Sum of a and b: 30

This indicates that 30 was correctly calculated as a result of adding a and b together.

Summary of the entire process

  1. We created a box called “variable” with a and b to store numbers.
  2. We put 10 in a and 20 in b.
  3. We added them together and put the result in a new box called sum.
  4. System.debug() was used to display the result (the contents of sum) on the console.

This is the entire flow of the code.

Apex練習問題 recent post

  1. Exercise 2: Conditional Branching and Variabl…

  2. Exercise 1: Variable Declaration and Manipula…

関連記事

プロフィール

●氏名:ARAKAN
●年齢:32歳
●居住地:九州
●勤務先:情報システム部
●一言:とある企業でSalesforce管理者として勤務しているARAKANです。業務の中での気づきやTipsを投稿しています。
<保有資格>
・Salesforce認定 アドミニストレーター
・Salesforce認定 上級アドミニストレーター
・Salesforce認定 Sales Cloudコンサルタント
・Salesforce認定 Service Cloudコンサルタント
・Salesforce認定Marketing Cloud Account Engagement コンサルタント
・Salesforce認定Marketing Cloud Account Engagement スペシャリスト
●Name: ARAKAN
●Age: 32 years old
●Place of residence: Kyushu
●Workplace: Information Systems Department
●One word: My name is ARAKAN and I work as a Salesforce administrator at a certain company. I post my observations and tips during my work.
Qualifications held
・Salesforce Certified Administrator
・Salesforce Certified Senior Administrator
・Salesforce Certified Sales Cloud Consultant
・Salesforce certified Service Cloud consultant
・Salesforce Certified Marketing Cloud Account Engagement Consultant

カレンダー

October 2024
MTWTFSS
 123456
78910111213
14151617181920
21222324252627
28293031 

アーカイブ

Language Switcher

PAGE TOP