+ Start a Discussion
Claire SunderlandClaire Sunderland 

System.AssertException Error.

I'm getting this error when I try to deploy my test class to production: System.AssertException: Assertion Failed: Expected: 005G0000008Mb3iIAC, Actual: 0050f000009KoD2AAK  Stack Trace: Class.TestRRMQL.testRRMQLInsertPriority: line 29, column 1. The actual is coming back as data not available and expected coming back as my user.

Can anyone help?

Here is the class: 

@isTest
private class TestRRMQL {

//-- Tests RoundRobinLeadRoutingMQL.trigger
    static testMethod void testRRMQLInsertPriority(){
        Profile prof = [select Id from Profile where Name = 'Outbound Sales'];
        
        //ensure existing users are not available for round robin
        List<User> existingUsers = [select Id from User where ( Profile.Name = 'Outbound Sales' OR Profile.Name = 'Assoc Account Executive' ) 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 = 'MQL Re-engage', Vertical_Industry__c = 'Pilates',
                             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 RoundRobinLeadRoutingMQL.trigger
    static testMethod void testRRMQLUpdatePriority(){
        Profile prof = [select Id from Profile where Name = 'Outbound Sales'];
        
        //ensure existing users are not available for round robin
        List<User> existingUsers = [select Id from User where ( Profile.Name = 'Outbound Sales' OR Profile.Name = 'Assoc Account Executive') 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 = 'Pilates', 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 = 'MQL Re-engage'; 
        update lead;
        Test.stopTest();
        
       
       }
       }
@Amit Kumar Giri@Amit Kumar Giri
I think its the sales user, not the lead owner resulting it to failed. Actual is 0050f000009KoD2AAK . check which user it is, definitely its not ur user. for salesUsr.Id u need to put a debug and see if this is having above id or not. Without main class i can think of this by seeing the error and below 2 line
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];

 
Claire SunderlandClaire Sunderland
When I check which user it is I just get 'data not available'. Not sure how to put a debug on it.
Claire SunderlandClaire Sunderland
Here's the trigger if that helps:


trigger RoundRobinLeadRoutingMQL 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.Status == 'MQL Re-engage') ){
          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){
          if((lead.Status == 'MQL Re-engage')){
            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' || u.Profile.Name == 'Assoc Account Executive' ) && 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' && userMap.get(lead.OwnerId).Profile.Name != 'Assoc Account Executive') ){
          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);
  }
}