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
KevSnellKevSnell 

TestMethod Help - Logic seems right but getting 0%

Hey All,


Not sure what is going on here especially as it's a simple trigger.

 

As you can see the trigger flags an error if anyone other than a Systems Administrator tries to delete a Completed Task.

 

trigger trgr_Task_PreventDelete_bd on Task (Before delete) {

    for (Integer i = 0; i < Trigger.Old.size(); i++) {
        if (Trigger.Old[i].Status == 'Complete' && UserInfo.getProfileId() != '00eE0000000XjEM') {
               // Cannot delete this task
               Trigger.Old[i].addError('This task cannot be deleted because you are not assigned as the owner.  Please contact an administrator.');
        }
    }
}

 

So my logic was create a lead and task and then try to delete while running as a user other than a system admin but it doesn't work.

 

@isTest
private class test_trgr_PreventTaskDelete{
    public static testMethod void testDeleteFail()
    {
        User rep = [Select Profileid from User where Name = 'Test Chair' limit 1];
        System.runAs(rep)
        {
        System.debug('VistageDebug: entering TriggerTests_Campaign_ActiveCampaign_Before_Delete');
        
        Lead l2 = new Lead(OwnerID = '005E0000000YhNn',Status = 'New',Company = 'Test',LastName = 'Test',LeadSource = 'Advertising',Industry = 'Fitness');
        Insert l2;

    //Setup t2 and relate to L2
        Task t2 = new Task(OwnerID = '005E0000000YhNn',Subject = 'test',Type = 'Cold Call',Status = 'Not Started',Priority = 'Normal',WhoId = l2.id);
        insert t2;
    
    //Update t2
        Task updateMe = [SELECT Id FROM Task WHERE Id = :t2.Id];
        updateMe.Status = 'Completed';
        update updateMe;
        
        Task TaskDelete = [SELECT Id FROM Task WHERE Id = :t2.Id];
           try
               {
                delete TaskDelete;
                  // should throw an exception - the following assertion will cause an error if the code carries on
                  System.assert(false);
               }
               catch (DMLException e)
               {
               // expected - could assert the message here
               }
           }
        }
}

 

Any help would be highly appreciated as one moment I think I'm starting to understand things and then the next moment I hit another road block.

 

Thanks

Kevin 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The trigger is looking for a status of "Complete':

 

if (Trigger.Old[i].Status == 'Complete'

 

but in the test class, you set the status to 'Completed':

 

 

 updateMe.Status = 'Completed';

 

All Answers

bob_buzzardbob_buzzard

The trigger is looking for a status of "Complete':

 

if (Trigger.Old[i].Status == 'Complete'

 

but in the test class, you set the status to 'Completed':

 

 

 updateMe.Status = 'Completed';

 

This was selected as the best answer
KevSnellKevSnell

Thanks - what a silly mistake but unfortunately I'm still getting 0% on my test coverage.

 

Kev

KevSnellKevSnell

I found the issue:

 

User rep = [Select Profileid from User where Name = 'Test Chair' limit 1];

 Changed to:

 

User rep = [Select Profileid from User where ProfileId = '00eE0000000XjEM' limit 1];

Now got 100% cover - but I couldn't have figured it out without the spot of the typo from bob_buzzard.