如何在Attachment对象的顶点触发器下方进行批量处理,对于单个记录工作正常?

时间:2018-08-11 16:29:34

标签: salesforce apex

如何在Attachment对象上的顶点触发器下方进行批量处理,对于单个记录是否工作正常?

trigger UpdateEventParticAttacDate on Attachment(after insert, after update, after delete) {

List < EventParticipant__c > ToUpdate = new List < EventParticipant__c > ();
List < EventParticipant__c > EvntParti = new List < EventParticipant__c > ();
//Datetime LastModifiedDt;
Set < Id > pId = new Set < Id > ();

if (Trigger.isInsert || Trigger.isUpdate) {
    for (Attachment att: Trigger.new) {
        //LastModifiedDt = att.LastModifiedDate;
        pId.add(att.ParentId);
    }

    if (pId.size() > 0) {
        EvntParti = [select Id, Attached_Document_On__c from EventParticipant__c where Id IN: pId];

        for (EventParticipant__c ep: EvntParti) {
            ep.Attached_Document_On__c = DateTime.now();
            ToUpdate.add(ep);
        }
        Update ToUpdate;
    }
} else if (Trigger.isDelete) {
    for (Attachment att: Trigger.old) {
        pId.add(att.ParentId);
    }

    EvntParti = [select id, Name, Attached_Document_On__c from EventParticipant__c where id IN: pId];

    If(EvntParti.size() > 0) {
        List < Attachment > allChildren = [Select id, LastModifiedDate from Attachment where parentid IN: EvntParti order by LastModifiedDate desc];

        if (allChildren == null || allChildren.size() == 0) {
            for (EventParticipant__c ep: EvntParti) {
                ep.Attached_Document_On__c = null;
                ToUpdate.add(ep);
            }
        } else if (allChildren.size() > 0) {
            for (EventParticipant__c ep: EvntParti) {
                ep.Attached_Document_On__c = allChildren[0].LastModifiedDate;
                ToUpdate.add(ep);
            }
        }
    }
    Update ToUpdate;        
}
}

0 个答案:

没有答案