You need to sign in to do that
Don't have an account?
Apex Test Class Help (Query And Update)
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(); } }
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.
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. 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.
Thanks,
Nagendra