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
Sean GroveSean Grove 

Code Coverage wont change.


Ive created an apex class that pulls in certain leads and then feeds all the IDs into a round robin assigner apex class. Im not a developer but Ive been able to hack together enough to get this running as intended, my only problem now is the text class I have doesnt have enough code coverage to get into production. I assume part of this problem is the List that is created does not have its own method and therefore is hard to create a test for? But agian Im not a developer, Ive included both the class as well as the test code. If someone could point me in the right direction that would be greatly appreciated!

CODE:

public with sharing class RRAOnline {

    list<roundRobinAssigner.RoundRobinAssignment> rras = new list<roundRobinAssigner.RoundRobinAssignment>();

    List<User> users = [SELECT Id FROM User WHERE Lead_Reassignment__c = true AND IsActive = true AND ProfileId = '00e4P000000KDMk'];
 
    List<Lead> leads = [SELECT Id 
                        FROM Lead
                        WHERE OwnerID IN : users
                        AND Next_Business_Date__c <= TODAY 
                        AND (
                        Program_of_Interest__c = 'Animation' OR
                        Program_of_Interest__c = 'Fashion Design' OR
                        Program_of_Interest__c = 'Fine Arts' OR
                        Program_of_Interest__c = 'Game Art' OR
                        Program_of_Interest__c = 'Graphic Design' OR
                        Program_of_Interest__c = 'Illustration' OR
                        Program_of_Interest__c = 'Illustrative Design' OR
                        Program_of_Interest__c = 'Interior Design' OR
                        Program_of_Interest__c = 'Non-Degree Seeking' OR
                        Program_of_Interest__c = 'Photography' OR
                        Program_of_Interest__c = 'Summer Camp' OR
                        Program_of_Interest__c = 'Undeclared' OR
                        Program_of_Interest__c = 'Certificate Program - Graduate'
                        )
                        AND (
                        Status__c = 'New' OR 
                        Status__c = 'Attempting Contact 1' OR
                        Status__c = 'Attempting Contact 2' OR
                        Status__c = 'Attempting Contact 3' OR
                        Status__c = 'Attempting Contact 4' OR
                        Status__c = 'Attempting Contact 5')
                        LIMIT 50
                        ];
                  
     public void createrrassigns(){
        for (lead a:leads){
            roundRobinAssigner.RoundRobinAssignment rra = new roundRobinAssigner.RoundRobinAssignment();
            rra.groupName = 'Online Round Robin';
            rra.recordId = a.id;
            rra.fieldName = 'OwnerId';
            rras.add(rra);
            }
        if (rras.size() > 0){
            roundRobinAssigner.assign(rras);
            } 
         else {
            system.debug('list is empty');
            }
      }


}



TEST CODE:


@isTest
private class RRAOnline_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    
    // Setup 4 Test Leads
    List<Lead> leads = new List<Lead>();

    while (leads.size() < 5) {
      Blob b = Crypto.GenerateAESKey(128);
      String h = EncodingUtil.ConvertTohex(b);
      String uid = h.SubString(0,8);
      Lead l = new Lead(FirstName = 'FirstName'+ uid, LastName = 'LastName'+ uid, Status = 'New Inquiry', Status__c = 'New', Program_of_Interest__c = 'Graphic Design', 
            Next_Business_Date__c = System.today());      
      leads.add(l);
    }
    insert(leads);
    
    
     
    // Setup 4 Test Users
    Profile p = [SELECT Id FROM Profile WHERE Name='Admissions User']; 
    List<User> user_Obj = new List<User>();

    while (user_Obj.size() < 5) {
      Blob b = Crypto.GenerateAESKey(128);
      String h = EncodingUtil.ConvertTohex(b);
      String uid = h.SubString(0,8);
      User u = new User(Alias = uid, Email= uid + '@myorg.com', 
          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
          LocaleSidKey='en_US', ProfileId = p.Id, 
          TimeZoneSidKey='America/New_York', UserName= uid + '@myorg.com', Lead_Reassignment__c = true, IsActive = true);      
      user_Obj.add(u);
    }
    insert(user_Obj);

    test.stopTest();
  }
  
    static testMethod void test_createrrassigns_UseCase1(){
    RRAOnline obj01 = new RRAOnline();
    obj01.createrrassigns();
  }
  
//    static testMethod void test_createrrassigns_UseCase2(){
//    RRAOnline users = new RRAOnline();
//    users.getUsers();
//  }
  

}
Best Answer chosen by Sean Grove
Maharajan CMaharajan C
Hi Sean,

You test class should be like below:
 
@isTest
private class RRAOnline_Test{
	@testSetup
	static void setupTestData(){	
	// use the exact profile name in below line as you reffered from actual class. use the profile name of ProfileId = '00e4P000000KDMk'
		Profile p = [SELECT Id FROM Profile WHERE Name='Admissions User']; 

		User userrec = new User(alias = 'test01', email='test01@testorg.com',
					emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
					localesidkey='en_US', profileid = p.Id,
					timezonesidkey='America/Los_Angeles', username='test01@testorg.com',Lead_Reassignment__c = true);

		insert userrec;

		test.startTest();
		
		Lead l = new Lead(FirstName = 'FirstName', LastName = 'LastName', Status = 'New Inquiry', Status__c = 'New', Program_of_Interest__c = 'Graphic Design', 
						  Next_Business_Date__c = System.today(), OwnerId = userrec.Id);   
		insert l;

		test.stopTest();
	}

	static testMethod void test_createrrassigns_UseCase1(){
		RRAOnline obj01 = new RRAOnline();
		obj01.createrrassigns();
	}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sean,

You test class should be like below:
 
@isTest
private class RRAOnline_Test{
	@testSetup
	static void setupTestData(){	
	// use the exact profile name in below line as you reffered from actual class. use the profile name of ProfileId = '00e4P000000KDMk'
		Profile p = [SELECT Id FROM Profile WHERE Name='Admissions User']; 

		User userrec = new User(alias = 'test01', email='test01@testorg.com',
					emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
					localesidkey='en_US', profileid = p.Id,
					timezonesidkey='America/Los_Angeles', username='test01@testorg.com',Lead_Reassignment__c = true);

		insert userrec;

		test.startTest();
		
		Lead l = new Lead(FirstName = 'FirstName', LastName = 'LastName', Status = 'New Inquiry', Status__c = 'New', Program_of_Interest__c = 'Graphic Design', 
						  Next_Business_Date__c = System.today(), OwnerId = userrec.Id);   
		insert l;

		test.stopTest();
	}

	static testMethod void test_createrrassigns_UseCase1(){
		RRAOnline obj01 = new RRAOnline();
		obj01.createrrassigns();
	}
}

Thanks,
Maharajan.C
This was selected as the best answer
Omar Rajab 94Omar Rajab 94
Hi Sean, 

Does the Profile Id "00e4P000000KDMk" in the class "RRAOnline " belong to "Admissions User"? 

Regards, 
Omar
 
Sean GroveSean Grove
Yes it does
Sean GroveSean Grove
Thank you Maharajan, that worked!!
Sean GroveSean Grove
Actually... it still wont let me deploy to production... I get 100% code coverage in sandbox, but failes for 0% code coverage in change set deployment... any ideas?
Sean GroveSean Grove
Sorry looks like it deployed after all local tests were run, weird