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
Smike25Smike25 

Cannot_insert_update_activate_entity Help. Simple Trigger

Hello, 

Our ORG was trying to fire workflow rules off formula values.  After seeing that couldn't be done with a WFR, we decided to use a trigger.  The trigger is on the Account and checks the field Activity_Test__c.  If that date changes, we need that date value to change the value in the other field Activity_Status_Date__c.

The Trigger:
trigger CheckActivityDate on Account (before insert, before update) 
{
    for(Account a:Trigger.new)
    {
        if(system.trigger.OldMap.get(a.Id).Activity_Test__c != system.trigger.NewMap.get(a.Id).Activity_Test__c)
        {
            a.Activity_Status_Date__c = a.Activity_Test__c;
        }
    }
}
Upon running that it works correctly and is covered 75% through other test classes.  The only line that wasn't covered is "a.Activity_Status_Date__c = a.Activity_Test__c;" which is what i'm assuming my problem lies.  Do I need to build a standalone test class for this trigger or modify an existing one?  It is causing the other triggers to error with "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CheckActivityDate: execution of BeforeInsert"
 
@Karanraj@Karanraj
Kevin,
You can't use trigger.old or trigger.old map in the trigger insert operation.
Trigger.oldMap is only available in the update and delete trigger.
 
trigger CheckActivityDate on Account (before insert, before update) 
{
    for(Account a:Trigger.new)
    {
        if(Trigger.isUpdate && system.trigger.OldMap.get(a.Id).Activity_Test__c != system.trigger.NewMap.get(a.Id).Activity_Test__c)
        {
            a.Activity_Status_Date__c = a.Activity_Test__c;
        }
    }
}
Add the condition to check only in the update operation
 
Smike25Smike25
Thanks for the explanation and the help S.Karanraj!
sumit singh 37sumit singh 37
You  cannot use trigger.old in insert operation .
use  Trigger.isupdate and trigger.isInsert to saperate the action .and write two action one for insert and other for update
And for test class write two different method for insert and update
 
trigger CheckActivityDate on Account (before insert, before update) 
{
    for(Account a:Trigger.new)
    {
	    if(Trigger.insert)
		{
		 // Add your action 
		}
	
        if(Trigger.isUpdate && system.trigger.OldMap.get(a.Id).Activity_Test__c != system.trigger.NewMap.get(a.Id).Activity_Test__c)
        {
            a.Activity_Status_Date__c = a.Activity_Test__c;
        }
    }
}

 
Smike25Smike25
Still having a problem trying to accomplish the ultimate goal.  When someone sends and email or logs a call we want that activity to populate Today() into the field of Activity_Test__c.  Then have the trigger automatically stamp that date into the field Activity_Status_Date__c.  Are we going about this trigger incorrectly?