高章伟
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public with sharing class CampaignMemberTriggerHandler extends Oly_TriggerHandler {
    private Map<Id, CampaignMember__c> newMap;
    private Map<Id, CampaignMember__c> oldMap;
    private List<CampaignMember__c> newList;
    private List<CampaignMember__c> oldList;
    public CampaignMemberTriggerHandler() {
        this.newMap = (Map<Id, CampaignMember__c>) Trigger.newMap;
        this.oldMap = (Map<Id, CampaignMember__c>) Trigger.oldMap;
        this.newList = (List<CampaignMember__c>) Trigger.new;
        this.oldList = (List<CampaignMember__c>) Trigger.old;
    }
 
    protected override void afterInsert() {
        updateCampaigntoContact();
    }
 
    protected override void afterUpdate() {
        updateCampaigntoContact();
    }
    protected override void BeforeDelete() {
 
        removeCampaigntoContact();
    }
    protected override void afterUndelete() {
        updateCampaigntoContact();
 
    }
 
 
    private void updateCampaigntoContact() {
        this.newlist =
            [select id, Contact_ID__c, Campaign__c from CampaignMember__c
             where Campaign__r.recordType.DeveloperName = 'ServiceEngineerTraining' and id in: newlist];
        list<Contact> updateContactlist = new list<Contact>();
        for (CampaignMember__c temCM : newlist) {
            updateContactlist.add(new Contact(id = temCM.Contact_ID__c, Campaign__c = temCM.Campaign__c, IsEndoscope__c = '是'));
        }
        if (updateContactlist.size() > 0) {
            ControllerUtil.updateSObjectContact(updateContactlist);
        }
 
    }
    private void removeCampaigntoContact() {
        this.oldList = [select id, Contact_ID__c, Campaign__c from CampaignMember__c
                        where Campaign__r.recordType.DeveloperName = 'ServiceEngineerTraining' and id in: oldList];
        list<id> oldContactIdlist = new list<id>();
        for (CampaignMember__c temCM : oldList) {
            oldContactIdlist.add( temCM.Contact_ID__c);
        }
        Map<id, contact> oldContactmap = new Map<id, contact>(
            [select id, Campaign__c
             from contact
             where Campaign__c != null
             and id in: oldContactIdlist]);
        list<Contact> updateContactlist = new list<Contact>();
        for (CampaignMember__c temCM : oldList) {
            Contact oldContact = oldContactmap.get(temCM.Contact_ID__c);
            if (oldContact != null && temCM.Campaign__c != null
                    && oldContact.Campaign__c.equals(temCM.Campaign__c)) {
                updateContactlist.add(new Contact(id = temCM.Contact_ID__c, Campaign__c = null, IsEndoscope__c = null));
            }
        }
        if (updateContactlist.size() > 0) {
            ControllerUtil.updateSObjectContact(updateContactlist);
        }
 
    }
 
}