You need to sign in to do that
Don't have an account?
Debugging Trigger
Hi all,
I am new to Salesforce, and I'm spending much time on this first trigger that I'm trying to deploy.
I feel like I am almost done creating it, but I get one problem with the trigger, and one with the test.
The goal of the trigger is to prevent the delete of contacts of record type 'contractor'. The reasoning bhind it is that Contractors often have child objects that I don't want to be deleted with them.
Here are the codes, and the problems (the lines where they appear are red).
Trigger:
//Prevent deleting a contact that is associated with a 203k disbursement
RecordType r =[Select id, name from RecordType where sobject='Contact' and name='Contractor'];
trigger dontDelete on Contact (before delete)
{
for (Contact x : Trigger.old)
{
if (x.RecordTypeId==r.id)
{
x.addError('You cannot delete this an account of type "Contractor"');
}
}
}
Probelm: unexpected token: RecordType
test:
@isTest
private class TestDontDelete
{
static testMethod void myUnitTest()
{
List<Contact> con = [SELECT id FROM Contact WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE sobject='Contact' and name='Contractor')];
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
Problem: No such column 'sboject' on entity 'RecordType'. If you are attempting to use a custon field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
Thank you all very much,
Gil
You need lil house keeping here
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"');
}
}
}
@isTest
private class TestDontDelete
{
static testMethod void myUnitTest()
{
List<Contact> con = [SELECT id FROM Contact WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE SobjectType='Contact' and name='Contractor')];
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
All Answers
You need lil house keeping here
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"');
}
}
}
@isTest
private class TestDontDelete
{
static testMethod void myUnitTest()
{
List<Contact> con = [SELECT id FROM Contact WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE SobjectType='Contact' and name='Contractor')];
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
However, I can't validate the trigger:
dontDelete: Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Deploy Error: Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.
If you could also help me with that, that will be great.
Well you need create a Contact programatically from the testClass.
Some thing like
Contact con = new Contact(LastName='Test',RecordtypeId='<Query the recordtype Id and assign>');
then execute the delete
Hi Avi and thanks for walking me pationately through this.
I did what you said, but I keep failing to validate. 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());
}
}
}