You need to sign in to do that
Don't have an account?
Alex Dorn
Writing test class
I spent all of yesterday trying to write test classes for my two new triggers, but with zero experience with coding it has been a very difficult process. All the apex triggers do is update the Last_activity_type__c field when a new activity is logged. Is there anyone who can help me write the test class code? Here are my triggers:
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Task t :trigger.new)
{
if(t.whatid!=null)
{
Schema.SObjectType tType= t.whatid.getSObjectType();
if(tType == Opportunity.Schema.SObjectType)
{
OpportunityIds.add(t.Whatid);
}
}
}
{
//Querying the related Opportunity based on whatid on Task
Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
for(Task t :Trigger.new)
for(Opportunity l : OpportunityMap.Values())
{
l.Last_Activity_Subject__C = t.subject;
OpportunityList.add(l);
}
}
// updating the Opportunity
if(OpportunityList.size()>0)
{
update OpportunityList;
}
}
trigger updateRelatedLead on Task (after insert,after update) {
List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();
for(Task t :trigger.new)
{
if(t.whoId!=null)
{
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
}
}
}
{
//Querying the related Lead based on whoId on Task
Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
for(Task t :Trigger.new)
for(Lead l : LeadMap.Values())
{
l.Last_Activity_Subject__C = t.subject;
LeadList.add(l);
}
}
// updating the Lead
if(LeadList.size()>0)
{
update LeadList;
}
}
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Task t :trigger.new)
{
if(t.whatid!=null)
{
Schema.SObjectType tType= t.whatid.getSObjectType();
if(tType == Opportunity.Schema.SObjectType)
{
OpportunityIds.add(t.Whatid);
}
}
}
{
//Querying the related Opportunity based on whatid on Task
Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
for(Task t :Trigger.new)
for(Opportunity l : OpportunityMap.Values())
{
l.Last_Activity_Subject__C = t.subject;
OpportunityList.add(l);
}
}
// updating the Opportunity
if(OpportunityList.size()>0)
{
update OpportunityList;
}
}
trigger updateRelatedLead on Task (after insert,after update) {
List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();
for(Task t :trigger.new)
{
if(t.whoId!=null)
{
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
}
}
}
{
//Querying the related Lead based on whoId on Task
Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
for(Task t :Trigger.new)
for(Lead l : LeadMap.Values())
{
l.Last_Activity_Subject__C = t.subject;
LeadList.add(l);
}
}
// updating the Lead
if(LeadList.size()>0)
{
update LeadList;
}
}
Hopefully this work, I have revised the trigger and test class....Please see my comments
And the test class....
All Answers
After this I tried using the test class you wrote with my current triggers I did have to change the periods in lines 7 and 13 to a comma. When I ran the test though it said it failed. I may be running the test wrong (I've never ran a test before) or need to change something in the code to use it with my trigger names.
Hopefully this work, I have revised the trigger and test class....Please see my comments
And the test class....