Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

forest_moon

Apex Lead Batch (1) 본문

Saelsforcee

Apex Lead Batch (1)

rokga 2023. 11. 20. 13:54

요구사항

Lead의 상태가 90일 이후에도 기회전환이 안된 상태라면 수요없음으로 변환.

Batch가 TrailHead 이후에는 처음이라 조금찾아보면서 했다. 

 

우선 batch를 크게보자면 start 에서 query를 가져오고, 가져온 query를 기반으로 excute에서 비즈니스로직을 통해 

일련의 처리를하고, finish 에서 추가작업을 한다(이메일, 알림 등등)

 

global class LeadStatusBatch implements Database.Batchable<sObject>, Database.stateful {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        System.debug(':::: Batch Start::::');

        String query = 'SELECT ';
               query += 'Id, ';
               query += 'Status, ';
               query += 'CreatedDate ';
               query += 'FROM Lead ';
               query += 'WHERE (Status = \'신규\' OR Status = \'수요검색 \') ';
               query += 'AND CreatedDate < LAST_N_DAYS: 90';
              
               System.debug(query);

        return Database.getQueryLocator(query);
    }

    
    global void execute(Database.BatchableContext bc, List<Lead> scope) {
        System.debug(':::: Batch execute ::::');
        System.debug('scope is' + scope );
        List<Lead> leadList = new List<Lead>();
        try {
            if(scope.size() > 0){
                for(Lead  ld : scope){
                    ld.Status = '수요없음';
                    leadList.add(ld);
                }
            update leadList;
            }
        } catch (Exception e)  {
            System.debug('exception : '+ e );
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        System.debug(':::: Batch Finish::::');
    }
}

 

start에서 요구조건에 해당하는 값을 찾기 위해서 where절에서 조건을 통해

변환x , 상태가 신규 or 수요검색 이고 생성일자가 90일 이내인 데이터를 찾아온다

 

excute에서는

scope.size() > 0  이라는 조건도 사실 필요없는데 혹시 몰라 검증을 한번 더 추가 했다

( start 에서 데이터가 없으면 배치가 실행되지 않음)

이후에 status 값을 변경하고 DML문을 통해 update 를 실행한다. 

 

finish는 batch작업만 실행하고 추가로 알림,이메일과 같은 이벤트는 요구사항이 없어 추가하지 않았다.

 

다음에는 batch를 등록했으니 스케쥴러를 통해서 자동으로 매일 업데이트를 할 예정

 

'Saelsforcee' 카테고리의 다른 글

세일즈포스 월드투어 후기  (0) 2024.06.21
Salesforce Standard label 변경  (0) 2023.12.04
Apex Lead Batch + Schedulable  (0) 2023.11.24
Apex Lead Batch (2) Testcode  (0) 2023.11.20
Salesforce Standard PDF Features  (0) 2023.10.24