Apex DMLを使用してsObjectレコードをデータベースに挿入する

環境
Salesforce

構文
1.リストの宣言
List<Account> リスト変数名 = new List<Account>();

2.while(条件式){
//Account sObject をインスタンスする
Account sObject変数名 = new Account();
変数名.sObject属性=値

処理コード
}

3.オブジェクトリストにオブジェクトを追加する
リスト変数名.add(sObject変数名)

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class AccountHandler {
public static void insertAccount(Integer value){
Integer counter=1;
List<Account> addAccounts = new List<Account>();
while(counter <= value){
Account store= new Account();
store.Name ='Acme Inc' + counter;
store.AccountNumber ='000'+ counter;
addAccounts.add(store);
counter = counter +1;
}
insert addAccounts;
}
}
public class AccountHandler { public static void insertAccount(Integer value){ Integer counter=1; List<Account> addAccounts = new List<Account>(); while(counter <= value){ Account store= new Account(); store.Name ='Acme Inc' + counter; store.AccountNumber ='000'+ counter; addAccounts.add(store); counter = counter +1; } insert addAccounts; } }
public class AccountHandler {
    public static void insertAccount(Integer value){
        Integer counter=1;
        List<Account> addAccounts = new List<Account>();
        while(counter <= value){
            Account store= new Account();
            store.Name ='Acme Inc' + counter;
            store.AccountNumber ='000'+ counter;
            addAccounts.add(store);
            counter = counter +1;
        }
        insert addAccounts;
    }
}

insertAccountメソッドを実行する
例 AccountHandler.insertAccount(3);

Apex

Posted by arkgame