Skip to main content
added 120 characters in body
Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = 'Company Internal'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        List<contact> contactsToCreate = new List<Contact>();
        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;contactsToCreate.add(userCon);
        }
        insert contactsToCreate;
    }
}
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = 'Company Internal'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;
        }
    }
}
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = 'Company Internal'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        List<contact> contactsToCreate = new List<Contact>();
        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            contactsToCreate.add(userCon);
        }
        insert contactsToCreate;
    }
}
added 9 characters in body
Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61
@isTest
private class UserContactTest {
testmethod static void userCreationTest(){
    Account a = new Account(Name = 'Test'Company Account'Internal');
    insert a;

    User u = new User();
    u.Firstname = 'mike';
    u.LastName = 'Smith';
    u.email = '[email protected]';
    u.Alias = 'alias';
    u.CommunityNickname = 'commnick';
    u.EmailEncodingKey = 'UTF-8';
    u.Username = '[email protected]';
    u.TimeZoneSidKey = 'America/Denver';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    u.LanguageLocaleKey = 'en_US';
    u.ProfileId = [SELECT id FROM profile where Name = 'System Administrator'].Id;
 
   insert u;
}
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = '<something>';'Company Internal'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;
            usercon.account.Name = 'Company Internal';
        }
    }
}
@isTest
private class UserContactTest {
testmethod static void userCreationTest(){
    Account a = new Account(Name = 'Test Account');
    insert a;

    User u = new User();
    u.Firstname = 'mike';
    u.LastName = 'Smith';
    u.email = '[email protected]';
    u.Alias = 'alias';
    u.CommunityNickname = 'commnick';
    u.EmailEncodingKey = 'UTF-8';
    u.Username = '[email protected]';
    u.TimeZoneSidKey = 'America/Denver';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    u.LanguageLocaleKey = 'en_US';
    u.ProfileId = [SELECT id FROM profile where Name = 'System Administrator'].Id;
 
   insert u;
}
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = '<something>'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;
            usercon.account.Name = 'Company Internal';
        }
    }
}
@isTest
private class UserContactTest {
testmethod static void userCreationTest(){
    Account a = new Account(Name = 'Company Internal');
    insert a;

    User u = new User();
    u.Firstname = 'mike';
    u.LastName = 'Smith';
    u.email = '[email protected]';
    u.Alias = 'alias';
    u.CommunityNickname = 'commnick';
    u.EmailEncodingKey = 'UTF-8';
    u.Username = '[email protected]';
    u.TimeZoneSidKey = 'America/Denver';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    u.LanguageLocaleKey = 'en_US';
    u.ProfileId = [SELECT id FROM profile where Name = 'System Administrator'].Id;
 
   insert u;
}
public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = 'Company Internal'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;
        }
    }
}
Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61

So, your problem is this line

Account Accountx = [SELECT ID FROM account];

and then

AccountID = AccountX.id,

Since test code cannot see any data, there are no accounts to assign. Make sure you create an account in your test code, like the sample below.

@isTest
private class UserContactTest {
testmethod static void userCreationTest(){
    Account a = new Account(Name = 'Test Account');
    insert a;

    User u = new User();
    u.Firstname = 'mike';
    u.LastName = 'Smith';
    u.email = '[email protected]';
    u.Alias = 'alias';
    u.CommunityNickname = 'commnick';
    u.EmailEncodingKey = 'UTF-8';
    u.Username = '[email protected]';
    u.TimeZoneSidKey = 'America/Denver';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    u.LanguageLocaleKey = 'en_US';
    u.ProfileId = [SELECT id FROM profile where Name = 'System Administrator'].Id;
 
   insert u;
}

However, that will fix your test failure but won't make your code work. In your trigger, you are making a SoQL query within a loop. That is a big no-no.

I am going to go ahead and assume that you do not want to get a "usercon" record created for each account in the system. That means a better way to query, and a way of figuring out which account you want to use.

With that, here is a possible example, assuming all users will go to the same account

public class UserContact {
    @future public static void createUserContact(set<ID> recordIDs){
        list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                            FROM user
                            WHERE ID in :recordIDs];

        String accountName = '<something>'; //This is where you will provide your name (could be any other field)

        //In this query you want to "LIMIT 1" to avoid more than 1 record returning
        Account Accountx = [SELECT ID
                                FROM account
                               where Name = :accountName
                               LIMIT 1];

        for (user x: users){
            Contact userCon = new contact(
            AccountID = AccountX.id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            email = x.Email);
            insert userCon;
            usercon.account.Name = 'Company Internal';
        }
    }
}

My example is almost trivial. Let me know your use case (i.e.: how do you determine the account to choose) and I can improve my sample code.