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
Apex EstrategiaApex Estrategia 

Code coverage is in 74%

Hi,

 

I created this trigger:

 

trigger CriarTarefa_Produtos_Cobranca on Task (after update) {

    List<Task> taskProd = new List<Task>();
    for (Task tt : Trigger.new)
    if (tt.subject == 'Cobrança - Problemas com produtos' && tt.status == 'Concluído' || tt.subject == 'Cobrança - Pergunta não solucionada' && tt.status == 'Concluído'){
                taskProd.add (new Task(
                         Subject = 'Cobrança - Questão solucionada?',
                         Status = 'Nenhum',
                         WhatID = tt.WhatID,
                         Description = tt.description,
                    	 OwnerId = '005U0000001KrXY',
                         ActivityDate = Date.today()));
 }

    for (Task tt : Trigger.new)
    if (tt.subject == 'Cobrança - Questão solucionada?' && tt.Quest_o_solucionada__c == 'SIM' && tt.status == 'Concluído'){
                taskProd.add (new Task(
                         Subject = 'Cobrança - Feche o chamado e notifique o formando',
                         Status = 'Nenhum',
                         WhatID = tt.WhatID,
                         Description = tt.description,
                    	 OwnerId = '005U0000001KrXY',
                         ActivityDate = Date.today()));
 }
    for (Task tt : Trigger.new)
    if (tt.subject == 'Cobrança - Questão solucionada?' && tt.Quest_o_solucionada__c == 'NÃO' && tt.status == 'Concluído'){
                taskProd.add (new Task(
                         Subject = 'Cobrança - Pergunta não solucionada',
                         Status = 'Nenhum',
                         WhatID = tt.WhatID,
                         Description = tt.description,
                    	 OwnerId = '005U0000000FrCz',
                         ActivityDate = Date.today()));
 }
                insert taskProd;
            }

 And my test class:

 

@IsTest(SeeAllData=true)
public class CriarTarefa_Pendencia_Duomind_test{
static testmethod void MyUnitTest(){
    
 
     User u = [select id from User where LastName='Marketing'];
      
     Case c = new Case (recordtypeid='012U000000011yC');
    
     Task tsk  = new Task(Status='Não iniciado', Priority='Normal', subject='- Iniciador do Fluxo -');
     insert tsk;
 
    tsk.Status = 'Concluído';
    update tsk;
}
}

 

My code coverage is in 74%.

What I'm missing here?

 

Any help?

 

Thanks !

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
If this is the case, I would suggest to look at the production classes. Find out class which is having less coverage or no coverage in Production. Modify that class in sandbox and include in your deployment.

All Answers

Bhawani SharmaBhawani Sharma
Open trigger in Salesforce. Click on coverage link and it will show what is not covered yet.
Apex EstrategiaApex Estrategia

But my problem is that I can't import to my org.

I'm still in Sandbox.

 

 


Thanks !

Bhawani SharmaBhawani Sharma
Are you getting this issue when trying to deploy?
Apex EstrategiaApex Estrategia

Yeah. That's right !

Bhawani SharmaBhawani Sharma
If this is the case, I would suggest to look at the production classes. Find out class which is having less coverage or no coverage in Production. Modify that class in sandbox and include in your deployment.
This was selected as the best answer
InsightTeamInsightTeam

Why are there 3 "for" loops?  Shouldn't your "if" statements just be inside one "for" loop?  Maybe I am reading it wrong but you're also missing curly brackets on the "for" loops.

Apex EstrategiaApex Estrategia

You're right InsightTeam!

 

I fix that.

Thanks !

 

Still trying to find where I'm get this 74% of code coverage.

I think that I almost there.

InsightTeamInsightTeam

I think part of the reason is your test does not set the subject and other fields to the values you are checking for in your "if" statements.  I'm still somewhat new to apex but below is an example of how I would have written your class.  All you'd need to do is add in lines of code to your test so that it will cover the conditions in each of your "if" statements.  I think you can even go as far as to use a list to insert the tasks so that you don't run the risk of pushing the limits.

 

trigger CriarTarefa_Produtos_Cobranca on Task (after update) {

    List<Task> taskProd = new List<Task>();
	
    for (Task tt : Trigger.new){
		if (tt.subject == 'Cobrança - Problemas com produtos' && tt.status == 'Concluído' || tt.subject == 'Cobrança - Pergunta não solucionada' && tt.status == 'Concluído'){
					taskProd.add (new Task(
							 Subject = 'Cobrança - Questão solucionada?',
							 Status = 'Nenhum',
							 WhatID = tt.WhatID,
							 Description = tt.description,
							 OwnerId = '005U0000001KrXY',
							 ActivityDate = Date.today()));
		}else if (tt.subject == 'Cobrança - Questão solucionada?' && tt.Quest_o_solucionada__c == 'SIM' && tt.status == 'Concluído'){
					taskProd.add (new Task(
							 Subject = 'Cobrança - Feche o chamado e notifique o formando',
							 Status = 'Nenhum',
							 WhatID = tt.WhatID,
							 Description = tt.description,
							 OwnerId = '005U0000001KrXY',
							 ActivityDate = Date.today()));
		}else if(tt.subject == 'Cobrança - Questão solucionada?' && tt.Quest_o_solucionada__c == 'NÃO' && tt.status == 'Concluído'){
					taskProd.add (new Task(
							 Subject = 'Cobrança - Pergunta não solucionada',
							 Status = 'Nenhum',
							 WhatID = tt.WhatID,
							 Description = tt.description,
							 OwnerId = '005U0000000FrCz',
							 ActivityDate = Date.today()));
		}else{
			//do nothing
		}
	
		insert taskProd;
	}
}

 

 

 

SF94108SF94108

Add 1 task for each of your if clauses in the code, presently you are inserting 1 task:

 

 

 Task tsk  = new Task(Status='Não iniciado', Priority='Normal', subject='- Iniciador do Fluxo -');

 

Add more tasks in test class:

Task tsk2 = new Task(.....);

Task tsk3 = new Task(...);

 

That will make your test class cover the conditions in your code and improve coverage.

 

 

Apex EstrategiaApex Estrategia

I think this will work !

 

But I'm getting one error when I try to save the class.

 

Error: Compilation error: Expression cannot be assigned na linha -1 column -1

 

@IsTest(SeeAllData=true)
public class PORFAVORt{
static testmethod void MyUnitTest(){    
 
     User u = [select id from User where LastName='Marketing'];
      
     Case c = new Case (recordtypeid='012U000000011yC');
    
     Task tsk  = new Task(Status='Nenhum', WhatID=c.id, Description='teste',OwnerId=u.id, ActivityDate=Date.today(), Quest_o_solucionada__c='SIM', subject='Cobrança - Questão solucionada?');
     insert tsk;
    
     Task tsk2  = new Task(Status='Nenhum', WhatID=c.id, Description='teste',OwnerId=u.id, ActivityDate=Date.today(), subject='Cobrança - Feche o chamado e notifique o formando');
     insert tsk2;

     Task tsk3  = new Task(Status='Nenhum', WhatID=c.id, Description='teste',OwnerId=u.id, ActivityDate=Date.today(), subject='Cobrança - Pergunta não solucionada');
     insert tsk3;
    
    tsk.Status = 'Concluído'; task.description = 'teste'; task.whatid = c.id; task.Quest_o_solucionada__c = 'SIM';
    update tsk;
}
}

 

Thanks !

Apex EstrategiaApex Estrategia

I founded one class that have 0% of coverage.

 

I modify that class and now my trigger works !

 

You're right since the beggining Tech Force!

 

Thanks !