You need to sign in to do that
Don't have an account?
Validating a trigger
Hi,
I'm working on my first trigger and test, and I think I got through most of the problems in the code, but I keep failing to validate them. In the developer console it tells me that the trigger has a 100% on all tests,but as you can see, this is not how the program reads it when validating.
Attached are the fails I get, and the modified code:
Trigger:
//Prevent deleting a contact that is associated with a 203k disbursement
trigger dontDelete on Contact (before delete)
{
RecordType r =[Select id, name from RecordType where SobjectType='Contact' and name='Contractor'];
for (Contact x : Trigger.old)
{
if (x.RecordTypeId==r.id)
{
x.addError('You cannot delete this an account of type "Contractor"');
}
}
}
Test:
@isTest
private class TestDontDelete
{
static testMethod void myUnitTest()
{
Contact con = new Contact (LastName = 'Testi', RecordTypeID= [SELECT Id FROM RecordType WHERE Name = 'Contractor'].Id);
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
Do u have more than one Record Type with the same label "Contractor" ?
This error usually comes when your query s returning more than one but u are capturing the result in a variable.Try to
change line in red,Either specify limit 1 clause or after querying the store them in the array .Look the red one.Change it to an arrar ,I mean list.
//Prevent deleting a contact that is associated with a 203k disbursement
trigger dontDelete on Contact (before delete)
{
RecordType r =[Select id, name from RecordType where SobjectType='Contact' and name='Contractor' limit 1];
for (Contact x : Trigger.old)
{
if (x.RecordTypeId==r.id)
{
x.addError('You cannot delete this an account of type "Contractor"');
}
}
}
Test:
@isTest
private class TestDontDelete
{
static testMethod void myUnitTest()
{
Contact con = new Contact (LastName = 'Testi', RecordTypeID= [SELECT Id FROM RecordType WHERE Name = 'Contractor'].Id);
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
Ok need some answers...
1. Is the test class running perfectly in your developer org ?
2. Are you getting this error while deployment ?
If yes I guess , you need to check the target ORG.
The target org doesnt seem to have a REcordtype Named "Contractor" for contacts. Create one.
2. I'm getting the errors either while deployment or validation.
The target ORG has RecordType "Contractor" under Contacts