You need to sign in to do that
Don't have an account?
Trigger deploy
Hello, I'm pretty new to salesforce and I'm trying to deploy a trigger I adjusted from an online source.
I cannot deploy/validate the trigger, because the results of the Test coverage are 0%. However, when I run the code on the developer console, I get higher percentages. Still, there are some tests that the code fails in the DC as well. I would appreciate any help in getting past this stage.
The code is meant to prevent deleting contacts of a certain record type id. Here are the code and the test:
//Prevent deleting a contact that is associated with a 203k disbursement
trigger dontDelete on Contact (before delete)
{
for (Contact x : Trigger.old)
{
if (x.RecordTypeId=='01250000000DwuV')
{
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 ='01250000000DwuV'];
try
{
delete con;
}
catch (dmlexception e)
{
system.assert(e.getMessage().contains('You cannot delete this an account of type "Contractor"'),
e.getMessage());
}
}
}
HI ,
Don't hard code the id in code.
Make a query to recordtype object-
RecordType r =[Select id, name from RecordType where sobject='Account' 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"');
}
}
}
Thanks
Shailu
Use Record type name to check both in trigger and in test method also.
SELECT ... FROM Contact WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'ABC')
Is this the code the way I should write it, or should I add the query in right before the for loop?
Did you mean to write Contact for sobject?
Trigger:
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"');
}
}
}
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());
}
}
}
Thank you all so much for all the help!
I'm looking in to how to solve two errors I now have.
on the trigger
line: RecordType r =[Select id, name from RecordType where sobject='Contact' and name='Contractor'];
Error:unexpected token: RecordType
on the test
line: List<Contact> con = [SELECT id FROM Contact WHERE RecordTypeId in (SELECT Id FROM RecordType WHERE sobject='Contact' and name='Contractor')];
error: no such column 'sobject' on entity 'rocordtype'...