+ Start a Discussion
Blake TanonBlake Tanon 

Apex noob: Test Class?

I'm very new to programming and even newer to apex, I've modified the below code from a sample I found but have no idea how to write a test class for it.  If it is not to difficult would anyone have an idea?

 

trigger SumSalesOnAccount on Account (before update)
{

Integer i = 0;

Account acc = Trigger.new[i];

// Current Account ID
String intTest = Trigger.new[i].Id;



//Step 2. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-1'];

Double a = 0;

// Loop through the filtered Transactions and sum up their amounts.
for(Transaction__c tr : trn)
{
If (tr.Gross_Amount__c != Null)
{
a += tr.Gross_Amount__c;
}
}
acc.Sales_1__c = a;

//Step 2.2. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn2 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-2'];

Double b = 0;

// Loop through the filtered Tranasctions and sum up their amounts.
for(Transaction__c tr2 : trn2)
{
If (tr2.Gross_Amount__c != Null)
{
b += tr2.Gross_Amount__c;
}
}
acc.Sales_2__c = b;

//Step 2.3. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn3 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-3'];

Double c = 0;

// Loop through the filtered Tranasctions and sum up their amounts.
for(Transaction__c tr3 : trn3)
{
If (tr3.Gross_Amount__c != Null)
{
c += tr3.Gross_Amount__c;
}
}
acc.Sales_3__c = c;

//Step 2.4. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn4 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-4'];

Double d = 0;

// Loop through the filtered Tranasctions and sum up their amounts.
for(Transaction__c tr4 : trn4)
{
If (tr4.Gross_Amount__c != Null)
{
d += tr4.Gross_Amount__c;
}
}
acc.Sales_4__c = d;

//Step 2.5. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn5 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-5'];

Double e= 0;

// Loop through the filtered Tranasctions and sum up their amounts.
for(Transaction__c tr5 : trn5)
{
If (tr5.Gross_Amount__c != Null)
{
e += tr5.Gross_Amount__c;
}
}
acc.Sales_5__c = e;

//Step 2.6. Create a list of Transactions who are children of this Account record.
List<Transaction__c> trn6 = [Select Transaction__c.Id, Transaction__c.Gross_Amount__c, Transaction__c.Month_Rank__c From Transaction__c where Transaction__c.Firm__c =: intTest AND Transaction__c.Month_Rank__c =: '-6'];

Double f = 0;

// Loop through the filtered Tranasctions and sum up their amounts.
for(Transaction__c tr6 : trn6)
{
If (tr6.Gross_Amount__c != Null)
{
f += tr6.Gross_Amount__c;
}
}
acc.Sales_6__c = f;
}

Anoop AsokAnoop Asok

Hi,

Execute the following steps in order in your test class. It'll give you the required coverage.

 

1. Create 6 dummy accounts
2. Create 6 dummy transaction records, each linking to one account through the Firm__c field, and Month_Rank__c varying from -1 to -6. Also give some value for Gross_Amount__c field.
3. update the 6 dummy accounts.

 

Thanks,

Anoop Asok