+ Start a Discussion
NarcissuNarcissu 

Need Help for Test Code

Hi,

 

I am writing the code below to update the fields on different objects. But I have a hard time writing the test code. Please have a look at the code and a 66% test code below, and please give me some ideas pointing me to the right direction.

 

Thanks in advance.

 

Main Code:

 

Trigger UpdateUpcoming on Task (after insert, after update)
{
    Set<id>AccountsIDs = new Set<id>();
    List<Account>AcctList = new List<Account>();
    
//This section will address the account

    For (task t:Trigger.new)
    {
    For (Account at:[Select id,upcoming_due__c,upcoming_due_2__c From Account where id =: t.accountid])
{

//This section will pick up the due date of the task

    If (at.upcoming_due__c == null)
        at.upcoming_due__c = date.valueof(t.activitydate);
    Else
    If (date.valueof(t.activitydate) < at.upcoming_due__c)
        at.upcoming_due__c = date.valueof(t.activitydate);
    Else
    If (date.valueof(t.activitydate) > at.upcoming_due__c)
        at.upcoming_due_2__c = date.valueof(t.activitydate);
    AcctList.add(at);
}
}

//This section will update the due date into the Upcoming Due in the corresponding Account

    update AcctList;
}

 

66% Code (Need 75% at least..):

 

@isTest
private class TestUpcomingDueDate
{
private static TestMethod void testTrigger()
{
account ac=new account(name='abc');
insert ac;
task t=new task(activitydate=system.today(),whatid=ac.id);
insert t;
update ac;
}
}


Best Answer chosen by Admin (Salesforce Developers) 
SRajesh8SRajesh8

HI

 

 

@isTest
private class TestUpcomingDueDate
{
private static TestMethod void testTrigger()
{
account ac=new account(name='abc',upcoming_due__c=NULL,upcoming_due_2__c=system.Today());
insert ac;

ac.upcoming_due__c=system.today()-2;

update ac;

ac.upcoming_due__c=system.today()+5;


update ac;

task t=new task(activitydate=system.today(),whatid=ac.id);
insert t;
update ac;
}
}

 

Regards,

Rajesh S.