+ Start a Discussion
GilzGilz 

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:

 

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

swatKatswatKat

Do u have more than one Record Type with the same label "Contractor" ?

SurpriseSurprise

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

Avidev9Avidev9

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.

GilzGilz
Under Contacts I have only one, but I also have one under Accounts
GilzGilz
1. Now that I look, the test class has zero code coverage, but no errors as well.
2. I'm getting the errors either while deployment or validation.

The target ORG has RecordType "Contractor" under Contacts
GilzGilz
I have under accounts and contacts