global class NFM623RestBatch implements Database.Batchable { 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 scope) { List campaignMemberList = new List(); List sobjectList = new List (); for (Sobject sobj: scope) { sobjectList.add(sobj.Id); } List capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ID in:sobjectList]; Map capMemMap = new Map(); //统一用户ID存入List,参会人员记录ID存入MAP List contactIdList = new List(); for (CampaignMember__c capMemInfo : capMemList) { contactIdList.add(capMemInfo.ViewContactId__c); capMemMap.put(capMemInfo.ViewContactId__c,capMemInfo.Id); } List 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) { } }