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
Shaowy-wowyShaowy-wowy 

Apex test class not applying to trigger, can't save to server.

I have setup a trigger to prevent users with a certain profile from deleting tasks. My trigger won't save to the server due to 0% test coverage, but I based on my understanding, my tyest code is testing the trigger just fine.

Here's my code:

trigger TaskNoDelete on Task (before delete) {

for (Task t : Trigger.old)
{
  if (UserInfo.getProfileId() == '00e600000015vZp')
  {
   t.addError('You are not authorized to delete tasks.');
  }
}
}



@isTest(SeeAllData=true)
public with sharing class TestTaskTriggers {

static testmethod void updatecharge() {

  User u = new User(alias = 'standt', email='standarduser@testorg.com',
  emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
  localesidkey='en_US', profileid = '00e600000015vZp', Center__c = 'Las Vegas, NV',
  timezonesidkey='America/Los_Angeles', username='shalstesteruser@testorg.com');
  insert u;

  Lead l = new Lead(Web_Salutation__c = 'Mrs.', LastName = 'Test',
  Gender__c = 'Female', LeadSource = 'Web Registrant',
  Referral_Source__c = 'Test Referral');

  Task task = new Task();
  task.Subject = 'Test Subject';
  task.ActivityDate = date.newInstance(2100, 4, 22);
  task.OwnerId = u.id;
  task.whatId = l.id;
  insert task;

  System.runAs(u)
  {
   try
   {
    delete task;
   }
   catch (DMLException e)
   {
    task.addError(e);
   }
  }
}


}




Any help is appreciated.
Bradley DelauneBradley Delaune
Shaowy,
You cannot write or save triggers in a Production Environment.  You must write the code in a sandbox and deploy to production via Change Sets or another Metadata API tool.

Your code likely works, but you'll have to deploy from a sandbox.
NehalNehal (Salesforce Developers) 
Hi,

The reason being you test class is not getting covered because of the hard coded Profile Id. When you hard code the Id's, it take 18 digit Id in picture rather than 15 digit. So it is better if instead of hardcoding Id's you do SOQL query to retrieve the Id's because that is always retrieved as 15 digit.

Kindly look into below test class and modify it accordingly. Your test class will get covered.

@isTest(SeeAllData=true)

Public class TaskNoDeleteTestClass {

static testMethod void insertTask() {

User u= new User();

u.FirstName='Nehal';
u.LastName='Jain';
u.Alias='njain';
u.Username='nehal.jain1@gmail.com';
u.Email='nehal.jain1@gmail.com';
u.CommunityNickname='njs';
u.ProfileId='00e900000010bnWAAQ';
u.TimeZoneSidKey='America/Denver';
u.LocaleSidKey= 'en_US';
u.EmailEncodingKey='UTF-8';
u.LanguageLocaleKey='en_US';

insert u;

Lead l= new Lead();
l.FirstName='MyLead';
l.LastName='Lead';
l.Company='SFDC';
l.Status='Working';

insert l;



Task t= new Task();
t.OwnerId=u.Id;
t.WhoId=l.Id;

t.Status='Progress';
t.Subject='call';
t.Priority='High';

insert t;

System.runAs(u) {
try
   {
    delete t;
   }
   catch (Exception e) {
   system.debug('You are not authorized to delete tasks');
   // Boolean expectedExceptionThrown =  e.getMessage().contains('You are not authorized to delete tasks') ? true : false;
   //        System.assertEquals(expectedExceptionThrown , true);
       }
       }



}

}


MY Trigger:
==========
trigger TaskNoDelete on Task (before delete) {

for (Task t : Trigger.old)
{
  System.debug('!@!@!@!#!#!#$');
  System.debug('@@@@@@@@ ' + UserInfo.getProfileId());
  if (UserInfo.getProfileId() == '00e900000010bnWAAQ')
  {
   System.debug('!@!@!@!#!#!#$');
   t.addError('You are not authorized to delete tasks.');
  }
}
}

I hope this helps.

Please mark this as a "Best Answer" if this has resolved your issue.