0

Here it is the Trigger on Event Activity

trigger eventHistoryChecker on Event (after update) 
{
    for (Event evt : Trigger.new)
    {
        Event oldevt = Trigger.oldMap.get(evt.id);
        String oldSub = oldevt.Subject;
        string newSub = evt.Subject;
        if(newSub.equals(oldSub)) { }
        else evt.addError('Can not change Subject Field');
    }
}

Here it is the Test Class for this Trigger but i am not sure its correct or not plz help me to resolve the problem

@isTest
public class eventHistoryChecker_TC
{
    static testmethod void TestMeth()
    {
        Event e=new Event();
        e.subject='Call';
        e.Ownerid='005900000037wZv';
        e.EndDateTime=System.Today();
        e.StartDateTime=System.Today();
        insert e;
    }
}
2
  • 2
    Your trigger fires on after update but you never update the Event only insert it. You will need to change the Subject field once its inserted
    – BarCotter
    Commented Dec 5, 2014 at 10:35
  • Thanks a lot for your help.Gotcha your comment got answer.
    – Krish25
    Commented Dec 5, 2014 at 10:46

2 Answers 2

2

Your trigger is an after upadate trigger. So you have to perform an update DML to fire the trigger. Read some documentations to get more information about the trigger contexts.

  1. Trigger Context Variables
  2. Context Variable Considerations

All you are doing fine just update the Event object to fire the trigger and test it.

@isTest
public class eventHistoryChecker_TC
{

    static testmethod void TestMeth()
    {
        Event e=new Event();
        e.subject='Call';
        e.Ownerid='005900000037wZv';
        e.EndDateTime=System.Today();
        e.StartDateTime=System.Today();
        insert e;

        e.subject='New One'; 
        update e;

        // add some assertEquals based on your requirement
    }
}
0

Telling you how to fix your code is missing the key takeaway. Just use a validation rule.

ISCHANGED(Subject)

Advantages:

  • No need to unit test
  • Cannot throw any exceptions
  • Easy and quick to change error message

I want to point out some of the issues with your code though so you can avoid them in future projects.

What happens if an end user sets the new Subject to null?

System.NullPointerException: Attempt to de-reference a null object

What happens if the production environment does not have a User with the specified Id? Or if you just want to run your test successfully in a developer environment?

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

I recommend you look at using the Selector library to take care of filtering logic for you.

If you feel you must roll your own filters, you should at least split it out into static methods and separate the filter logic from the validation logic.

public static List<Event> hasSubjectChanged(List<Event> newEvents, Map<Id, Event> oldMap)
{
    List<Event> changed = new List<Event>();
    for (Event newEvent : newEvents)
    {
        if (newEvent.Subject != oldMap.get(newEvent.Id).Subject) changed.add(newEvent);
    }
    return changed;
}

public static void validateSubjectChange(List<Event> invalidEvents)
{
    for (Event invalidEvent : invalidEvents)
        invalidEvent.addError('Cannot change Subject Field');
}

These methods would be easy to call from your trigger:

validateSubjectChange(hasSubjectChanged(trigger.new, trigger.oldMap));

If you use Selector:

validateSubjectChange(
    Select.Field.hasChanged(Event.Subject).filter(trigger.new, trigger.oldMap)
);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .