function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
blake.tanonblake.tanon 

'Roll-up' Trigger Code Review - low test 67-77%

Hello,

I'm a novice programmer at best, i've got a very large trigger that i'm working on for CampaignMembers.  We want to populate fields on the CampaignMember record to show Sales($), Orders(#), Calls(#) & Meetings(#) for the 90 days prior then 90 days post Campaign StartDate.

 

When I wrote this I had 6 different SOQL queries for each set of fields for a total of 24 - which kills the test class.  If I break the code down into 24 different triggers I can get to 77% coverage, but I would rather not do that if there is a better way to do it.

 

I'm sure this is the lest effcient way to write this code but here is a snippet for the first fields which at -30days from start date.  Where the testclass as issues (in this smaller version - the complete it hits SOQL limits and fails with 67%) is in the BLUE highlighted areas 

 

trigger CMTrans3 on CampaignMember(before update)
{

Integer i = 0;

CampaignMember cm = Trigger.new[i];

// Current CampaignMember and Contact ID
String intCm = Trigger.new[i].Id;
CampaignMember cmm = [Select Contact.id, Start_Date__c, Sales_1__c, Meeting_1__c, Calls_1__c, Kits_1__c from CampaignMember where id =: intCm];

//Step 2. Create a list of Transactions who are children of Contact record.
List<Contact> cnt = [Select Contact.id from Contact where Contact.id =: cmm.Contact.id];
List<Transaction__c> tr3 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Trade_Date__c From Transaction__c WHERE Transaction__c.Rep__c =: cmm.Contact.id AND Transaction__c.Trade_Date__c <: cmm.Start_Date__c AND Transaction__c.Trade_Date__c >: cmm.Start_Date__c - 30];
List<Task> tsk3 = [Select Task.Id, Task.Completed__c From Task WHERE WhoId =: cmm.Contact.id AND Task.ActivityDate <: cmm.Start_Date__c AND Task.ActivityDate >: cmm.Start_Date__c - 30];
List<Event> evt3 = [Select Event.Id, Event.Completed__c From Event WHERE WhoId =: cmm.Contact.id AND Event.ActivityDate <: cmm.Start_Date__c AND Event.ActivityDate >: cmm.Start_Date__c - 30];
List<Order__c> o3 = [Select Order__c.Id, Order__c.Contact__c, Order__c.Number_of_Kits__c, Order__c.Order_Date__c FROM Order__c WHERE Order__c.Contact__c =: cmm.Contact.id AND Order__c.Order_Date__c <: cmm.Start_Date__c AND Order__c.Order_Date__c >: cmm.Start_Date__c - 30]; Double a = 0; // Loop through the filtered Transactions last 30 days and sum up their amounts. for(Transaction__c tr : tr3) { If (tr.Gross_Amount__c != Null) { a += tr.Gross_Amount__c; } } cm.Sales_1__c = a; Double a2 = 0; // Loop through the filtered Task last 30 days and sum up amount. for(Task tski : tsk3) { If (tski.Completed__c != Null) { a2 += tski.Completed__c; } } cm.Calls_1__c = a2; Double a3 = 0; // Loop through the filtered Task last 30 days and sum up amount. for(Event evti : evt3) { If (evti.Completed__c != Null) { a3 += evti.Completed__c; } } cm.Meeting_1__c = a3; Double a4 = 0; // Loop through the filtered Task last 30 days and sum up amount. for(Order__c oi1 : o3) { If (oi1.Number_of_Kits__c != Null) { a4 += oi1.Number_of_Kits__c; } } cm.Kits_1__c = a4; }

 

HariDineshHariDinesh

Hi,

 

I can just give you small suggestion here.

 

As you mentioned Blue lines of code is not covered.

 

The reason here is your Test methods have no proper data to test the above trigger.

Prepare test data such that there will be some data populated to tr3, tsk3, evt3, o3

 

Then try to run the test method. Now it will cover that part as well.

 

if you can provide the test class as well someone can help you in better way to solve your problem