高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
global class NFM620RestBatch implements Database.Batchable<sObject> {
    public String query;
 
    global NFM620RestBatch() {
        //this.query = query;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //查找商品询问单中统一用户ID不为空且科室名、战略科室名、客户姓名均为空的数据
        return Database.getQueryLocator([select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ContactId__c != null and Hospital_Name__c = null and Department_Class__c = null and Contact_Name__c = null]);
    }
 
    global void execute(Database.BatchableContext BC, list<Inquiry_form__c> scope) {
        List<Inquiry_form__c> inquiryFormList = new List<Inquiry_form__c>();
        List <String> sobjectList = new List <String> ();
        for (Sobject sobj: scope) {
            sobjectList.add(sobj.Id);
        }
        
        List<Inquiry_form__c> inquiryList = [select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ID in:sobjectList];
        //统一用户ID存入List、询问单ID存Map
        Map<String, String> inquiryMap = new Map<String, String>();
        List<String> contactIdList = new List<String>();
        for (Inquiry_form__c inquiryInfo : inquiryList) {
            contactIdList.add(inquiryInfo.ContactId__c);
            inquiryMap.put(inquiryInfo.ContactId__c, inquiryInfo.Id);
        }
        //用商品询问单的统一用户ID去联系人表中查找科室名、战略科室分类、客户姓名、战略科室所有人
        List<Contact> conList = [select UnifiedI_Contact_ID__c,Account.Id, Strategic_dept_Class__c, Strategic_dept_Class__r.OwnerId,Id  from Contact where UnifiedI_Contact_ID__c in: contactIdList];
        if (conList.size() >0 ) {
            for (Contact contactInfo : conList) {
                Inquiry_form__c inquiry = new Inquiry_form__c();
                inquiry.Id = inquiryMap.get(contactInfo.UnifiedI_Contact_ID__c);      //ID
                inquiry.Hospital_Name__c = contactInfo.Account.Id;                    //科室名   
                inquiry.Department_Class__c = contactInfo.Strategic_dept_Class__c;    //战略科室分类
                inquiry.Contact_Name__c = contactInfo.Id;                             //客户姓名
                inquiry.OwnerId = contactInfo.Strategic_dept_Class__r.OwnerId;        //所有人
                inquiryFormList.add(inquiry);
            }
        }
 
        if (inquiryFormList.size() > 0) {
            update inquiryFormList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}