I just installed Daniel Hoechst's test factory into my sandbox and am trying to use it to create test data for the following objects:

  • User
  • CollaborationGroup
  • PermissionSet

I'm getting the following error when creating users: System.SObjectException: Invalid field Name for User

I tried adding a 'UserDefaults' method to set username as follows, but that isnt helping either.

UserDefaults Method in TestFactory

public class UserDefaults implements FieldDefaults{
        public Map<String,Object> getFieldDefaults() {
            return new Map<String, Object> {
                'Username' => 'test'+system.now()+'@ashoka.org',
                'Email'=>  'testemail'+system.now()+'@ashoka.org'

Note that I updated the line in my test method that creates the users as follows:

User[] userList = (User[])TestFactory.createSObjectList(new User(),10, 'TestFactory.UserDefaults', TRUE);

Also, would like clarification on if there is a way to ensure that each name field is incremented by a number when creating a list of records, or whether that is taken care of automatically. Say on a list of users, firstname1, firstname2, and so on.

Thanks! Getting the test factory figured out is going to make me much better at writing tests!

1 Answer 1


In the property called nameFieldMap, I think you just need to map the object to a field that exists on the User object that will be used in incrementing.

private static Map<String, String> nameFieldMap = new Map<String, String> {
    'Contact' => 'LastName',
    'Case' => 'Subject',
    'User' => 'UserName'

The purpose of this map is to identify the field that should get a counter on it when creating a list of sObjects. This is done here:

// Get the name field for the object
    String nameField = nameFieldMap.get(String.valueOf(sObj.getSObjectType()));
    if (nameField == null) {
        nameField = 'Name';

    // Clone the object the number of times requested. Increment the name field so each record is unique
    for (Integer i = 0; i < numberOfObjects; i++) {
        SObject clonedSObj = newObj.clone(false, true);
        clonedSObj.put(nameField, (String)clonedSObj.get(nameField) + ' ' + i);

Edit: Looking at this again, you might have to put some special logic in for users since you can't have a space in the username field and username needs to be unique.

  • thanks - makes sense, as it also needs to be in the form of an email address (which is the error I'm now getting). Would I put the logic into the UserDefaults method/implementation? And any thoughts on how to add an iterator in there? I suppose I could create a separate method just for creating users since that is probably more within my abilities.
    – gorav
    Feb 19, 2016 at 21:54
  • I'd probably put it inside the loop and add a test for object type. If user, append the number to the part before the @ symbol. Feb 19, 2016 at 22:20

You must log in to answer this question.

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