<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>サンプルコード &#8211; AI開発実践ラボ</title>
	<atom:link href="https://creativecontentlabtokyo.com/category/%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89/feed/" rel="self" type="application/rss+xml" />
	<link>https://creativecontentlabtokyo.com</link>
	<description>AIで、作る力を手に入れる。</description>
	<lastBuildDate>Sat, 04 Apr 2026 21:09:25 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Batch Apexサンプルコード（取引先の部門を一括更新するバッチ処理）</title>
		<link>https://creativecontentlabtokyo.com/apex-batch-update-account-sample-code/</link>
		
		<dc:creator><![CDATA[セールスフォース標準化推進ラボ]]></dc:creator>
		<pubDate>Sun, 03 Jul 2022 07:42:16 +0000</pubDate>
				<category><![CDATA[サンプルコード]]></category>
		<category><![CDATA[Apex]]></category>
		<category><![CDATA[Apex Trigger]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[スケジュールジョブ]]></category>
		<category><![CDATA[テスト]]></category>
		<guid isPermaLink="false">https://creativecontentlabtokyo.com/?p=3662</guid>

					<description><![CDATA[バッチ処理の概要 処理概要：取引先の部門項目を一括で更新するApexバッチ処理となります。 以下、取引先部門が未設定の取引先レコードに対して、一括で&#8217…]]></description>
										<content:encoded><![CDATA[<h2>バッチ処理の概要</h2>
<p>処理概要：取引先の部門項目を一括で更新するApexバッチ処理となります。</p>
<p>以下、取引先部門が未設定の取引先レコードに対して、一括で&#8217;未設定&#8217;という固定値を設定する処理</p>
<p><a href="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧.jpg"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3670" src="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧.jpg" alt="" width="1920" height="1089" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧.jpg 1920w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧-300x170.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧-1024x581.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧-768x436.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-処理前の取引先一覧-1536x871.jpg 1536w" sizes="(max-width: 1920px) 100vw, 1920px" /></a></p>
<p>バッチを実行することで、以下のように取引先部門に’未設定’の文言が一括でセットされます。</p>
<p><a href="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧.jpg"><img decoding="async" class="alignnone size-full wp-image-3668" src="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧.jpg" alt="" width="1920" height="1096" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧.jpg 1920w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧-300x171.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧-1024x585.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧-768x438.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-更新後の取引先一覧-1536x877.jpg 1536w" sizes="(max-width: 1920px) 100vw, 1920px" /></a></p>
<h2>Batch Apexのサンプルコード</h2>
<p>Batch Apexのサンプルクラス、スケジューラ、テストクラスの基本セット</p>
<p>詳細な仕様は公式の開発者ガイドを参照「<a href="https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_batch_interface.htm">Apexの一括処理</a>」してください。</p>
<h3>取引先の部門更新処理バッチ（BatchApexUpdateAccount.cls）</h3>
<pre class=""><code class="language-apex code_break">/**
 * プログラム名：BatchApexUpdateAccount
 * 概　　　要　：取引先の部門を一括で更新するためのバッチ処理
 * 作　成　日　：2022/07.01
 * 作　成　者　：cclt(David)
 * 変更履歴　　：
 */
global class BatchApexUpdateAccount implements Database.Batchable&lt;sObject&gt;, Database.Stateful {

    //取引先部門に設定する固定値を定義
    private final String SITE_DEFAULT_VALUE = '未指定';

    /**
     * スタートメソッドでは、QueryLocatorを使ってバッチ処理を行うレコードを取得する
     * 本サンプルでは、取引先の部門を更新するため部門がNULLのレコードを全て取得する
     **/
    global Database.QueryLocator start(Database.BatchableContext BC) {
        
        //取引先部門（Site）が未設定の取引先レコードをすべて取得
        String query = 'SELECT Id, Name, Site FROM Account Where Site = null';
        return Database.getQueryLocator(query);
    }
        
    /**
     * スタートで取得したレコードのリストをパラメータとして、メイン処理を実行
     * バッチ実行時に処理するレコード数が指定されていない場合、デフォルトでは200レコードずつ分割して処理される。
     */
    global void execute(Database.BatchableContext BC, List&lt;Account&gt; accList) {
        
        //取得した取引先部門が未指定のレコードについて、固定値'未指定'をセットする。
        for(Account acc : accList) {        
            try {
                //取引先部門に固定値をセット
                acc.Site = this.SITE_DEFAULT_VALUE;
            } catch(Exception e) {
                //桁数を超過する文字列をセットした場合などにエラーが発生します
                System.debug('エラーが発生しました。' + e.getMessage());
            }
        }
        /**
         * Database.SaveResultを使って処理したレコード1件ずつチェックし
         * 異常終了（エラー）の場合にはCSVファイルにエラーメッセージを添付しメール通知する
         * **/
        Database.SaveResult [] updResultList = Database.update(accList,false);
        for (Database.SaveResult di : updResultList) {
            //エラーの場合は、レコードにエラーメッセージを設定
            if (!di.isSuccess()) {
                for(Database.Error err : di.getErrors()){
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('登録エラー: ' + err.getFields());
                }   
            } else {
                System.debug('正常終了');
            }
        }
    }   
    
    /**
     * バッチ処理の最後に実行する処理を定義
     * バッチが異常終了した場合にもfinishで定義した処理は実行される
     */
    global void finish(Database.BatchableContext BC) {
        // 
        System.debug('finish');
    }
}
</code></pre>
<div class="cclt-compat-mybox st-mybox-class" style="background-color:#ffebee;border:2px solid #ef9a9a;border-radius:5px;margin:25px 0 25px 0;"><div class="cclt-compat-mybox__title" style="color:#ef5350;">注意ポイント</div><div class="cclt-compat-mybox__content"></p>
<p>バッチ処理を実装するには、Database.Batchableを継承する必要がありますが、合わせてDatabase.Statefulを指定することを忘れないように。</p>
<p>Database.Statefulを指定しない場合、Execute内で更新されたメンバ変数が引き継がれなくなります。</p>
<p></div></div>
<h3>開発コンソールでバッチを直接実行する</h3>
<div class="cclt-compat-cmemo" style="background-color:#fafafa;color:#000000;border:px solid ;"><span class="cclt-compat-cmemo__icon fa fa-file-text-o" style="color:#919191;font-size:150;"></span><div class="cclt-compat-cmemo__text"></p>
<p>//バッチ処理を開発コンソールで実行する場合は、以下のコメントを利用してください。<br />
BatchApexUpdateAccount xbatch = new BatchApexUpdateAccount();<br />
Id batchId = Database.executeBatch(xbatch);</div></div>
<h3>スケジュールクラス（ScheduledBatchUpdateAccount.cls）</h3>
<pre class=""><code class="language-apex code_break">/**
 * プログラム名：ScheduledBatchUpdateAccount
 * 概　　　要　：取引先部門一括更新バッチのスケジューラ
 * 作　成　日　：2022/07.01
 * 作　成　者　：cclt(David)
 * 変更履歴　　：
 */
global class ScheduledBatchUpdateAccount implements Schedulable {
    global void execute(SchedulableContext sc) {
        //取引先部門一括更新バッチクラスをインスタンス化
        BatchApexUpdateAccount b = new BatchApexUpdateAccount(); 
        //execcuteBatchでバッチ処理を実行
        Database.executeBatch(b);
    }
}
</code></pre>
<p>Schedulableとすることで、以下の画面からスケジュールジョブとして設定することができるようになります。</p>
<p><a href="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール.jpg"><img decoding="async" class="alignnone size-full wp-image-3669" src="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール.jpg" alt="" width="1920" height="1175" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール.jpg 1920w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール-300x184.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール-1024x627.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール-768x470.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-取引先の部門を一括更新ジョブをスケジュール-1536x940.jpg 1536w" sizes="(max-width: 1920px) 100vw, 1920px" /></a></p>
<p>登録するとスケジュール済みジョブの画面にて、次回の実行予定を確認することができます。</p>
<p><a href="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ.jpg"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-3667" src="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ.jpg" alt="" width="1920" height="1038" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ.jpg 1920w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ-300x162.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ-1024x554.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ-768x415.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2022/07/apex-batch-update-account-sample-code-スケジュール済みジョブ-1536x830.jpg 1536w" sizes="auto, (max-width: 1920px) 100vw, 1920px" /></a></p>
<div class="cclt-compat-mybox st-mybox-class" style="background-color:#ffebee;border:2px solid #ef9a9a;border-radius:5px;margin:25px 0 25px 0;"><div class="cclt-compat-mybox__title" style="color:#ef5350;">注意ポイント</div><div class="cclt-compat-mybox__content"></p>
<ul>
<li>スケジュール登録の際に、作成したスケジューラのバッチが選択できない場合があります。その場合には、一度開発コンソールで直接スケジュール設定を行うと表示されるようになります。</li>
<li><span class="hutoaka">スケジュール済み Apex ジョブは一度に 100 件しか設定できません。（分単位でバッチを登録すると1つのバッチだけで60個のジョブ枠が使われてしまうため注意が必要）※サポートに問い合わせしても変更できません。</span></li>
<li>スケジュール済み Apex の 24 時間あたりの最大実行数も制限されているため、ジョブの実行回数についても注意が必要です。</li>
</ul>
<p></div></div>
<div class="cclt-compat-cmemo" style="background-color:#fafafa;color:#000000;border:px solid ;"><span class="cclt-compat-cmemo__icon fa fa-file-text-o" style="color:#919191;font-size:150;"></span><div class="cclt-compat-cmemo__text"></p>
<p>//開発コンソールで直接スケジュールを設定する</p>
<p>ScheduledBatchUpdateAccount m = new ScheduledBatchUpdateAccount();<br />
String sch = &#8217;20 30 8 10 2 ?&#8217;;<br />
String jobID = system.schedule(&#8216;取引先部門一括更新ジョブ&#8217;, sch, m);</p>
<p></div></div>
<p>sch（スケジュール）に設定する内容については、</p>
<p>&#8216;<var class="keyword varname">Seconds（秒） Minutes（分） Hours（時間） Day_of_month（日） Month（月）Day_of_week（曜日）</var>&#8216;の順</p>
<p>設定方法に関する説明は以下の通り</p>
<table style="border-collapse: collapse; width: 100%;">
<tbody>
<tr>
<th id="d120169e351" class="featureTableHeader vertical-align-top " style="width: 26.6945%; background-color: #4225e8;"><span style="color: #ffffff;">Name</span></th>
<th id="d120169e354" class="featureTableHeader vertical-align-top " style="width: 39.6246%; background-color: #4225e8;"><span style="color: #ffffff;">Values</span></th>
<th id="d120169e357" class="featureTableHeader vertical-align-top " style="width: 33.5766%; background-color: #4225e8;"><span style="color: #ffffff;">Special Characters</span></th>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><var class="keyword varname">Seconds</var></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values">0–59</td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters">None</td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><var class="keyword varname">Minutes</var></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values">0–59</td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters">None</td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><var class="keyword varname">Hours</var></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values"><span style="font-family: 'andale mono', monospace;">0–23</span></td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">, &#8211; * /</samp></span></td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><span style="font-family: 'andale mono', monospace;"><var class="keyword varname">Day_of_month</var></span></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values"><span style="font-family: 'andale mono', monospace;">1–31</span></td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">, &#8211; * ? / L W</samp></span></td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><span style="font-family: 'andale mono', monospace;"><var class="keyword varname">Month</var></span></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values"><span style="font-family: 'andale mono', monospace;">1–12 or the following:</span></p>
<ul class="ul bulletList">
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">JAN</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">FEB</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">MAR</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">APR</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">MAY</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">JUN</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">JUL</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">AUG</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">SEP</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">OCT</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">NOV</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">DEC</samp></span></li>
</ul>
</td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">, &#8211; * /</samp></span></td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><span style="font-family: 'andale mono', monospace;"><var class="keyword varname">Day_of_week</var></span></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values"><span style="font-family: 'andale mono', monospace;">1–7 or the following:</span></p>
<ul class="ul bulletList">
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">SUN</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">MON</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">TUE</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">WED</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">THU</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">FRI</samp></span></li>
<li class="li"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">SAT</samp></span></li>
</ul>
</td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">, &#8211; * ? / L #</samp></span></td>
</tr>
<tr>
<td class="entry" style="width: 26.6945%;" headers="d120169e351" data-title="Name"><span style="font-family: 'andale mono', monospace;"><var class="keyword varname">optional_year</var></span></td>
<td class="entry" style="width: 39.6246%;" headers="d120169e354" data-title="Values"><span style="font-family: 'andale mono', monospace;">null or 1970–2099</span></td>
<td class="entry" style="width: 33.5766%;" headers="d120169e357" data-title="Special Characters"><span style="font-family: 'andale mono', monospace;"><samp class="codeph apex_code">, &#8211; * /</samp></span></td>
</tr>
</tbody>
</table>
<p>【特殊なスケジュール設定方法】</p>
<p>・Special Charactersの文字列を使うことで、複雑なスケジュールの指定が可能となります。</p>
<p>①カンマ&#8217;,&#8217;を使った場合、月のパラメータに&#8221;JAN,FEB,MAR”といったように複数の月を指定することができます。</p>
<p>②半角ハイフン&#8217;-&#8216;を使うと、月のパラメータの場合、&#8221;JAN-MAR&#8221;といったように月のFROM-TO指定が可能となります。</p>
<p>※そのほかの例は、以下公式HELPサイトよりご確認ください。</p>
<p>「https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm」</p>
<h3>Batch Apexテストクラス（）</h3>
<pre class=""><code class="language-apex code_break">@isTest
private class ScheduledBatchUpdateAccountTest {

    public static String CRON_EXP = '0 0 * * * ? 2022';
    static testmethod void testScheduledJob() {
        // 処理対象の取引先データを作成
        List&lt;Account&gt; accRecs = new List&lt;Account&gt;();
        for (Integer i=0; i&lt;10; i++) {
            Account acc = new Account(
                Name = 'テスト取引先 ' + i,
                Site =  null);
            accRecs.add(acc);
        }
        insert accRecs;
        
        // インサート後のレコードIDとレコードをマップに格納（検証用）
        Map&lt;Id, Account&gt; accMap = new Map&lt;Id, Account&gt;(accRecs);

        // テスト開始
        Test.startTest();
            // スケジュールジョブ実行
            String jobId = System.schedule('ScheduledBatchUpdateAccountTest',
                CRON_EXP, new ScheduledBatchUpdateAccount());
                
        //テスト終了
        Test.stopTest();

        //テスト結果の検証
        for(Account acc:[SELECT Id,Name,Site FROM Account
                        WHERE Id IN :accMap.keyset()]){
            System.assertEquals(acc.Site , null);
        }
    }
}
</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cclt-compat-mybox st-mybox-class" style="background-color:#ffebee;border:2px solid #ef9a9a;border-radius:5px;margin:25px 0 25px 0;"><div class="cclt-compat-mybox__title" style="color:#ef5350;">注意ポイント</div><div class="cclt-compat-mybox__content"></p>
<ul>
<li>System.schedule メソッドを使って、スケジュールした結果を確認する場合には、Test.startTest()→System.schedule メソッド→Test. stopTest()の順に実行する必要があります。</li>
</ul>
<p></div></div>
<p>サンプルコード一覧へ戻る場合は、こちらへ</p>
<div class="cclt-compat-card st-no-shadow cclt-compat-card--text"><span class="cclt-compat-card__label" style="background-color:#cccccc;color:#ffffff;">参考</span><a href="https://creativecontentlabtokyo.com/programming-code-template-sample-list/" class="cclt-compat-card__link"><div class="cclt-compat-card__thumb"><img decoding="async" width="300" height="169" src="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg" class="attachment-medium size-medium wp-post-image" alt="" loading="lazy" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1024x576.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-768x432.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1536x864.jpg 1536w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-2048x1152.jpg 2048w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3.jpg 1920w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><div class="cclt-compat-card__body"><div class="cclt-compat-card__title">サンプルコード（テンプレート）Apex/Lwc/Visualforce/Flow</div><div class="cclt-compat-card__excerpt">Salesforceの開発を行うときにベースとなるサンプルコードを探すことが多い…</div><span class="cclt-compat-card__readmore">続きを読む</span></div></a></div>
		<div class="wpulike wpulike-animated-heart " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="3662"
					data-ulike-nonce="804434ec09"
					data-ulike-type="post"
					data-ulike-template="wpulike-animated-heart"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					data-ulike-append="&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop one&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop two&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop three&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop four&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop five&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop six&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop seven&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop eight&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop nine&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_3662"><svg class="wpulike-svg-heart wpulike-svg-heart-icon" viewBox="0 -28 512.00002 512" xmlns="http://www.w3.org/2000/svg"><path
						d="m471.382812 44.578125c-26.503906-28.746094-62.871093-44.578125-102.410156-44.578125-29.554687 0-56.621094 9.34375-80.449218 27.769531-12.023438 9.300781-22.917969 20.679688-32.523438 33.960938-9.601562-13.277344-20.5-24.660157-32.527344-33.960938-23.824218-18.425781-50.890625-27.769531-80.445312-27.769531-39.539063 0-75.910156 15.832031-102.414063 44.578125-26.1875 28.410156-40.613281 67.222656-40.613281 109.292969 0 43.300781 16.136719 82.9375 50.78125 124.742187 30.992188 37.394531 75.535156 75.355469 127.117188 119.3125 17.613281 15.011719 37.578124 32.027344 58.308593 50.152344 5.476563 4.796875 12.503907 7.4375 19.792969 7.4375 7.285156 0 14.316406-2.640625 19.785156-7.429687 20.730469-18.128907 40.707032-35.152344 58.328125-50.171876 51.574219-43.949218 96.117188-81.90625 127.109375-119.304687 34.644532-41.800781 50.777344-81.4375 50.777344-124.742187 0-42.066407-14.425781-80.878907-40.617188-109.289063zm0 0" /></svg></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="+1"></span>			</div></div>
	]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>カスタマーポータルユーザの作成（Apex Class サンプルコード）</title>
		<link>https://creativecontentlabtokyo.com/salesforce-how-to-setup-customer-portal-user-by-apex/</link>
		
		<dc:creator><![CDATA[セールスフォース標準化推進ラボ]]></dc:creator>
		<pubDate>Sat, 11 Jun 2022 07:35:28 +0000</pubDate>
				<category><![CDATA[サンプルコード]]></category>
		<category><![CDATA[Apex]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[カスタマーポータル]]></category>
		<category><![CDATA[カスタマー取引先]]></category>
		<category><![CDATA[デジタルエクスペリエンス]]></category>
		<category><![CDATA[ポータルユーザ]]></category>
		<category><![CDATA[作成]]></category>
		<guid isPermaLink="false">https://creativecontentlabtokyo.com/?p=3440</guid>

					<description><![CDATA[今回のサンプルは、取引先責任者IDを使ってカスタマー取引先ポータルのユーザを作成するApexコードのサンプルをご紹介します。 主に以下のような用途で利用いただけ…]]></description>
										<content:encoded><![CDATA[<p>今回のサンプルは、取引先責任者IDを使ってカスタマー取引先ポータルのユーザを作成するApexコードのサンプルをご紹介します。</p>
<p>主に以下のような用途で利用いただけます。</p>
<p>・ポータルサイトを使って、お客様からのお問い合わせの管理を行ったり、お客様に向けてナレッジなどの情報を提供する</p>
<p>【必要なライセンス】</p>
<p>カスタマー取引先ポータル（カスタマーポータル）を利用するためには、カスタマーポータルユーザライセンス（※１）が必要となります。</p>
<p>※Experience Cloudユーザライセンス（外部ユーザラインセンスとして、Customer Community、Customer Community Plusのいずれか）</p>
<p>代理店などの自社パートナーに対しては、別のパートナーコミュニティラインセンス（Partner Community）が用意されているので注意</p>
<div class="cclt-compat-kaiwa cclt-compat-kaiwa--left"><div class="cclt-compat-kaiwa__icon"><img decoding="async" src="https://creativecontentlabtokyo.com/wp-content/uploads/2024/11/schooldays1.jpg" alt="サポーターさん" loading="lazy" /><span class="cclt-compat-kaiwa__name">サポーターさん</span></div><div class="cclt-compat-kaiwa__bubble" style="background-color:#f4f4e3;"><div class="cclt-compat-kaiwa__text">Developer Edition環境を使えば動作検証は可能です</div></div></div>
<p>Developer Edition（無料の開発環境）を持っていない方は、以下の記事を参考にSalesforce組織を取得してください。</p>
<div class="cclt-compat-card st-no-shadow cclt-compat-card--text"><span class="cclt-compat-card__label" style="background-color:#cccccc;color:#ffffff;">参考</span><a href="https://creativecontentlabtokyo.com/salesforce-basic1-3/" class="cclt-compat-card__link"><div class="cclt-compat-card__thumb"><img decoding="async" width="300" height="169" src="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-300x169.jpg" class="attachment-medium size-medium wp-post-image" alt="" loading="lazy" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-300x169.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-1024x576.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-768x432.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-1536x864.jpg 1536w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1-2048x1152.jpg 2048w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-study-1.jpg 1920w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><div class="cclt-compat-card__body"><div class="cclt-compat-card__title">セールスフォースの基礎Ⅰ（３）Developer組織の作成～ログインまで</div><div class="cclt-compat-card__excerpt">【第三回】セールスフォースの基礎講義 前回の記事では、インスタンスとリージョンの…</div><span class="cclt-compat-card__readmore">続きを読む</span></div></a></div>
<h2>カスタマーポータルユーザの作成（ApexTriggerサンプルコード）</h2>
<h3>ポータルユーザ作成処理</h3>
<pre class=""><code class="language-apex code_break">/**
 *　クラス名：CreateCommunityUser 
 *　処理概要：
 *　作成者：CreativeContentLabTokyo
 *　作成日：2022/06/10 
 * 　
*/
global class CreateCommunityUser{

    //カスタムExceptionの定義
    public class CustomException extends Exception {}
    
    /** 
     *　メソッド名：createCommunityUserByContactId
     *　処理概要：取引先責任者のIDを使って、コミュニティユーザを作成
     *　引数：取引先責任者ID
     *　戻り値：ユーザレコード
    **/
    WebService static User createCommunityUserByContactId(String contactId) {
    
      //取引先責任者のIDを使って、氏名とメールアドレスの情報を取得
      Contact contactRec = [SELECT FirstName,LastName,Email,AccountId 
                        FROM Contact WHERE Id =: contactId LIMIT 1];
                           
      //カスタマーコミュニティユーザのプロファイル名を指定してプロファイルIDを取得
      //Profile prof = [SELECT Id,Name FROM profile WHERE name='Customer Community User' LIMIT 1];
      //標準のCustomer Community Userの利用は推奨されないため、コピーしてカスタムプロファイルを生成しておく
      Profile prof = [SELECT Id,Name FROM profile WHERE 
                     name='Custom Customer Community User' LIMIT 1];
      
      //ユーザ名生成
      String strUserName = contactRec.Email + 'ccltPortail';

      //ニックネーム生成
      String strNikcname;
      if(contactRec.FirstName == null){
          strNikcname = contactRec.LastName + Math.random();
      } else {
          strNikcname = contactRec.FirstName + Math.random();
      }
      
      //Alias生成(姓名の文字列短い場合を考慮してパディングしておく)
      String strAlias = String.valueOf(contactRec.LastName.substring(0,1) 
                  + Math.random() ).leftPad(3,'0').substring(0,5);
      
      //カスタマーコミュニティユーザを作成 
      User newCommunityUser = new User(contactId = contactId, 
                          username = strUserName, 
                          firstname = contactRec.FirstName,
                          lastname = contactRec.LastName, 
                          email=contactRec.Email,
                          communityNickname = strNikcname,
                          alias = strAlias, 
                          profileid = prof.Id, 
                          emailencodingkey='ISO-2022-JP',
                          languagelocalekey='ja', 
                          localesidkey='ja_JP', 
                          timezonesidkey='Asia/Tokyo');                  

    try {
          insert newCommunityUser ;
          
    //例外発生時処理
    } catch(DMLException ex) {
        //その他例外処理
        Throw new CustomException('[Exception]'+ex.getMessage());
    }
    //正常終了の場合、作成したユーザレコードを返却
      return newCommunityUser ;

    }
}
</code></pre>
<h3>テストクラス（CreateCommunityUserTest.cls）</h3>
<pre class=""><code class="language-apex code_break">/**--------------------------------------------------------------------------
 * プログラム名:CreateCommunityUserTest
 * 概　要 ：CreateCommunityUserテストクラス 
 * 作成日 ：2022.06.10
 * 作成者 ：cclt
 * --------------------------------------------------------------------------**/
@isTest(SeeAllData=false)
private class CreateCommunityUserTest {

    // テストデータのセットアップ
    @testSetup
    static void initTest(){ 
        // 取引先の作成
        Account acc = new Account(Name='クリエイティブコンテンツラボトウキョウ');
        insert acc;
        // 取引先に紐づく取引先責任者を作成
        contact con = new Contact();
        con.FirstName ='David';
        con.LastName ='cclt';
        con.AccountId = acc.Id;
        con.Email='cclt.customerportaltest@gmail.com';
        insert con;
    }

    @isTest static void Test_Case_createCommunityUser() {   

        //取引先責任者IDを取得
        Contact con = [select id,FirstName,LastName,AccountId,Email 
                        From Contact limit 1];
        User portalUser;
        //テスト開始
        Test.startTest();
            //カスタマーコミュニティユーザ作成処理実行
            portalUser = CreateCommunityUser.createCommunityUserByContactId(con.Id);
        
            //テスト終了
        Test.stopTest();

        //検証
        System.assert(portalUser != null);
        System.assert(portalUser.Email.equals(con.Email));
        
    }   

    @isTest static void Test_Case_ContactNameNull() {   

        // 不正な取引先責任者を作成
        contact con = new Contact();
        con.FirstName ='';
        con.LastName ='cclt';
        insert con;
        User portalUser;
        //テスト開始
        Test.startTest();
            try {
                //カスタマーコミュニティユーザ作成処理実行
                portalUser = CreateCommunityUser.createCommunityUserByContactId(con.Id);             
            } catch (Exception ex) {
                //検証
                System.assert(ex.getMessage().contains('FIELD_INTEGRITY_EXCEPTION'));
            }
            //テスト終了
        Test.stopTest();
        
    }   
}
</code></pre>
<p>&nbsp;</p>
<p>サンプルコード一覧へ戻る場合は、こちらへ</p>
<div class="cclt-compat-card st-no-shadow cclt-compat-card--text"><span class="cclt-compat-card__label" style="background-color:#cccccc;color:#ffffff;">参考</span><a href="https://creativecontentlabtokyo.com/programming-code-template-sample-list/" class="cclt-compat-card__link"><div class="cclt-compat-card__thumb"><img decoding="async" width="300" height="169" src="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg" class="attachment-medium size-medium wp-post-image" alt="" loading="lazy" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1024x576.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-768x432.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1536x864.jpg 1536w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-2048x1152.jpg 2048w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3.jpg 1920w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><div class="cclt-compat-card__body"><div class="cclt-compat-card__title">サンプルコード（テンプレート）Apex/Lwc/Visualforce/Flow</div><div class="cclt-compat-card__excerpt">Salesforceの開発を行うときにベースとなるサンプルコードを探すことが多い…</div><span class="cclt-compat-card__readmore">続きを読む</span></div></a></div>
		<div class="wpulike wpulike-animated-heart " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="3440"
					data-ulike-nonce="4f4ceb03ea"
					data-ulike-type="post"
					data-ulike-template="wpulike-animated-heart"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					data-ulike-append="&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop one&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop two&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop three&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop four&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop five&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop six&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop seven&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop eight&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop nine&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_3440"><svg class="wpulike-svg-heart wpulike-svg-heart-icon" viewBox="0 -28 512.00002 512" xmlns="http://www.w3.org/2000/svg"><path
						d="m471.382812 44.578125c-26.503906-28.746094-62.871093-44.578125-102.410156-44.578125-29.554687 0-56.621094 9.34375-80.449218 27.769531-12.023438 9.300781-22.917969 20.679688-32.523438 33.960938-9.601562-13.277344-20.5-24.660157-32.527344-33.960938-23.824218-18.425781-50.890625-27.769531-80.445312-27.769531-39.539063 0-75.910156 15.832031-102.414063 44.578125-26.1875 28.410156-40.613281 67.222656-40.613281 109.292969 0 43.300781 16.136719 82.9375 50.78125 124.742187 30.992188 37.394531 75.535156 75.355469 127.117188 119.3125 17.613281 15.011719 37.578124 32.027344 58.308593 50.152344 5.476563 4.796875 12.503907 7.4375 19.792969 7.4375 7.285156 0 14.316406-2.640625 19.785156-7.429687 20.730469-18.128907 40.707032-35.152344 58.328125-50.171876 51.574219-43.949218 96.117188-81.90625 127.109375-119.304687 34.644532-41.800781 50.777344-81.4375 50.777344-124.742187 0-42.066407-14.425781-80.878907-40.617188-109.289063zm0 0" /></svg></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="+1"></span>			</div></div>
	]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Apexテストクラスのまとめ</title>
		<link>https://creativecontentlabtokyo.com/explain-about-apex-test-class-method/</link>
		
		<dc:creator><![CDATA[セールスフォース標準化推進ラボ]]></dc:creator>
		<pubDate>Sun, 09 Jan 2022 09:45:12 +0000</pubDate>
				<category><![CDATA[サンプルコード]]></category>
		<category><![CDATA[Apex]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[TestClass]]></category>
		<category><![CDATA[testSetup]]></category>
		<guid isPermaLink="false">https://creativecontentlabtokyo.com/?p=2256</guid>

					<description><![CDATA[本記事では、Apexのテストクラスに関する注意事項やサンプルコードなどを記載しています。 テストクラスの標準テンプレート テストクラスの標準テンプレート（サンプ…]]></description>
										<content:encoded><![CDATA[<p>本記事では、Apexのテストクラスに関する注意事項やサンプルコードなどを記載しています。</p>
<h2>テストクラスの標準テンプレート</h2>
<p>テストクラスの標準テンプレート（サンプル）</p>
<pre class=""><code class="language-apex code_break">/**--------------------------------------------------------------------------
* プログラム名 ：TestClassSample
* 概　要       ：xxxのテストクラス
* 作成日       ：yyyy.mm.dd
* 作成者       ：cclt
*--------------------------------------------------------------------------**/
@isTest(SeeAllData=false)
private class TestClassSample {
    
    static final String DEFAULT_STRING = 'テスト内固定文字列の定義';

    //testSetupアノテーションを使ってテストデータを生成
    @testSetup
    static void initTest(){
        TestDataFactory.createTestUsers('標準ユーザ');    
    }
    
    //テストメソッド
    //命名ルール（テストクラスのメソッド名_テストケースNO＿処理期待値）
    static void testClassMethodXX_TestCase1_GetUserInfo(){ 

        //テスト準備
        User runUser = TestDataFactory.createTestUsers('標準ユーザ'); 
    
        //テスト開始
        Test.startTest();
         
        //テスト実行ユーザを指定して実行する場合
        System.runAs(runUser){
            //test code
        }
         
        //テスト終了
        Test.stopTest();
         
        //テスト結果検証
        System.assert(true);
        System.assertEquals('xx' , 'xx');
    }
}
</code></pre>
<h2>テストデータ作成のユーティリティクラス（TestDataFactory.cls）サンプル</h2>
<p>テストで利用するユーザを作成する場合には、ユーザ名の命名に注意する必要があります。ご存じの通りSalesforceのユーザ名は全世界で一意の値とする必要があり、テストクラスでユーザを作成する場合も同様です。</p>
<p>毎回テストクラスを作成するたびに一意のユーザ名を命名するのは大変なので、ユーザ名を検証するようなプログラムでない限りは、ランダムに採番した値を利用する共通のテストクラスを用意しておくと便利です。</p>
<p>以下は、指定プロファイルのユーザを生成するメソッドを含むテストユーティリティクラスとなります。</p>
<pre class=""><code class="language-apex code_break">@isTest(SeeAllData=false)
public with sharing class TestDataFactory  {

    static final String DEFAULT_USER_PROFILE_NAME = '標準ユーザ';
    static final String DEFAULT_USER_DOMAIN_NAME = '@xxxtestorg.com';

    //プロファイル名を指定してランダムなユーザ名のUserレコードを作成
    public static User createTestUsers(String profileName) {    
    
    //ユーザプロファイル取得
    Profile profile = getProfile(profileName); 

    //AESキーを16進にして先頭8桁をuidとして定義
    Blob b = Crypto.GenerateAESKey(128);
    String hex = EncodingUtil.ConvertTohex(b);
    String uid = hex.SubString(0,8);

    User user = new User();
    user.Alias = uid;
    user.Email= uid + DEFAULT_USER_DOMAIN_NAME; 
    user.EmailEncodingKey='UTF-8';
    user.LastName=uid;
    user.LanguageLocaleKey='ja';
    user.LocaleSidKey='ja_JP';
    user.ProfileId = profile.Id;
    user.TimeZoneSidKey='Asia/Tokyo';
    user.UserName= uid + DEFAULT_USER_DOMAIN_NAME; 
    insert user;
    return user;
    }

    //プロファイルが未指定の場合デフォルトのプロファイルをセット
    public static Profile getProfile(String profileName){
        if( String.isBlank(profileName) ){
             profileName = DEFAULT_USER_PROFILE_NAME;
        }
        Profile profile = new Profile();
        profile = [ SELECT Id, Name FROM Profile WHERE Name = :profileName LIMIT 1];
        return profile;
    }
}
</code></pre>
<h4>実行結果</h4>
<p>以下のような簡単なテストクラスを作成して、testSetupからテスト用のユーザを作成した結果のデバッグログです。</p>
<pre class=""><code class="language-apex code_break">@isTest
private class MyTestClass {
    @testSetup
    static void initTest(){
        TestDataFactory.createTestUsers(null);
    }
}
</code></pre>
<p>以下、デバッグログ<br />
<div class="cclt-compat-mybox st-mybox-class" style="background-color:#ffffff;border:2px solid #ccc;border-radius:2px;margin:25px 0 25px 0;"><div class="cclt-compat-mybox__content"></p>
<p>|USER_DEBUG|[15]|DEBUG|:Blob:Blob[16]</p>
<p>|USER_DEBUG|[17]|DEBUG|:hex:feb46887a6b4124bf97aaea120ab8c9b</p>
<p>|USER_DEBUG|[19]|DEBUG|:uid:feb46887</p>
<p>|USER_DEBUG|[31]|DEBUG|:user:User:{Alias=feb46887, Email=feb46887@xxxtestorg.com, EmailEncodingKey=UTF-8, LastName=feb46887, LanguageLocaleKey=ja, LocaleSidKey=ja_JP, ProfileId=00e5h0000032ZSgAAM, TimeZoneSidKey=Asia/Tokyo, Username=feb46887@xxxtestorg.com, Id=0055h000004JDHEAA4}</p>
<p></div></div></p>
<h2>テストデータ作成時の注意事項</h2>
<h3>テストデータとして利用できる組織データ</h3>
<p>デフォルトでは、Apex テストメソッド (API バージョン 24.0 以降) は、標準オブジェクト、カスタムオブジェクト、カスタム設定データなどの既存の組織データにはアクセスできません。アクセスできるのは、テストメソッドが作成したデータのみです。ただし、組織またはメタデータオブジェクトの管理に使用する次のようなオブジェクトなどは、そのままテストでアクセスできます。</p>
<p>以下については、<span class="huto">テストデータを作成しなくてもテスト実行組織に登録されているデータを参照して利用することができます</span>。</p>
<ul id="TestDataAccessDef2" class="ul bulletList">
<li class="li">User</li>
<li class="li">Profile</li>
<li class="li">Organization</li>
<li class="li">CronTrigger</li>
<li class="li">RecordType</li>
<li class="li">ApexClass</li>
<li class="li">ApexTrigger</li>
<li class="li">ApexComponent</li>
<li class="li">ApexPage</li>
</ul>
<p>ただし組織のデータを利用するとテスト実行時の状況によってテスト結果に影響を及ぼしてしまうため、可能な限りテストデータはテストクラス、メソッド毎に用意することが推奨されています。</p>
<h2>テストクラスで利用可能な＠アノテーション</h2>
<p>テストクラスで利用可能なアノテーションについて説明します。</p>
<h3>@testSetup</h3>
<p>テスト設定メソッドであるtestSetupアノテーションを利用したメソッドは、テストの最初に実行されます。注意事項は以下の通り。</p>
<blockquote><p><span style="color: #333399;">テスト設定メソッドで作成されたレコードは、テストクラス内のすべてのテストメソッドで使用でき、テストクラス実行終了時にロールバックされます。レコード項目の更新やレコード削除など、テストメソッドがこれらのレコードを変更した場合、その変更は、各テストメソッドの実行終了後にロールバックされます。次に実行されるテストメソッドは、元の変更されていない状態のレコードにアクセスできます</span>。</p>
<p><span style="color: #333399;">テスト設定メソッドは、テストクラスのデフォルトのデータ分離モードでのみサポートされます。テストクラスまたはテストメソッドが <samp class="codeph apex_code"><span class="keyword">@isTest(SeeAllData=true)</span></samp> アノテーションを使用することで組織データにアクセスできる場合、そのクラスではテスト設定メソッドはサポートされません。テストのためのデータ分離を使用できるのは API バージョン 24.0 以降であるため、テスト設定メソッドを使用できるのもこれらのバージョンのみです</span>。</p>
<p>出典：Salesforce公式Apex開発者ガイド「<a href="https://developer.salesforce.com/docs/atlas.ja-jp.218.0.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm">TestSetup アノテーション</a>」より</p></blockquote>
<p>【構文例】</p>
<pre class=""><code class="language-apex code_break">@isTest
private class MyTestClass {
    @testSetup
    static void initTest(){
        TestDataFactory.createTestUsers(null);
    }
}</code></pre>
<h3>@TestVisible</h3>
<p>TestVisibleアノテーションを使用すると、テストクラスでないクラスのprivae非公開メンバーまたは保護メンバーにテストメソッドからアクセスすることができるようになります。注意事項は以下の通り。</p>
<p>【構文例】</p>
<pre class=""><code class="language-apex code_break">@isTest
public class TestVisibleMethodSample {
    // 非公開のメンバー変数
    @TestVisible private static String prefix = 'Prefix';

    // 非公開のメソッド
    @TestVisible 
    private static void updateAccount(String name) {
        //処理コード
    }
}</code></pre>
<p>（テストクラスからの呼び出し）</p>
<pre class=""><code class="language-apex code_break">@isTest
private class TestVisibleMethodTest {
    @isTest static void case_doUpdateAccount_fail() {

        // TestVisibleアノテーションのメンバーにアクセスする
        String prefix = TestVisibleMethodSample.prefix;
        System.assertEquals('Prefix', prefix);

        // TestVisibleアノテーションのメソッドにアクセスする
        TestVisibleMethodSample.updateAccount(prefix);
    }
}</code></pre>
<h2>テスト関連トピック</h2>
<h3>指定ユーザでテストを実行する</h3>
<p>System.runAs(uid)メソッドを利用することで、uidで指定されてユーザの権限でテストを実行することが可能。詳細は以下参照。</p>
<pre class=""><code class="language-apex code_break">//テスト実行ユーザを指定して実行する場合　※uidにユーザのIDを指定
System.runAs(uid){
   //test code
}
</code></pre>
<div class="cclt-compat-cmemo" style="background-color:#fafafa;color:#757575;border:px solid ;"><span class="cclt-compat-cmemo__icon fa fa-file-text-o" style="color:#BDBDBD;"></span><div class="cclt-compat-cmemo__text">Salesforce公式　Apex開発者ガイド「<a href="https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_testing_tools_runas.htm">runAs メソッドの使用</a>」</div></div>
		<div class="wpulike wpulike-animated-heart " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="2256"
					data-ulike-nonce="7852a3dea0"
					data-ulike-type="post"
					data-ulike-template="wpulike-animated-heart"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					data-ulike-append="&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop one&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop two&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop three&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop four&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop five&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop six&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop seven&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop eight&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop nine&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_2256"><svg class="wpulike-svg-heart wpulike-svg-heart-icon" viewBox="0 -28 512.00002 512" xmlns="http://www.w3.org/2000/svg"><path
						d="m471.382812 44.578125c-26.503906-28.746094-62.871093-44.578125-102.410156-44.578125-29.554687 0-56.621094 9.34375-80.449218 27.769531-12.023438 9.300781-22.917969 20.679688-32.523438 33.960938-9.601562-13.277344-20.5-24.660157-32.527344-33.960938-23.824218-18.425781-50.890625-27.769531-80.445312-27.769531-39.539063 0-75.910156 15.832031-102.414063 44.578125-26.1875 28.410156-40.613281 67.222656-40.613281 109.292969 0 43.300781 16.136719 82.9375 50.78125 124.742187 30.992188 37.394531 75.535156 75.355469 127.117188 119.3125 17.613281 15.011719 37.578124 32.027344 58.308593 50.152344 5.476563 4.796875 12.503907 7.4375 19.792969 7.4375 7.285156 0 14.316406-2.640625 19.785156-7.429687 20.730469-18.128907 40.707032-35.152344 58.328125-50.171876 51.574219-43.949218 96.117188-81.90625 127.109375-119.304687 34.644532-41.800781 50.777344-81.4375 50.777344-124.742187 0-42.066407-14.425781-80.878907-40.617188-109.289063zm0 0" /></svg></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Apex Trigger サンプルコード</title>
		<link>https://creativecontentlabtokyo.com/apex-trigger-sample-code-account/</link>
		
		<dc:creator><![CDATA[セールスフォース標準化推進ラボ]]></dc:creator>
		<pubDate>Sun, 21 Nov 2021 09:27:29 +0000</pubDate>
				<category><![CDATA[サンプルコード]]></category>
		<category><![CDATA[Apex]]></category>
		<category><![CDATA[Apex Trigger]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[テスト]]></category>
		<guid isPermaLink="false">https://creativecontentlabtokyo.com/?p=1301</guid>

					<description><![CDATA[取引先オブジェクトのApexTriggerサンプルコード 取引先（Account）のトリガクラス、ハンドラクラス、テストクラスの基本セット 取引先トリガ（Acc…]]></description>
										<content:encoded><![CDATA[<h2>取引先オブジェクトのApexTriggerサンプルコード</h2>
<p>取引先（Account）のトリガクラス、ハンドラクラス、テストクラスの基本セット</p>
<h3>取引先トリガ（AccountTrigger.cls）</h3>
<pre class=""><code class="language-apex code_break">Trigger AccountTrigger on Account (before insert, after insert, before update, after update,before delete, after delete, after undelete) {

    //Handlerクラス 
    AccountTriggerHandler handler = new AccountTriggerHandler();

    //レコードが新規登録された時
    if (Trigger.isInsert) {
        if (Trigger.isBefore) {
            // before insertの処理
            handler.onBeforeInsert(Trigger.New);
        } else if (Trigger.isAfter) {
            // after insertの処理
            handler.onAfterInsert(Trigger.New);
        }
    //レコードが更新された時
    } else if (Trigger.isUpdate) {
        if (Trigger.isBefore) {
            // before updateの処理
            handler.onBeforeUpdate(Trigger.Old, Trigger.New);
        } else if (Trigger.isAfter) {
            // after updateの処理
            handler.onAfterUpdate(Trigger.Old, Trigger.New);
        }
    //レコードが削除された時
    } else if (Trigger.isDelete) {
        if (Trigger.isBefore) {
            // before deleteの処理
            handler.onBeforeDelete(Trigger.OldMap, Trigger.NewMap);
        } else if (Trigger.isAfter) {
            // after deleteの処理
            handler.onAfterDelete(Trigger.OldMap, Trigger.NewMap);
        }
    //レコードがごみ箱から復元された時
    } else if (Trigger.isUndelete) {
        if (Trigger.isAfter) {
            // after undeleteの処理
            handler.onAfterUnDelete(Trigger.New);
        }
    }
}
</code></pre>
<h3>トリガハンドラ（AccountTriggerHandler.cls）</h3>
<pre class=""><code class="language-apex code_break">/**--------------------------------------------------------------------------
* プログラム名 ：AccountTriggerHandler
* 概　要       ：Accountトリガーのハンドラークラス
* 作成日       ：yyyy.mm.dd
* 作成者       ：cclt
*--------------------------------------------------------------------------**/
public without sharing class AccountTriggerHandler { 

    /** * before insert */ 
    public void onBeforeInsert(List&lt;Account&gt; newAccList){ 
        for( Account acc : newAccList) {         
            //処理
        } 
    } 
    
    /** * after insert */ 
    public void onAfterInsert(List&lt;Account&gt; newAccList){ 
        for( Account acc : newAccList) {         
            //処理
        } 
    } 
    
    /** * before update */ 
    public void onBeforeUpdate(List&lt;Account&gt; oldAccList,List&lt;Account&gt; newAccList){  
        for( Account acc : newAccList) { 
            //処理
        } 
    } 
    
    /** * after update */ 
    public void onAfterUpdate(List&lt;Account&gt; oldAccList,List&lt;Account&gt; newAccList){  
        for( Account acc : newAccList) { 
            //処理
        } 
    } 
    
    /** * before delete */ 
    public void onBeforeDelete(Map&lt;Id,Account&gt; oldAccMap,Map&lt;Id,Account&gt; newAccMap){  
        for( Account acc : oldAccMap.values()) { 
            //処理
        } 
    } 
    
    /** * after delete */ 
    public void onAfterDelete(Map&lt;Id,Account&gt; oldAccMap,Map&lt;Id,Account&gt; newAccMap){  
        for( Account acc : oldAccMap.values()) { 
            //処理
        } 
    }     
    
    /** * after undelete */ 
    public void onAfterUnDelete(List&lt;Account&gt; newAccList){  
        for( Account acc : newAccList) { 
            //処理
        } 
    }    
}
</code></pre>
<h3>取引先トリガテストクラス（TestAccountTrigger.cls）</h3>
<pre class=""><code class="language-apex code_break">/**--------------------------------------------------------------------------
* プログラム名 ：TestAccountTrigger
* 概　要       ：Accountトリガーのテストクラス
* 作成日       ：yyyy.mm.dd
* 作成者       ：cclt
*--------------------------------------------------------------------------**/
@isTest(SeeAllData=false)
private class TestAccountTrigger {
    
    //テスト設定メソッド
    @testSetup
    static void initTest(){
        // テストデータのセットアップ
        // 取引先の作成後、取引先に紐づく、責任者と商談を作成
        Account acc = new Account(Name='クリエイティブコンテンツラボトウキョウ');
        insert acc;
        
        // 取引先に紐づく取引先責任者を作成
        contact con = new Contact(FirstName='デイビッド',LastName='cclt');
        insert con;
        
        // 取引先に紐づく商談を作成
        List&lt;Opportunity&gt; oppList = new List&lt;Opportunity&gt;();
        oppList.add(new Opportunity(Name=acc.Name + '様との新規商談について',
                                    StageName='Prospecting',CloseDate=System.today().addMonths(1), AccountId=acc.Id));
        oppList.add(new Opportunity(Name=acc.Name + '様との別商談について',
                                    StageName='Prospecting',CloseDate=System.today().addMonths(3),AccountId=acc.Id));
        insert oppList; 
    }
    
    // テストケース：取引先名が更新されたときに商談の名前が更新されるか
    @isTest static void testUpdateAccountName() {      
        
        // 取引先の名前を更新するため最新のレコードを取得
         List&lt;Account&gt; updAccList = [select id, Name from Account];
        updAccList[0].Name = 'CreativeContentLabTokyo';        
        
        // テスト実行
        Test.startTest();

        //トリガー起動のための処理を記述
        Database.SaveResult result = Database.update(updAccList[0], false);

        Test.stopTest();

        // テスト結果の検証 
        System.assert(result.isSuccess());
        System.assert(result.getErrors().size() == 0);

    }
    
    // テストケース：取引先を削除
    @isTest static void testDeleteAccountRecord() {      
        
        // 取引先の名前を更新するため最新のレコードを取得
        List&lt;Account&gt; delAccList = [select id, Name from Account];
        
        // テスト実行
        Test.startTest();

        //トリガー起動のための処理を記述
        delete delAccList[0];

        Test.stopTest();
        
        List&lt;Account&gt; resultAcc = [select id, Name from Account];

        // テスト結果の検証 
        System.assert(resultAcc.isEmpty());

    }   
}
</code></pre>
<p>&nbsp;</p>
<p>サンプルコード一覧へ戻る場合は、こちらへ</p>
<div class="cclt-compat-card st-no-shadow cclt-compat-card--text"><span class="cclt-compat-card__label" style="background-color:#cccccc;color:#ffffff;">参考</span><a href="https://creativecontentlabtokyo.com/programming-code-template-sample-list/" class="cclt-compat-card__link"><div class="cclt-compat-card__thumb"><img decoding="async" width="300" height="169" src="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg" class="attachment-medium size-medium wp-post-image" alt="" loading="lazy" srcset="https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-300x169.jpg 300w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1024x576.jpg 1024w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-768x432.jpg 768w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-1536x864.jpg 1536w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3-2048x1152.jpg 2048w, https://creativecontentlabtokyo.com/wp-content/uploads/2021/11/eye-catching-picture-pc-3.jpg 1920w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div><div class="cclt-compat-card__body"><div class="cclt-compat-card__title">サンプルコード（テンプレート）Apex/Lwc/Visualforce/Flow</div><div class="cclt-compat-card__excerpt">Salesforceの開発を行うときにベースとなるサンプルコードを探すことが多い…</div><span class="cclt-compat-card__readmore">続きを読む</span></div></a></div>
		<div class="wpulike wpulike-animated-heart " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="1301"
					data-ulike-nonce="bb0a0cc793"
					data-ulike-type="post"
					data-ulike-template="wpulike-animated-heart"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					data-ulike-append="&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop one&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop two&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop three&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop four&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop five&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop six&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop seven&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop eight&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;&lt;svg class=&quot;wpulike-svg-heart wpulike-svg-heart-pop nine&quot; viewBox=&quot;0 0 32 29.6&quot;&gt;&lt;path d=&quot;M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z&quot;/&gt;&lt;/svg&gt;"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_1301"><svg class="wpulike-svg-heart wpulike-svg-heart-icon" viewBox="0 -28 512.00002 512" xmlns="http://www.w3.org/2000/svg"><path
						d="m471.382812 44.578125c-26.503906-28.746094-62.871093-44.578125-102.410156-44.578125-29.554687 0-56.621094 9.34375-80.449218 27.769531-12.023438 9.300781-22.917969 20.679688-32.523438 33.960938-9.601562-13.277344-20.5-24.660157-32.527344-33.960938-23.824218-18.425781-50.890625-27.769531-80.445312-27.769531-39.539063 0-75.910156 15.832031-102.414063 44.578125-26.1875 28.410156-40.613281 67.222656-40.613281 109.292969 0 43.300781 16.136719 82.9375 50.78125 124.742187 30.992188 37.394531 75.535156 75.355469 127.117188 119.3125 17.613281 15.011719 37.578124 32.027344 58.308593 50.152344 5.476563 4.796875 12.503907 7.4375 19.792969 7.4375 7.285156 0 14.316406-2.640625 19.785156-7.429687 20.730469-18.128907 40.707032-35.152344 58.328125-50.171876 51.574219-43.949218 96.117188-81.90625 127.109375-119.304687 34.644532-41.800781 50.777344-81.4375 50.777344-124.742187 0-42.066407-14.425781-80.878907-40.617188-109.289063zm0 0" /></svg></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="+1"></span>			</div></div>
	]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
