Li Jun
2022-04-24 dd10019315aa7e0b26432bb48716da308cc11d82
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Author: Bubba Li
 * Created Date: 04/22/2022
 * Purpose: sync agency contact to aws
 * Test Class: SyncAccountContactToAWS_Test
 * History: 
 *         04/22/2022 - Bubba Li - Initial Code.
 * 
 * */
global class  SyncAccountContactToAWS implements Schedulable,Database.Batchable<SObject>,Database.AllowsCallouts{
 
    global String query;
    
    global SyncAccountContactToAWS(String query) {
        this.query = query;
    }
    global SyncAccountContactToAWS() {
        this.query = 'SELECT id,Doctor_Division1__c,Doctor_Division1_Encrypted__c,Name,Name_Encrypted__c,Type__c,Type_Encrypted__c,AWS_Data_Id__c,Contact__c, Contact__r.Doctor_Division1_Encrypted__c,Contact__r.LastName_Encrypted__c, Contact__r.Type_Encrypted__c FROM Agency_Contact__c WHERE AWS_Data_Id__c =\'\' And Contact__c != null order by lastmodifieddate desc';
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('Query by custom soql:'+this.query);
        return Database.getQueryLocator(this.query);
    }
    global void execute(Database.BatchableContext BC, list<Agency_Contact__c> scope) {
        if(scope!=null && scope.size()>0){
            for(Agency_Contact__c  aContact:scope){
                aContact.Doctor_Division1_Encrypted__c = aContact.Contact__c!=null&&aContact.Contact__r.Doctor_Division1_Encrypted__c!=''?aContact.Contact__r.Doctor_Division1_Encrypted__c:'';
                aContact.Name_Encrypted__c =  aContact.Contact__c!=null&&aContact.Contact__r.LastName_Encrypted__c!=''?aContact.Contact__r.LastName_Encrypted__c:'';
                aContact.Type_Encrypted__c =  aContact.Contact__c!=null&&aContact.Contact__r.Type_Encrypted__c!=''?aContact.Contact__r.Type_Encrypted__c:'';
                aContact.AWS_Data_Id__c = '';
            }
            system.debug('Agency Contact Info to AWS:'+JSON.serialize(scope));
            AWSServiceTool2.EncryptPushCore(JSON.serialize(scope),'Agency_Contact__c');
        }      
    }
 
    global void execute(SchedulableContext SC) {
        Id execBTId = Database.executeBatch(new SyncAccountContactToAWS(), 1);
    }
 
    public static void assignOnceOneMinuteLater() {
        String hour = String.valueOf(Datetime.now().hour());
        String min = String.valueOf(Datetime.now().minute() + 1); 
        String ss = String.valueOf(Datetime.now().second());
        //parse to cron expression
        String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
        SyncAccountContactToAWS s = new SyncAccountContactToAWS(); 
        System.schedule('Job Started At ' + String.valueOf(Datetime.now()), nextFireTime, s);
    }
 
    global void finish(Database.BatchableContext BC) {
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
        //then use the active job id and abort it
        system.abortJob(a.id);
    }
}