+ Start a Discussion
Aaliya YusufzaiAaliya Yusufzai 

Apex Trigger keeps failing

I need help to somehow break this trigger as It is not allowing me to change ownership of a lead due to activities assigned to users. I'm not a developer and someone had created this Apex Trigger below.  I tried to inactivate this by creating a change set and deploying into production, however, it keeps giving me the following error message when I am deploying: "System.AssertException: Assertion Failed: Event: The Owner Id is not updating: Expected: 005W00000039nhYIAQ, Actual: 005U0000006SDUqRBO 
Stack Trace: Class.LeadOwnerChangeReassignEvent_test.test: line 32, column 1"


The Apex Trigger is as follows:
trigger LeadOwnerChangeReassignEvent on Lead (before insert, before update, after update) {
    //if(Trigger.IsInsert){
    if(Trigger.IsBefore){   
        List<User> u = [Select id, name, username,email from USER where name like '%Bastian Spekker%'];
        for (Lead l : trigger.new){
            String add = (l.City!=null?l.City.toLowercase():'')+'-'+(l.Street!=null?l.Street.toLowercase():'')+'-'+(l.State!=null?l.State.toLowercase():'')+'-'+(l.Country!=null?l.Country.toLowercase():'');
            if(add.contains('Australia') && u!=null && u.size()>0){
                l.OwnerId = u[0].id;    //Assuming Bastian is queried above
                System.debug('***Updating owner to '+l.OwnerId);
               
            }
        }
    }
    if(Trigger.IsUpdate && Trigger.IsAfter){
        List<Event> events;
        List<Task> tasks;
        List<Id> leadIds = new List<Id>();
       
        for (Lead l : trigger.new)
            if(trigger.newMap.get(l.Id).OwnerId != trigger.oldMap.get(l.Id).OwnerId)
                leadIds.add(l.id);
       
        events = [SELECT Id, WhoId, ScheduleOnce__Event_status__c, EndDateTime FROM Event WHERE WhoId in :leadIds];
       
        for (Lead l : trigger.new) {
            for (Event e : events) {
               if (l.Id == e.WhoId && ( e.EndDateTime < date.Today() ||
                   ( e.ScheduleOnce__Event_status__c != 'Completed'
                     && e.ScheduleOnce__Event_status__c != 'Canceled (rescheduled by Customer)'
                     && e.ScheduleOnce__Event_status__c != 'Canceled by Customer'
               && e.ScheduleOnce__Event_status__c != 'Canceled (reschedule requested by Owner)'
                     && e.ScheduleOnce__Event_status__c != 'Canceled by Owner'
                     && e.ScheduleOnce__Event_status__c != 'No-Show'
                  ))) e.OwnerId = l.OwnerId;
            }
        }
        if (events.size() != 0) update events;
 
      tasks = [SELECT Id, WhoId, isClosed FROM Task WHERE WhoId in :leadIds];
       
        for (lead l : trigger.new) {
            for (Task t : tasks) {
                if (l.id == t.WhoId && !t.isClosed) {
                    t.OwnerId = l.OwnerId;               
                }
            }
        }
        if (tasks.size() != 0) update tasks;
    }
}
trigger LeadOwnerChangeReassignEvent on Lead (before insert, before update, after update) {
    //if(Trigger.IsInsert){
    if(Trigger.IsBefore){   
        List<User> u = [Select id, name, username,email from USER where name like '%Bastian Spekker%'];
        for (Lead l : trigger.new){
            String add = (l.City!=null?l.City.toLowercase():'')+'-'+(l.Street!=null?l.Street.toLowercase():'')+'-'+(l.State!=null?l.State.toLowercase():'')+'-'+(l.Country!=null?l.Country.toLowercase():'');
            if(add.contains('Mexico') && u!=null && u.size()>0){
                l.OwnerId = u[0].id;    //Assuming Bastian is queried above
                System.debug('***Updating owner to '+l.OwnerId);
               
            }
        }
    }
    if(Trigger.IsUpdate && Trigger.IsAfter){
        List<Event> events;
        List<Task> tasks;
        List<Id> leadIds = new List<Id>();
       
        for (Lead l : trigger.new)
            if(trigger.newMap.get(l.Id).OwnerId != trigger.oldMap.get(l.Id).OwnerId)
                leadIds.add(l.id);
       
        events = [SELECT Id, WhoId, ScheduleOnce__Event_status__c, EndDateTime FROM Event WHERE WhoId in :leadIds];
       
        for (Lead l : trigger.new) {
            for (Event e : events) {
               if (l.Id == e.WhoId && ( e.EndDateTime < date.Today() ||
                   ( e.ScheduleOnce__Event_status__c != 'Completed'
                     && e.ScheduleOnce__Event_status__c != 'Canceled (rescheduled by Customer)'
                     && e.ScheduleOnce__Event_status__c != 'Canceled by Customer'
               && e.ScheduleOnce__Event_status__c != 'Canceled (reschedule requested by Owner)'
                     && e.ScheduleOnce__Event_status__c != 'Canceled by Owner'
                     && e.ScheduleOnce__Event_status__c != 'No-Show'
                  ))) e.OwnerId = l.OwnerId;
            }
        }
        if (events.size() != 0) update events;
 
      tasks = [SELECT Id, WhoId, isClosed FROM Task WHERE WhoId in :leadIds];
       
        for (lead l : trigger.new) {
            for (Task t : tasks) {
                if (l.id == t.WhoId && !t.isClosed) {
                    t.OwnerId = l.OwnerId;               
                }
            }
        }
        if (tasks.size() != 0) update tasks;
    }
}
 
Best Answer chosen by Aaliya Yusufzai
Manish BhatiManish Bhati
Hi Aaliya,

The Test class for this trigger is Failing.

- Goto Apex Class from quick find box
- Search for LeadOwnerChangeReassignEvent_test by clicking on L Alphabet.
- Goto Line No. 32.
- Comment this line by using //

Hope this helps.

All Answers

Manish BhatiManish Bhati
Hi Aaliya,

The Test class for this trigger is Failing.

- Goto Apex Class from quick find box
- Search for LeadOwnerChangeReassignEvent_test by clicking on L Alphabet.
- Goto Line No. 32.
- Comment this line by using //

Hope this helps.
This was selected as the best answer
Aaliya YusufzaiAaliya Yusufzai
It worked!!! Thank you so much Manish