+ Start a Discussion
Claire SunderlandClaire Sunderland 

I am trying to de-activate a trigger and push to production but I am getting a fail on my apex class. It is erroring on line 29, column 1.

Here is the error: System.AssertException: Assertion Failed: Expected: 0050f000008wRZYAA2, Actual: 0050f000009O7CcAAK 
Stack Trace: Class.TestRROutboundYoga.testRROutboundInsertPriority: line 29, column 1

Here is the class:
@isTest
private class TestRROutboundYoga {

//-- Tests RoundRobinLeadRoutingOutbound.trigger
    static testMethod void testRROutboundInsertPriority(){
        Profile prof = [select Id from Profile where Name = 'Outbound Sales Yoga'];
        
        //ensure existing users are not available for round robin
        List<User> existingUsers = [select Id from User where ( Profile.Name = 'Outbound Sales Yoga' ) and isActive = true and Available_for_Leads__c = true];
        for(User u : existingUsers){
            u.Available_for_Leads__c = false;
        }
        update existingUsers;
        
        User salesUsr = new User(FirstName = 'Outbound', LastName = 'Sales', Alias = 'out', UserName='test@roundrobin.com', ProfileId = prof.Id, Available_for_Leads__c = true,
                                 LanguageLocaleKey='en_US', Email='test@roundrobin.com', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles');
        insert salesUsr;
        
        Test.startTest();
        Lead lead = new Lead(FirstName = 'Round', LastName = 'Robin', Company = 'Round Robin Test', Status = 'Prospect', Vertical_Industry__c = 'Yoga',
                             Email = 'test@rr.com', Phone = '4085551234', Country = 'US', Street = '123 Billing Street', City = 'Davis', State = 'CA', PostalCode = '11111',
                             Website = 'website@domain.com', Number_of_Members__c = 64);
        insert lead;
        Test.stopTest();
        
        //requery and assert lead was assigned to user and last lead assignment updated
        lead = [select Id, OwnerId, Owner.Name from Lead where Id = :lead.Id];
        salesUsr = [select Id, Last_Lead_Assignment__c from User where Id = :salesUsr.Id];
        system.assertEQUALS(lead.OwnerId,salesUsr.Id);
        system.assertEquals(salesUsr.Last_Lead_Assignment__c.Date(), system.today());
    }
    
    
    //-- Tests RoundRobinLeadRoutingOutbound.trigger
    static testMethod void testRROutboundUpdatePriority(){
        Profile prof = [select Id from Profile where Name = 'Outbound Sales Yoga'];
        
        //ensure existing users are not available for round robin
        List<User> existingUsers = [select Id from User where ( Profile.Name = 'Outbound Sales Yoga' OR Profile.Name = 'Outbound Team Leader' ) and isActive = true and Available_for_Leads__c = true];
        for(User u : existingUsers){
            u.Available_for_Leads__c = false;
        }
        update existingUsers;
        
        User salesUsr = new User(FirstName = 'Outbound', LastName = 'Sales', Alias = 'inb', UserName='test@roundrobin.com', ProfileId = prof.Id, Available_for_Leads__c = true,
                                 LanguageLocaleKey='en_US', Email='test@roundrobin.com', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles');
        insert salesUsr;
        
        Lead lead = new Lead(FirstName = 'Round', LastName = 'Robin', Company = 'Round Robin Test', Vertical_Industry__c = 'Yoga', Email = 'test@rr.com', Phone = '4085551234',
                             Country = 'US', Street = '123 Billing Street', City = 'Davis', State = 'CA', PostalCode = '11111', Website = 'website@domain.com', Number_of_Members__c = 64);
        insert lead;
        
        Test.startTest();
        lead.Status = 'Prospect';
        update lead;
        Test.stopTest();
        
       
       }
       }


AND HERE IS THE TRIGGER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
trigger RoundRobinLeadRoutingOutboundYoga on Lead (before insert, before update) {
  
  try{
    //lists to hold all leads of relevant status
    List<Lead> lowLeads = new List<Lead>();
    
    //lists to hold only those leads which should be round robined
    List<Lead> roundRobinLow = new List<Lead>();
    
    Map<Id, User> userMap = new Map<Id, User>(); //map user info by Id
    //lists holding eligible users in order of lead assignment
    List<LowPrioritySort> lowUsers = new List<LowPrioritySort>(); //wrapper class for users implements compareTo on last low priority lead assignment
    List<User> mqlUsers = new List<User>();
    
    //set and list to ensure unique users to update
    Set<Id> userSet = new Set<Id>();
    List<User> userLst = new List<User>();
    
    //if trigger is insert, collect leads inserted with a relevant status
    if(trigger.isInsert){
      for(Lead lead : trigger.new){
        if(lead.Vertical_Industry__c == 'Yoga' && (lead.Status == 'Prospect') ){
          lowLeads.add(lead);
        }
      }
    }
    //if trigger is update, collect leads which have changed to a relevant status
    else if(trigger.isUpdate){
      for(Lead lead : trigger.new){
        if(trigger.oldMap.get(lead.Id).Status != lead.Status ||trigger.oldMap.get(lead.Id).Vertical_Industry__c != lead.Vertical_Industry__c ){
        if(lead.Vertical_Industry__c == 'Yoga' && (lead.Status == 'Prospect') ){
            lowLeads.add(lead);
          }
        }
      }
    }
    
    
    //if leads have been updated to any of the relevant statuses they may be eligible for round robin
    if(lowLeads.size() > 0){
      //query additional user info
      List<User> users = [select Id, ProfileId, Profile.Name, isActive, Available_for_Leads__c, Last_Lead_Assignment__c,
                Last_Low_Priority_Lead_Assignment__c from User
                order by Last_Lead_Assignment__c asc nulls first];
      
      //create a map of all users by Id
      for(User u : users){
        userMap.put(u.Id, u);
      }
      
      //assign users to list of outbound sales agents who are avail for leads
      for(User u : users){
        if( ( u.Profile.Name == 'Outbound Sales Yoga') && u.isActive == true && u.Available_for_Leads__c == true){
          lowUsers.add(new LowPrioritySort(u));
        }
      }
      
      //sort lowUsers list by last low priority lead assignments
      lowUsers.sort();
    }
      
    
    //only round robin low priority leads not already owned by outbound rep
    if(lowLeads.size() > 0){
      for(Lead lead : lowLeads){
        //if lead not already owned by outbound sales agent, add to list to round robin
        if( ( userMap.get(lead.OwnerId).Profile.Name != 'Outbound Sales Yoga') && (userMap.get(lead.OwnerId).Profile.Name != 'Inbound Sales')){
          roundRobinLow.add(lead);
        }
      }
    }
    
    /* ROUND ROBIN LOW PRIORITY LEADS */
    if(roundRobinLow.size() > 0){
      //round robin to available outbound sales agents
      if(lowUsers.size() > 0){
        Integer userIndex = 0; //initialize index
        for(Lead lead : roundRobinLow){
          //assign to current user indexed
          User currUser = lowUsers[userIndex].usr;
          lead.OwnerId = currUser.Id;
          currUser.Last_Lead_Assignment__c = system.now(); //update last lead assignment
          if(!userSet.contains(currUser.Id)){ //check if already in list
            userSet.add(currUser.Id);
            userLst.add(currUser);
          }
          userIndex++; //advance to next user
          if(userIndex >= lowUsers.size()){
            userIndex = 0; //reset index
          }
        }
      }
    }
    
    
    
    //update users if necessary
    if(userLst.size() > 0){
      update userLst;
    }
  }
  catch(Exception e){
    system.debug('EXCEPTION in RoundRobinLeadRouting.trigger: ' + e);
  }
}


Can anyone help me with fixing this line?
GarryPGarryP
Hi Claire,

As per error, your test class is failing due to assert statement
Here is the error: System.AssertException: Assertion Failed: Expected: 0050f000008wRZYAA2, Actual: 0050f000009O7CcAAK 

The ID that you are trying to is not matching hence error-
        system.assertEQUALS(lead.OwnerId,salesUsr.Id);
However as per your code comments it looks like assertion is correct but the lead is not getting assgned to USER hence owner Ids are not matching.

You will have to look at your Assignment rule and see why lead is not getting assigned to LOGGED in user.

Regarrds
Girish P