0

I have 3 objects Master Contact, Master Volunteer_Project__c and Detail Participation__c. Means Participation__c is detail for both Master. Now One Volunteer_Project__c can have many Participation__c. Participation__c has a field called Volunteer_Email__c which is formula field and it is fetching the it's contact's email. Now My task is to count Number of Participation__c worked for each Volunteer_Project__c and I am counting unique email addresses (Volunteer_Email__c) for it. I can't do it with Roll-up summary field so I wrote a trigger and test cases but need a help with test cases.

Trigger: WORKS FINE!

    Trigger UniqueEmailCount On Volunteer_Project__c(before insert, before update)
{
    List<Participation__c> ParticipationList = new List<Participation__c>();
    Set<String> uniqueEmails = new Set<String>();

    ParticipationList = [SELECT Volunteer_Email__c FROM Participation__c where Volunteer_Project_Name__c IN :Trigger.New];

        for (Integer i = 0; i< ParticipationList.size(); i++)
        {
            uniqueEmails.add(ParticipationList[i].Volunteer_Email__c);

        }

        for(Volunteer_Project__c proj: Trigger.new)
        {
            proj.Total_Associated_Volunteers__c = uniqueEmails.size();
        }
}

Test Cases: (NOT GETTING ANY COVERAGE)

  @isTest (seeAllData=true) 
Public class UniqueEmailCount_Test{
    Public Static TestMethod Void TestingUniqueEmailCount()
    {

        Set<String> uniqueEmails = new Set<String>();
        List<Participation__c> partiList = new List<Participation__c >();
        List<Volunteer_Project__c> Vprojects = new List<Volunteer_Project__c >();
        List<Contact> Cons = New List<Contact>();


        for(Integer L = 0; L < 200 ; L++){
        Contact C = New Contact(FirstName = 'John', LastName = 'Doe' + L, Email = '[email protected]' + L);

        Cons.add(C);
        }
        insert Cons; // In here  we have 200 contacts created with Email address

        for(Integer I = 0; I < 200; I++)
        {
            Participation__c Parti = New Participation__c (Volunteer_Name__c = 'John Doe' + i, Volunteer_Project_Name__c = 'Project:' + i
                                                , Devoted_Hours__c = 1);   
            insert Parti; // Here is we inserted the Parti so I think at this point we have Volunteer_Email__C populated.
            partiList.add(Parti);

        }
        insert partiList; // In here we have 200 participant created with Volunteer_Email__C I guess.

        for (Integer k = 0; k< partiList.size(); k++)
        {
            uniqueEmails.add(partiList[k].Volunteer_Email__c);
        }
        for(Integer M = 0; M < 200; M++){
        Volunteer_Project__c vp = New Volunteer_Project__c(Name = 'Project: ' + M, Total_Associated_Volunteers__c = uniqueEmails.Size());
        Vprojects.add(vp);

        }
        insert Vprojects; // In here we have 200 volunteer projects creted.

        for(Volunteer_Project__c V: Vprojects)
        {
            System.assertEquals('V.Total_Associated_Volunteers__c', 'uniqueEmails.size()');

        }



    }

}
2
  • You should use dlrs.
    – Adrian Larson
    Oct 25, 2016 at 18:52
  • Would It allow me to roll up only identical email address of child record.
    – Govind
    Oct 25, 2016 at 18:58

2 Answers 2

0

Clicks not code!

I recommend you use declarative-lookup-rollup-summaries, with which you can simply create a Lookup Rollup Summary record that uses the Aggregate Operation Count Distinct.

  • Parent Object: Volunteer_Project__c
  • Relationship Field: Volunteer_Project_Name__c
  • Child Object: Participation__c
  • Field to Aggregate: Volunteer_Email__c
  • Aggregate Operation: Count Distinct
  • Aggregate Result Field: Total_Associated_Volunteers__c
0

I would surmise that you are failing test coverage because the test mocking is failing on DML limits as you are adding Parti one-by-one in a for loop. There is a limit of 150 DML statements per transaction

for(Integer I = 0; I < 200; I++)
   {
    Participation__c Parti = New Participation__c (Volunteer_Name__c = 'John Doe' + i, Volunteer_Project_Name__c = 'Project:' + i
                                                , Devoted_Hours__c = 1);   
    insert Parti; // Ack!! DML inside for loop
    partiList.add(Parti);

  }
insert partiList; // In here we have 200 participant created with Volunteer

A coding aside; the above can be rewritten more compactly:

for (Integer i = 0; i < 200, i++)
  partiList.add(new Participation__c(Volunteer_Name__c = ....));
insert partiList;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .