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
GilzGilz 

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

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

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

Avidev9Avidev9

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());
    }
  }
}

 

This was selected as the best answer
GilzGilz
Thank you very much Avi, no more "problems" now,
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.
Avidev9Avidev9

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

GilzGilz

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:

 

TestDontDelete.myUnitTest() - Failure Message: "System.QueryException: List has more than 1 row for assignment to SObject", Failure Stack Trace: "Class.TestDontDelete.myUnitTest: line 7, column 1"
 
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.

 

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());
  }
}
}