【Salesforce】Apex でのドメインレイヤの原則の適用

スポンサーリンク

スポンサーリンク

Trailhead

Challengeの所も日本語化してほしいよね。。。

Implement a basic Domain class and Apex trigger

Implement a basic Domain class and accompanying Apex trigger with default and update logic based upon domain conventions.

Prework: If you haven’t deployed the ApexMocks and Apex Common open source libraries yet, do that now. Otherwise you won’t be able to complete this challenge.

  • Create a basic Domain class named Accounts that extends fflib_SObjectDomain.
  • Create a trigger named AccountsTrigger for Account that calls the fflib_SObjectDomain triggerHandler method for all trigger methods.
  • Implement defaulting logic that executes when a record is inserted and sets the Description field to the value Domain classes rock!
  • Implement update logic that calculates the Levenshtein distance between the phrase Domain classes rock! and whatever the contents of the Description field is when an Account is updated. Use the Apex String method getLevenshteinDistance(stringToCompare) and store the result in the Annual Revenue field.
https://trailhead.salesforce.com/ja/content/learn/modules/apex_patterns_dsl/apex_patterns_dsl_apply_dl_principles

サンプルソースはこちら。
このままコピペでも通ります。

public with sharing class Accounts extends fflib_SObjectDomain {

    public Accounts(List<Account> sObjectList) {
        super(sObjectList);
    }

    public class Constructor implements fflib_SObjectDomain.IConstructable {
        public fflib_SObjectDomain construct(List<SObject> sObjectList) {
            return new Accounts(sObjectList);
        }
    }

    public override void onApplyDefaults() {
        setDescriptionToRock();
    }

    public override void onBeforeUpdate(Map<Id, SObject> existingRecords) {
        calculateLevenshteinDistance();
    }

    public void setDescriptionToRock() {
        for (Account acc : (List<Account>) Records) {
            acc.Description = 'Domain classes rock!';
        }
    }

    public void calculateLevenshteinDistance() {
        for (Account acc : (List<Account>) Records) {
            if (acc.Description != null) {
                acc.AnnualRevenue = acc.Description.getLevenshteinDistance('Domain classes rock!');
            }
        }
    }
}
trigger AccountsTrigger on Account (before insert, before update) {
    fflib_SObjectDomain.triggerHandler(Accounts.class);
}

コメント

スポンサーリンク






スポンサーリンク





タイトルとURLをコピーしました