如何获取父对象字段中子对象中的记录数?

时间:2015-07-21 07:59:51

标签: salesforce apex

我有一个对象参与者,它是互动的孩子。在编辑子对象记录时,父对象中的字段应该递增。该场景是第一次如果invite为true且rsvp为null,则对父对象的计数应递增。如果rsvp设置为yes,则对于同一记录,接下来只有父对象上的rsvp字段应该递增。根据我的逻辑,它在被邀请和rsvp中都会增加。以下是触发器

Set<Id> interactionIds = new Set<Id>();
for (Participant__c erpAccount : participantList){
    interactionIds.add(erpAccount.Interaction__c);
}

List<Interaction__c> relatedInteractions =
    [Select i.Total_Number_Of_Invited_Attendees__c,
        (Select Invited__c,RSVP_Status__c,HCP__c,Interaction__c
 From  Participants__r
            WHERE Role_Group__c = 'Attendee'
                        AND (Invited__c = true
                        OR RSVP_Status__c = 'Yes'
                        OR HCP__c !=NULL))
        From Interaction__c i where id IN : interactionIds];

for(Interaction__c acc : relatedInteractions){
    //acc.Total_Number_Of_Invited_Attendees__c = 0; 

    for(Participant__c p : acc.Participants__r){
        if(p.Invited__c == true)
        {      
            if(p.RSVP_Status__c == 'Yes')
            {
                if(p.HCP__c != null)
                {
                   acc.Total_No_of_Confirmed_Customer_Attendees__c += 1;
                }
                else
                {
                    acc.Total_Number_of_Confirmed_Attendees__c += 1;                          
                }
            }
            else
            {
                acc.Total_Number_of_Invited_Attendees__c += 1;
            }
        }       
    }
}

update relatedInteractions;

提前致谢!!

0 个答案:

没有答案