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
TsvetyTsvety 

Can't activate a trigger after deactivating it

Hi guys, 
I run into the following issue.

1) I tried to move a new custom object from Sandbox to Production using the change sets
2) When validating I got 2 errors with two of Triggers that don't belong to this object.
3) Read online that the solution is to disable the triggers in Production (I did this by disabling them in sandbox, transferring them to Production using change sets)
4) Did this and with no problem the new custom object passed the test and I was able to deploy it in production
5) As a next step I wanted to activate the triggers in Production so I went to sandbox, activated them, put them in a change set and tried to move them.
Unfortunately when I try to deploy them they don't pass the validation test. 

Before deactivating them the triggers worked perfectly and I didn't touch any of the code.

Here are the errors I am getting:

Class Name: datafloTriggerTest DemoRequestTriggerTest
Method Name: datafloTriggerTest DemoRequestTriggerTest
Error Message: System.DmlException: Upsert failed. First exception on row 0 with id a1n12000000UDgCAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DemoRequestTrigger: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00T1200001lvs5vEAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Not allowed to Delete Tasks. Please Contact an Admin to Delete.: [] Trigger.DemoRequestTrigger: line 438, column 1: [] 
Stack Trace: Class.datafloTriggerTest.DemoRequestTriggerTest: line 308, column 1

Class Name: datafloTriggerTest
Method Name: taskDeleteTriggerTest
Error Message: datafloTriggerTest taskDeleteTriggerTest System.DmlException: Delete failed. First exception on row 0 with id 00T1200001lvs5wEAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Not allowed to Delete Tasks. Please Contact an Admin to Delete.: [] 
Stack Trace: Class.datafloTriggerTest.taskDeleteTriggerTest: line 193, column 1

Here is one of the Apex trigger code (datafloTriggerTest). It doesn't allow users from deleting Activities after recorded on the Contact object:

1 trigger TaskTrigger on Task (before delete, before insert) 
2{
3//** Before Insert Trigger **************************************************************//
4   if ((trigger.isBefore) && (trigger.isInsert))
5   {
6       for (Task t : trigger.new)
7        {
8            if (t.Type=='Call')
9            {
10                t.Type='Auto Dialer Call';
11            }
12            // Marketo User
13            if (t.CreatedById=='005A0000001Nrgm')
14            {
15                t.Type='Marketing';
16            }
17        }
18    }   
19    
20//** Before Delete Trigger **************************************************************//
21    if ((trigger.isBefore) && (trigger.isDelete))
22    {
23        Profile myProfile;
24        try
25        {
26            myProfile = [SELECT Id,Name
27                        FROM Profile WHERE Id=:UserInfo.getProfileId()];
28        }catch (Exception E){myProfile = new Profile();}
29        
30        for (Task t : trigger.old)
31        {
32            // Non Sys-Admins not allowed to Delete
33            if (myProfile.Name!='System Administrator')
34            {
35                t.addError('Not allowed to Delete Tasks.  Please Contact an Admin to Delete.');
36          }
37        }
38    }
39}
Best Answer chosen by Tsvety
SarfarajSarfaraj
Yes this is the reason. In case it is not possible to perform this deployment with profile named 'System Administrator', you may want to modify the trigger code to this,
if (myProfile.Name!='System Administrator' && myProfile.Name !='System Admin with XYZ Homepage')
Only impact for this that from now on you and anyone with your profile will be able to delete tasks. This trigger prevents tasks from deletion by anyone other than 'System Administrator'.
 

All Answers

Kunal GhoshKunal Ghosh
Check your test classes.. There is some validation related to the new custom object. 

Add the related code to your test classes
TsvetyTsvety
Thanks for taking the time to reply. 
My new custom object has no validation rules related to activities so this can't be the problem.
Anything else I have to check?
SarfarajSarfaraj
Is your profile name 'System Administrator'? If no, then probably in the corresponding test class is attempting to delete a task and causing the validation exception. Please login as System Administrator to deploy the code.
If your profile name is 'System Administrator' then please post the code for test class datafloTriggerTest.
TsvetyTsvety
Yes, I am a System Admin, but the name of my profile is "System Admin with XYZ Homepage", not exactly "System Administrator". Do you think that this is what causes the problem? 
SarfarajSarfaraj
Yes this is the reason. In case it is not possible to perform this deployment with profile named 'System Administrator', you may want to modify the trigger code to this,
if (myProfile.Name!='System Administrator' && myProfile.Name !='System Admin with XYZ Homepage')
Only impact for this that from now on you and anyone with your profile will be able to delete tasks. This trigger prevents tasks from deletion by anyone other than 'System Administrator'.
 
This was selected as the best answer
TsvetyTsvety
Thank you so much! I will ask one of the other system admins to deploy it and will report back if it worked. 
TsvetyTsvety
Akram,
I changed my profile and it worked! Thanks so much for your help!
SarfarajSarfaraj
You are welcome