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
Felipe FernandesFelipe Fernandes 

Test class - Task Trigger

Hi,

 

I have a trigger that update a field in opportunity when a task related to this opportunity have the status set to completed.

Here's my trigger:

 

 

trigger TestTask on Task (after update) {
    public Id oppId;
    for(Task t:Trigger.New){
        if(t.isClosed){
            oppId = t.WhatId;
        }
    }
    List<Opportunity> opp = [select Id from Opportunity where id = :oppId];
    for(Opportunity o : opp){
        o.Tarefa__c = 'Não';
        update o;
    }
   
}

 

I have to write a text class but i'm not sure how can I test the value of the updated field. I've inserted an opportunity and a task, but I don't know how can I finish the test class.

 

 

 

 

@isTest
private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger(){
        Opportunity opp = new Opportunity();
        opp.name = 'test opp 1';
        opp.StageName = 'Oferecer projeto/orçamento';
        opp.LeadSource = 'Google';
        opp.Projeto__c = 'COM';
        opp.Regiao__c = 'Baixada Santista';
        opp.Forma_de_atencimento__c = 'individual';
        opp.Garantia__c = '01 ano contra defeito de fabricação';
        opp.CloseDate = date.today();
                                
        insert opp;
        
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Donni';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Responsavel__c='Felipe';

        insert t;       

 

Any help?

 

Thanks!!

 

BaguiarBaguiar

Hi Felipe.  Brasileiro nao eh ? :)

Test something like this:

private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger(){

List<Opportunity> opp = new List<Opportunity>{ new opp(
        name = 'test opp 1',
        StageName = 'Oferecer projeto/orçamento',
        LeadSource = 'Google',
        Projeto__c = 'COM',
        Regiao__c = 'Baixada Santista',
        Forma_de_atencimento__c = 'individual',
        Garantia__c = '01 ano contra defeito de fabricação',
        CloseDate = system.today()) };
                                
        insert opp;
        
List<task> t = new List<task>{ new task(
        WhatID = opp[0].id,
        Subject='Donni',
        Status='Completed',
        Priority='Normal',
        Responsavel__c='Felipe')};

        insert t;  


Opportunity[] opps = [select id,Tarefa__c from Opportunity where id in :opp];
    System.assertEquals('Não',opps[0].Tarefa__c);
    
 }
}

 

But I'd insert more than one record for Opportunity and task, so that the test can really assert the correct values.

Breno

Felipe FernandesFelipe Fernandes

Hi Breno. Sim Sim =]

 

I recieved an error:

 

System.AssertException: Assertion Failed: Expected: Não, Actual: null

Class.testMyTaskTrigger.testTTrigger: line 30, column 5 External entry point

 

Do you know how can i fix that?

Thanks.

 

Felipe.

 

BaguiarBaguiar

Try this... Just saw that your trigger was "after update"..

 

private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger(){

List<Opportunity> opp = new List<Opportunity>{ new opp(
        name = 'test opp 1',
        StageName = 'Oferecer projeto/orçamento',
        LeadSource = 'Google',
        Projeto__c = 'COM',
        Regiao__c = 'Baixada Santista',
        Forma_de_atencimento__c = 'individual',
        Garantia__c = '01 ano contra defeito de fabricação',
        CloseDate = system.today()) };
                                
        insert opp;
        
List<task> t = new List<task>{ new task(
        WhatID = opp[0].id,
        Subject='Donni',
        Status='New',
        Priority='Normal',
        Responsavel__c='Felipe')};

        insert t;  

List<task> taskstoupdate = New List<task>{ [select id from task where id in :t]};
    for(task t:taskstoupdate)
    t.status = 'Completed';

        Update eventstoupdate;



Opportunity[] opps = [select id,Tarefa__c from Opportunity where id in :opp];
    System.assertEquals('Não',opps[0].Tarefa__c);
    
 }

 

Are you suing the Force.com IDE to test ?

Pradeep_NavatarPradeep_Navatar

When you are inserting a Task in Test Class then you need to give the Whatid also.

 

t.Whatid = opp.id;

BaguiarBaguiar

Good catch... I had my code copied from an instance where I've inserted multiple Opportunities. Thats why the opp[0].id there..

Felipe FernandesFelipe Fernandes

I can't save the class... I got an error in this part:

 

List<task> tasktoupdate = New List<task>{ [select id from task where id in :t]};
    for(task t:tasktoupdate)
    t.status = 'Completed';

 

Error: "Duplicate variable: t"

 

I'm using Sales Force to run tests. Button run tests on apex classes page.

BaguiarBaguiar

oops..  had used the t already on previous task ..

try it:

private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger(){

List<Opportunity> opp = new List<Opportunity>{ new opp(
        name = 'test opp 1',
        StageName = 'Oferecer projeto/orçamento',
        LeadSource = 'Google',
        Projeto__c = 'COM',
        Regiao__c = 'Baixada Santista',
        Forma_de_atencimento__c = 'individual',
        Garantia__c = '01 ano contra defeito de fabricação',
        CloseDate = system.today()) };
                                
        insert opp;
        
List<task> t = new List<task>{ new task(
        WhatID = opp[0].id,
        Subject='Donni',
        Status='New',
        Priority='Normal',
        Responsavel__c='Felipe')};

        insert t;  

List<task> taskstoupdate = New List<task>{ [select id from task where id in :t]};
    for(task tOK:taskstoupdate)
    tOK.status = 'Completed';

        Update eventstoupdate;



Opportunity[] opps = [select id,Tarefa__c from Opportunity where id in :opp];
    System.assertEquals('Não',opps[0].Tarefa__c);
    
 }

 

Felipe FernandesFelipe Fernandes

I'm having the same error =/

 

"System.AssertException: Assertion Failed: Expected: Não, Actual: null"

 

any help?

 

my code:

 

 

@isTest

private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger(){

List<Opportunity> opp = new List<Opportunity>{ new opportunity(
        name = 'test opp 1',
        StageName = 'Oferecer projeto/orçamento',
        LeadSource = 'Google',
        Projeto__c = 'COM',
        Regiao__c = 'Baixada Santista',
        Forma_de_atencimento__c = 'individual',
        Garantia__c = '01 ano contra defeito de fabricação',
        CloseDate = system.today()) };
                                
        insert opp;
        
List<task> t = new List<task>{ new task(
        WhatID = opp[0].id,
        Subject='Donni',
        Status='New',
        Priority='Normal',
        Responsavel__c='Felipe')};

        insert t;  

List<task> taskstoupdate = New List<task>{ [select id from task where id in :t]};
    for(task tOK:taskstoupdate)
    tOK.status = 'Completed';

        Update taskstoupdate;



Opportunity[] opps = [select id,Tarefa__c from Opportunity where id in :opp];
    System.assertEquals('Não',opps[0].Tarefa__c);
    
 }
 }

 

 

BaguiarBaguiar

Felipe, Try changing the 2nd line to reference you trigger, as you've specified on the trigger name:

 

Should read " Private class testTesttask {  "

 

Also, are the values of the Task status field changed to Portuguese ? Like " Nova, Nao iniciada, completa, etc.." ?

BaguiarBaguiar

Were you able to get this to work ?

Felipe FernandesFelipe Fernandes

The problem was the task status...

The code is working now!

 

Thanks!!