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
bonny mankotiabonny mankotia 

I have a simple trigger but enable to cover the code coverage please anyone idea about this?

trigger ClosedOpportunityTrigger on Opportunity (before insert) 
{    
    list<task> carry = new list<task>();
    for(opportunity opp: trigger.new)
    {
        if(opp.StageName == 'Closed Won')
        {
           task t = new task(whatid=opp.id,Subject = 'Follow Up Test Task');
            carry.add(t); 
        }
   }
     insert carry;
}

Here is my test class

@isTest
public class TestOppTrig 
{
    static testmethod void testFun()
    {
        Opportunity opp = new opportunity();
        opp.Name = 'Bob';
        opp.StageName = 'closed won';
        opp.CloseDate = system.today();
        insert opp;
        task t = new task();
        t.whatid=opp.id;
        t.subject = 'dasfsdfds';
        insert t;
    }
}
Best Answer chosen by bonny mankotia
JonathanBaltzJonathanBaltz
I believe you're most of the way there.  First, I'm wondering if this trigger is actually working because it seems you're trying to create a Task record that is related to an Opportunity before the Opportunity is finished being created.  This trigger will probably work better as an "after" trigger instead of "before."  Secondly, you may also want to cover your bases for Opportunities that are 'updated' to the stage "Closed Won."

​Overall, you test class has everything you need for a positive test.  Once you insert the Opportunity, just confirm that a related task has been created by the trigger.  This should work as long as your trigger is working. 

All Answers

JonathanBaltzJonathanBaltz
I believe you're most of the way there.  First, I'm wondering if this trigger is actually working because it seems you're trying to create a Task record that is related to an Opportunity before the Opportunity is finished being created.  This trigger will probably work better as an "after" trigger instead of "before."  Secondly, you may also want to cover your bases for Opportunities that are 'updated' to the stage "Closed Won."

​Overall, you test class has everything you need for a positive test.  Once you insert the Opportunity, just confirm that a related task has been created by the trigger.  This should work as long as your trigger is working. 
This was selected as the best answer
Gyanender SinghGyanender Singh
Hi Bonny,

First please run your trigger after insert because in before insert you can not get the whatid in the task so please use the below code:
trigger ClosedOpportunityTrigger on Opportunity (after insert) 
{    
    list<task> carry = new list<task>();
    for(opportunity opp: trigger.new)
    {
        if(opp.StageName == 'Closed Won')
        {
           task t = new task(whatid=opp.id,Subject = 'Follow Up Test Task');
            carry.add(t); 
        }
   }
     insert carry;
}

@isTest
public class TestOppTrig 
{
    static testmethod void testFun()
    {
        Opportunity opp = new opportunity();
        opp.Name = 'Bob';
        opp.StageName = 'closed won';
        opp.CloseDate = system.today();
        insert opp;
        
    }
}

and your triggerr cover 100% code coverage.

Thanks
Gyani
http://www.mirketa.com