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
cmunozhernancmunozhernan 

Help needed with Test Method for trigger!

Hello! I have been trying to do a Test Method for the following Trigger but everything I do doesn't seem to work. Please help me in any way you can. Here is my Trigger:

 

trigger duplicamant on Mantenimiento_Gastos__c(before insert){
    for(Mantenimiento_Gastos__c c : Trigger.new)
        {
Mantenimiento_Gastos__c[] contacts=[Select id,Propiedad__c from Mantenimiento_Gastos__c Where Propiedad__c=:c.Propiedad__c];
            if (contacts.size()>0) 
             {
            c.Propiedad__c.addError('The number is already registered!');
              }
          }
}

I have tried the following Test Method but it is giving me an error and I think it is because of the id, I don't seem to find a way for it not to give me an error. Please help me!

 

@isTest
class testmantenimiento{

static testmethod void testmantenimiento(){

Integer size = 1;
List<Mantenimiento_Gastos__c> opp = new List<Mantenimiento_Gastos__c>();
for (Integer i = 0; i < size; i++)  {
   Mantenimiento_Gastos__c o = new Mantenimiento_Gastos__c();
   o.Propiedad__c = 'a0IQ0000000o962';
   opp.add(o);
}
insert opp;  
}
}



Best Answer chosen by Admin (Salesforce Developers) 
CLKCLK

Propiedad__c is lookup to Propiedad object or whatever. So create one instance/record of that object in code & then insert it. And then set opp.Propiedad__c = objPropiedad.id.

 

It will work.

All Answers

Shashikant SharmaShashikant Sharma

here is your test class

 

@isTest
class testmantenimiento{

static testmethod void testmantenimiento(){

Integer size = 1;
Mantenimiento_Gastos__c opp = new Mantenimiento_Gastos__c();                                     //fill other req fields if any
insert opp;  

Mantenimiento_Gastos__c opp1 = new Mantenimiento_Gastos__c();
opp1.Propiedad__c = opp.id;//fill other req fields if any 
insert opp1;  

Mantenimiento_Gastos__c opp2 = new Mantenimiento_Gastos__c();
opp2.Propiedad__c = opp.id;
//fill other req fields if any 
insert opp2;  

}
}

 

 

 let me knwo if any issues with it.

cmunozhernancmunozhernan

One last thing, Propiedad__c is a look up field, that is why is so difficult this Test Method for me since I am new to SF. Thank you...

Here is another Test Method I tried and didn't work either:

 

@isTest
class testmantenimiento{

static testmethod void testmantenimiento(){

Mantenimiento_Gastos__c ca = new Mantenimiento_Gastos__c(); 
ca.Propiedad__c = 'a0IQ0000000o962'  ;
insert ca;
system.assertEquals(ca.Propiedad__c ,'a0IQ0000000o962'  );
}
}



Shashikant SharmaShashikant Sharma

try the one i gave it will definitely work.

cmunozhernancmunozhernan

Thank you very much Mr. Sharma for the Test Method. I tried it and I get the following error:

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Propiedad: id value of incorrect type: a0FQ0000008w7S5MAI: [Propiedad__c]

The issue that I am having is still with the id. I thought I had the issue solved by using the following Id: a0IQ0000000o962, but when I deploy it to Production it gives me an Id error.

Shashikant SharmaShashikant Sharma

Ohh this is a lookup to another object I thouht lookup is to itself .

 

try this

 

@isTest
class testmantenimiento{

static testmethod void testmantenimientoMethod(){


Mantenimiento_Gastos__c opp1 = new Mantenimiento_Gastos__c();
opp1.Propiedad__c = 'a0IQ0000000o962';//fill other req fields if any 
insert opp1;  

Mantenimiento_Gastos__c opp2 = new Mantenimiento_Gastos__c();
opp2.Propiedad__c = 'a0IQ0000000o962';
//fill other req fields if any 
insert opp2;  

 Idealy you should not use hardcoded id.instead you should insert Propiedad__c also

 

@isTest
class testmantenimiento{

static testmethod void testmantenimientoMethod(){


Propiedad__c opp = new Propiedad__c();                                                            //fill other req fields if any
insert opp;  

Mantenimiento_Gastos__c opp1 = new Mantenimiento_Gastos__c();
opp1.Propiedad__c = opp.id;//fill other req fields if any 
insert opp1;  

Mantenimiento_Gastos__c opp2 = new Mantenimiento_Gastos__c();
opp2.Propiedad__c = opp.id;
//fill other req fields if any 
insert opp2;  

}
}

 

CLKCLK

Propiedad__c is lookup to Propiedad object or whatever. So create one instance/record of that object in code & then insert it. And then set opp.Propiedad__c = objPropiedad.id.

 

It will work.

This was selected as the best answer
cmunozhernancmunozhernan

Thank you very much for all your help, you both really helped get my ideas straight. I hope the code can help anybody having the same issue. The following code is:

 

@isTest
class testmantenimiento{

static testmethod void testmantenimientoMethod(){


Propiedad__c objPropiedad = new Propiedad__c();                               
insert objPropiedad;  

Mantenimiento_Gastos__c opp1 = new Mantenimiento_Gastos__c();
opp1.Propiedad__c = objPropiedad.id;//fill other req fields if any 
insert opp1;  

}
}



Shashikant SharmaShashikant Sharma

I am  surprised how your method is covering all the code

the proper test method is this  taht I gave you in my last post , your test method can not cover code inside contacts.size() > 0 , for this you need to create such condition, 

 

But it is up to you which one you consider correct answer. I would request you to just give it a try.

 

@isTest
class testmantenimiento{

static testmethod void testmantenimientoMethod(){

try{
Propiedad__c opp = new Propiedad__c();                                                            //fill other req fields if any
insert opp;  

Mantenimiento_Gastos__c opp1 = new Mantenimiento_Gastos__c();
opp1.Propiedad__c = opp.id;
insert opp1;  

Mantenimiento_Gastos__c opp2 = new Mantenimiento_Gastos__c();
opp2.Propiedad__c = opp.id;
//fill other req fields if any 
insert opp2;  
}                                                                                                 catch(exception e)                                                                                      {                                                                                                       }
}
}

 

cmunozhernancmunozhernan

Thank you very much Shashikant Sharma for the code! I really appreciate you took the time to help me with the issue. I tried your code and the coverage is of 100%. Believe me that if I could give you both the correct answer I would. The other code had only 75% but that is all I wanted for it to be deployed to production. Now with your code I have a better knowledge of what can be achieved. Thank you so much!