Salesforce:クイックスタート: Apex
Trailheadのクイックスタート:Apexをやってみました。
はじめてのクラス
副題は「はじめてのクラスを作成しましょう」です。クラスって?と思いながら作成しています。※「クラスとはオブジェクト指向プログラミングにおいて、オブジェクトの設計図に相当するもの」??オブジェクト指向のプログラミング???もよくわからないのですが、今は深堀せずに進めます。
これでクラスが作成されたので 次は
{}の中に「クラス本文=何か役に立つことを実行するメソッドを一つ以上」を入れるとのこと。
クラス本文=メソッド
「クラス本文=メソッド」は用意されているコードをコピペするという課題でした。
public class OlderAccountsUtility { public static void updateOlderAccounts() {
// Get the 5 oldest accounts
Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
// loop through them and update the Description field
for (Account acct : oldAccounts) {
acct.Description = 'Heritage Account';
}
// save the change you made
update oldAccounts;
}
}
課題はこれで↑完了なのですが、解説をもとに興味本位の我流解読をしてみます。なお、正解ではありません。なにぶんオブジェクト指向??の段階です。この先勉強しながら答え合わせすると面白いかなと思ってのものです。
メソッドを分解してみる
--------------------------------------------------------
public static void updateOlderAccounts() {
// Get the 5 oldest accounts
Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
// loop through them and update the Description field
for (Account acct : oldAccounts) { acct.Description = 'Heritage Account'; }
// save the change you made
update oldAccounts;
}
--------------------------------------------------------
①public static void updateOlderAccounts() と
その{}中身↓
②// Get the 5 oldest accounts
③// loop through them and update the Description field
④// save the change you made
①メソッド名
public static void updateOlderAccounts() {
}
Public Static Void :直訳▷公開静的空欄 ???
javaの説明では、
public「どこからでも呼び出せる」
static「インスタンスなしに使える」インスタンス??
void「戻り値なし」=returnを使用しない
updateOlderAccounts=メソッド名
() …
①はメソッド名とメソッドの性格指定といったところ?
メソッドの内容{}は次の②③④
②5つの最も古い取引先をGetする
//がつく行はコメント
// Get the 5 oldest accounts
Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
Accunt[] はオブジェクト「取引先」を指定してる様子?
oldAccounts=[ ]
oldAccountsに"SELECT Id, Description「説明」 FROM Account ORDER BY CreatedDate ASC LIMIT 5「作成日 昇順:小さい順 限定5」"をGET
③ループして「説明」項目を更新
// loop through them and update the Description field
for (Account acct : oldAccounts) {
acct.Description = 'Heritage Account';
}
for (Account acct : oldAccounts)
forはjavaでループの意
()の中がループ範囲としてoldAccountsは②で指定している。
{}の中がループ処理内容
acct.Description = 'Heritage Account'
④変更を保存
// save the change you made
update oldAccounts;
}
update oldAccounts ②で指定したoldAccountsを更新
デバッグ
OlderAccountsUtility.updateOlderAccounts();
クラス名.メソッド名()
②で指定した取引先の作成日の古い順5レコードの説明項目が ③で指定した「Heritage Account」に ④更新されている
おお!組み立てた結果でこうして動くのがやはり楽しくて好きなのだわと思います。
この記事が気に入ったらサポートをしてみませんか?