You need to sign in to do that
Don't have an account?

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;
}
}
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
here is your test class
let me knwo if any issues with it.
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:
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' );
}
}
try the one i gave it will definitely work.
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.
Ohh this is a lookup to another object I thouht lookup is to itself .
try this
Idealy you should not use hardcoded id.instead you should insert Propiedad__c also
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.
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:
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.
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!