Apex & .NET Basics 02

Jaehyun_Ban·2022년 4월 11일
0

실행 컨텍스트 이해

트리거 = 필터같은 존재?

Trigger Essentials

before insert
before update
before delete
after insert
after update
after delete
after undeleteys

대량 작업

Apex Trigger는 한번에 최대 200갸의 레코드를 수신 SOQL쿼리 수에 대한 동기 제한은 100개이고 발행된 총 DML문 수에 대해 150개이다.

📌 대량 트리거 참조


Challenge

Account Trigger

trigger AccountTrigger on Account (before insert) { // before records are inserted
    if (Trigger.isBefore && Trigger.isInsert) { //Insert되거나 before일 때
        AccountTriggerHandler.CreateAccounts(Trigger.New);//새로운 레코드를 생성
    }
}

AccountTriggerHandler

public class AccountTriggerHandler {
    //The method must accept a List of Account objects
    //이 메서드는 List<account>를 받는다
    public static void CreateAccounts(LIST<Account> acctList){
        //ShippingState와 BillingState를 같은 상태로 만들어 놓기
        for(Account acc: acctList){
            if(acc.ShippingState != acc.BillingState){
                acc.ShippingState = acc.BillingState;
            }
        }        
    }
}

AccountTriggerTest

@isTest
public class AccountTriggerTest {
    // 200개의 Account records를 만들기 위한 test method
    @isTest static void TestInsertRecords(){
        LIST<Account> acctList = new LIST<Account>();
        
        for(Integer i = 0; i < 200; i++){
            Account accounts = new Account(Name='Test'+i, BillingState='CA');
            acctList.add(accounts);
        }
        
        Test.startTest();//테스트 실행
        insert acctList; //200개 insert
        Test.stopTest(); //stop
        
        List<Account> TestAccount=[SELECT ShippingState FROM Account];
        for(Account acc: TestAccount){
            System.assertNotEquals('CA', acc.ShippingState, 'Incorrect');//(예상값, 실제값, 메세지)
        }
    }
}

0개의 댓글