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
Alex DornAlex Dorn 

Help writing test class

How would I write a test class for each of these two codes I am beyond confused with Apex. The triggers just update the Last_activity_type__c field when a new activity is logged.

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;
    }
}
Leonardo AlvesLeonardo Alves
So that's what you need. Once you finish with the test, there is a button called 'Run test' :)