0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EmployeeTriggerHandler

Last updated at Posted at 2019-11-23
public class EmployeeTriggerHandler extends TriggerHandler {
    public override void BeforeInsert(List<SObject> newList) {
        // 対象データを取得する
        List<Employee__c> employeeList = (List<Employee__c>)newList;
        
        // アドレス設定
        addressSetting(employeeList);
    }

    public override void BeforeUpdate(Map<Id, SObject> oldMap, Map<Id, SObject> newMap) {
        // 対象データを取得する
        List<Employee__c> employeeList = (List<Employee__c>)newMap.values();
        
        // アドレス設定
        addressSetting(employeeList);
    }

    private void addressSetting(List<Employee__c> employeeList) {
        // 関連の住所コードを取得する
        Set<String> addressCDSet = new Set<String>();
        for (Employee__c item : employeeList){
            If (!String.IsBlank(item.AddressCD__c)) {
                addressCDSet.add(item.AddressCD__c);
            }
        }
        
        // 住所マスタから住所情報を取得する
        List<AddressMaster__c> addressMasterList = [select id,Name, Address__c from AddressMaster__c where Name IN :addressCDSet]; 

        // 住所コードマップ作成(マップ<住所コード,住所詳細>)
        Map<String, String> mapAddress = new Map<String, String>();
        for (AddressMaster__c item : addressMasterList) {
            mapAddress.put(item.name, item.Address__c);
        }

        // 社員マスタの住所1を設定する
        for (Employee__c item : employeeList){
            if (!String.IsBlank(item.AddressCD__c)) {
                if (mapAddress.containsKey(item.AddressCD__c)) {
                    item.Address1__c = mapAddress.get(item.AddressCD__c);
                } else {
                    item.Address1__c.adderror('関連の住所コードが存在しません。');
              
                }
            }
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?