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
mrtelesmrteles 

Write a test class to coverage my trigger

hi community, I'm anxious to deploy my trigger, but my code coverage is bad <75% =(

My trigger:
trigger InsertDeploymentProject on Opportunity (after insert, after update) {
	List<Projeto_Implantacao__c> pi = new List<Projeto_Implantacao__c>();
	Projeto_Implantacao__c projeto;
	Map<Id, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
	Set<Id> accountIds =  new Set<Id>();
	
	for(Opportunity o:Trigger.new){
		accountIds.add(o.AccountId);
	}
	Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
	
	
	if(Trigger.isAfter) {
    if(Trigger.isUpdate) {
       for(Opportunity o: Trigger.new) {
       	for(Opportunity old: Trigger.old) {
           if(o.Id==old.id && o.IsWon  ){
           	 
             projeto = new Projeto_Implantacao__c();
             
             projeto.Name = 'Imp - '+ accountMap.get(o.AccountId).Name + ' - ' + rtMap.get(o.RecordTypeId).getName();
             projeto.Implantacao__c = o.AccountId;
             projeto.Status__c = 'Aguardando início';
             pi.add(projeto);           
           }
       	}   
       }       
    }
    if(!pi.IsEmpty())
      insert projeto;
  }
}

My test:

@isTest private class TesteInsertDeploymentProject {

    @istest static void TestDeploymentProject() {
	Account account = new Account();
	account.Name = 'Nome da conta de teste';
	account.Telefone_2__c = '(067) 3027-9600';
	account.Phone = '(067) 3027-9600';
	account.Classificacao_PAF_ECF__c = 'Sem ECF + Não PAF';
	account.CNPJ__c = '86.571.322/0001-06';
	account.Unidade_de_atendimento__c = 'Campo Grande';
	String accid = account.Id;
	
	insert account;
	
	Opportunity opp = new Opportunity();
	opp.Name = 'Opp';
	opp.AccountId = account.Id;
	opp.ForecastCategoryName = 'Funil';
	opp.CloseDate = Date.today();
	opp.StageName = 'Negocição e revisão';
	opp.Expectativas_do_cliente__c = 'Test';
	opp.Numero_de_licencas__c = 1;
	opp.Amount = 1;
	opp.Valor_Implantacao__c = 1;

	String op = opp.Id;
	
	insert opp;
	
	Projeto_Implantacao__c projeto = new Projeto_Implantacao__c();             
	System.debug(projeto.Name = 'Imp - '+ opp.AccountId + ' - ' + opp.RecordTypeId);
	System.debug(projeto.Implantacao__c = opp.AccountId);
	System.debug(projeto.Status__c = 'Waiting for start');
	insert projeto;
	
	System.debug(opp.StageName = 'Close and won');
	update opp;
	
	projeto = new Projeto_Implantacao__c();             
	System.debug(projeto.Name = 'Imp - '+ opp.AccountId + ' - ' + opp.RecordTypeId);
	System.debug(projeto.Implantacao__c = opp.AccountId);
	System.debug(projeto.Status__c = 'Waiting for start');
	insert projeto;
	
	opp.StageName = 'Close and Lost';
	update opp;
	
	projeto = new Projeto_Implantacao__c();             
	System.debug(projeto.Name = 'Imp - '+ opp.AccountId + ' - ' + opp.RecordTypeId);
	System.debug(projeto.Implantacao__c = opp.AccountId);
	System.debug(projeto.Status__c = 'Waiting for start');
	insert projeto;
	          
	projeto = new Projeto_Implantacao__c();             
	System.debug(projeto.Name = 'Imp - '+ opp.AccountId + ' - ' + opp.RecordTypeId);
	System.debug(projeto.Implantacao__c = opp.AccountId);
	System.debug(projeto.Status__c = 'Waiting for start');
	insert projeto; 
	
	
	
    }
}


I'm writing some triggers and I have difficult to deploy because I have difficult to create test class.

I saw various docs but they not helped a lot. 

Anyone help me?

Tnx.
 

Leonardo AlvesLeonardo Alves
Hello,

When you set the stage name to 'Close and Lost' and 'Close and Won', are those correct status codes? Should it 'Closed Won'?
mrtelesmrteles

Hello Leonardo, 

They are correct. 

The problem is in not coverage to the lines 19 to 24

.....
projeto = new Projeto_Implantacao__c();
             
             projeto.Name = 'Imp - '+ accountMap.get(o.AccountId).Name + ' - ' + rtMap.get(o.RecordTypeId).getName();
             projeto.Implantacao__c = o.AccountId;
             projeto.Status__c = 'Aguardando início';
             pi.add(projeto); 

....
Tnx.
Leonardo AlvesLeonardo Alves
So, the if condition to enter this part of the code is:

(o.Id==old.id && o.IsWon  )

Can you check your stage names? Please go to:

- Setup -> Customize -> Opportunities -> Fields
- Then click on the stage field and check if the stage picklist values 'Close and Won' or 'Close and Lost' are marked as 'Won' besides them