-2

I have an Apex class on Custom Object Called QueueDistroHelper__c

What I am basically doing is getting the Queue Id from a custom field(Related_Queue_Id__c) on the Custom object

Querying all the users in that queue

And updating another custom field(Queue_Members__c ) with all the users in that Queue

My apex class is working fine

Here is the apex class -

    global class lookUpAccountAnnotation {
public class inputValues{
        @InvocableVariable(label='Queue ID' )
        public string QueueId;
        @InvocableVariable(label='Record Id' )
        public Id recordid;
        public string temp;
    }
   @InvocableMethod
    public static void deletePackageLicense(List<inputValues> inputs){

      for (inputValues i : inputs)
   {
     List<QueueDistroHelper__c> getcurrentobject =  [SELECT Queue_Members__c ,  Next_Queue_Reassignment_User__c  FROM QueueDistroHelper__c WHERE Id=:i.recordid ];

      List<User> users =  [SELECT
                    name
                FROM
                    user
                WHERE
                    id IN ( SELECT userOrGroupId FROM groupmember WHERE groupId =:i.QueueID)
                    AND
                    isActive = true

                ];
      for ( User test : users){
      if(getCurrentObject != null && !getCurrentObject .isEmpty()) {
if(getCurrentObject[0].Queue_Members__c == null)
   getCurrentObject[0].Queue_Members__c = '';
getCurrentObject[0].Queue_Members__c = getCurrentObject[0].Queue_Members__c + ' ' + test.name;
}

getCurrentObject[0].Next_Queue_Reassignment_User__c =+ users.get(1).name;
getCurrentObject[0].Last_Queue_Reassignment_User__c =+ users.get(0).name;


update getcurrentobject[0];


}   
      }
   }
}

I am trying to make a test class for the same , Here are the steps I am doing for testing

  1. Creating 4 test users

  2. Adding those users in a test queue

  3. Updating my fields on my Custom object with User name and Queue ID

Not sure if its the right approach

Here is my test Class -

@isTest
private class lookUpAccountAnnotation_Test{
   @TestSetup
     static void setup() {    
    // Setup 4 Test Users
 Group grpObj = new Group(Type='Queue', Name='Test Queue1');
insert grpObj;
QueueSObject qObj = new QueueSObject(SobjectType='Lead', QueueId=grpObj.Id);
insert qObj;
    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
    List<User> users = new List<User>();

    while (users.size() < 5) {
      Blob b = Crypto.GenerateAESKey(128);
      String h = EncodingUtil.ConvertTohex(b);
      String uid = h.SubString(0,8);
      //insert 4 test users
      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');      
         ID gr = [SELECT Id FROM Group WHERE Name = 'Test Queue1'].Id;
         ID us = [SELECT Id , Name FROM User WHERE LastName='Testing'].Id;
         //Add one user in a test queuecreated above
        GroupMember gm = new GroupMember(GroupId = gr, UserOrGroupId = us);
           insert gm;
    }
System.Test.startTest();

QueueDistroHelper__c q = new QueueDistroHelper__c();
insert q;
//insert user name and Queue ID  in custom Object  
q.Related_Queue_Id__c = grpObj.Id;
q.Queue_Members__c = users.get(1).Name;
lookUpAccountAnnotation l = new lookUpAccountAnnotation();
//queryHelper q = new QueueDistroHelper__c(Name='TestQuery' , Queue_Members__c=users.name);    

System.Test.stopTest();

}
}
3
  • What's your question exactly? Is something not working?
    – Adrian Larson
    Commented Jan 22, 2019 at 19:38
  • So its my test class which is not working
    – richie
    Commented Jan 22, 2019 at 20:41
  • 2
    Please be more specific than "not working" or it will be nearly impossible to help you. Please add any clarifications via edit.
    – Adrian Larson
    Commented Jan 22, 2019 at 20:48

1 Answer 1

2

Your code works.. Its not bulkified, and would fail in bulk tests. Also, there are no asserts in your code so I would call test class as useless as you are not testing behavior but just doing pseudocode coverage. Let's tackle one problem as of now. Bulkification of the main class.

  1. You are doing DML and SOQL in for-loops. A big No and red flag. SF has limits, of 100SOQL and 150DML in 1 transaction. So if lookUpAccountAnnotation.inputValues is called with more than 100 records it will break.

    Use bulk pattern and gather data first and then do a single SOQL using IN Clause

    You can read more about bulkification here.

    Set<String> emailAddresses = new Set<String>();
    
    for (Invoice__c invoice: listOfInvoices){
    
      if (invoice.Status__c == 'Overdue'){
    
        emailAddresses.add(invoice.Email__c); 
    
      } 
    }
    
    // here we invoke a query. this also has a governor limit.
    // up to 100 of these in a transaction before it throws an exception
    
    List<Contact> contactsOverdue = [SELECT Id, Name 
                                     FROM Contact 
                                     WHERE Email in: emailAddresses];
    
    1. Test class: You are not actually calling the deletePackageLicense method in your code. Thus your actuall method would never be called and you are writing test for nothing. You have to write test class in bulkified manner, and then test the Behavior. The test method should cover positive , negative and bulk scenarios to ensure your code won't break. This is a great starting point to learn about Apex Tests.
3
  • Thanks a lot. Thats a good catch . I will update that
    – richie
    Commented Jan 22, 2019 at 20:42
  • Sorry, Just a small doubt . What would be the best way to call my method in the code. I tried to call the method. but test still says '0 methods called '
    – richie
    Commented Jan 22, 2019 at 21:01
  • @richie I cant see you calling lookUpAccountAnnotation.deletePackageLicense(values); Commented Jan 22, 2019 at 21:03

You must log in to answer this question.

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