高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class NFM623RestBatch implements Database.Batchable<sObject> {
    public String query;
 
    global NFM623RestBatch() {
        //this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ViewContactId__c != null and Contact_ID__c = null]);
    }
 
    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List <String> sobjectList = new List <String> ();
        for (Sobject sobj: scope) {
            sobjectList.add(sobj.Id);
        }
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ID in:sobjectList];
        Map<String, String> capMemMap = new Map<String, String>(); 
        //统一用户ID存入List,参会人员记录ID存入MAP
        List<String> contactIdList = new List<String>();
        for (CampaignMember__c capMemInfo : capMemList) {
            contactIdList.add(capMemInfo.ViewContactId__c);
            capMemMap.put(capMemInfo.ViewContactId__c,capMemInfo.Id);
        }
        List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,AccountId,Account.Name from Contact where UnifiedI_Contact_ID__c in :contactIdList];
        if (contactList.size() > 0) {
            for (Contact contactInfo : contactList) {
                CampaignMember__c camMemberInfo = new CampaignMember__c();
                camMemberInfo.Id = capMemMap.get(contactInfo.UnifiedI_Contact_ID__c);
                camMemberInfo.Contact_ID__c = contactInfo.ID;                   //参会人员ID
                camMemberInfo.Department_ID__c = contactInfo.AccountId;         //医院科室
                camMemberInfo.Department__c = contactInfo.Account.Name;         //医院科室(文本)
                camMemberInfo.Contact__c = contactInfo.Name;                    //客户人员姓名
                campaignMemberList.add(camMemberInfo);
            }
            
        }
        if (campaignMemberList.size() > 0) {
            update campaignMemberList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}