Created a DataFactory class to reuse the code in test classes. I am not able to get the List of users record id into the test class. Can anyone please help me with this?
It is currently throwing Error : Initial term of field expression must be a concrete SObject: List
TestDataFactory Apex class:
@isTest
public class testDataFactory{
public Static List<User> cuser(){
List<User> UsersList = new List<User>();
Profile p = [SELECT Id FROM Profile WHERE Name='Profile1'];
User usr1 = new User(Alias = 'aa', Email='testuser111@testorg.com',
EmailEncodingKey='UTF-8', FirstName='yhsl',LastName='test', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='testuser111@testorg.com');
UsersList.add(usr1);
User usr2 = new User(Alias = 'ab', Email='testuser1112@testorg.com',
EmailEncodingKey='UTF-8', FirstName='iuqwet',LastName='Test', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='testuser1112@testorg.com');
UsersList.add(usr2);
insert UsersList;
return UsersList;
}
}
Test Class:
@isTest
private class TestClass
{
Account Acc = new Account(Name='Test');
Insert Acc;
Acc.ownerId = testDataFactory.cuser().UsersList[0].id; // It is throwing Error : Initial term of field expression must be a concrete SObject: List<User>
update Acc;
}
Best Answer
The class
TestClass
does not have access to theUsersList
variable in the current configuration, sinceUsersList
only has scope in thecuser()
method (that is, it doesn't exist outside of the method). Rather than trying to accessUserList
directly, try accessing the reference to the list that is returned by the method. Changeto