Another one of these, I know. I have been looking at similar errors for the last few hours with no luck. I tried using a "Try" "Catch" workaround with no luck, I tried using an Array, I tried making everything visible to the test class. I had no luck with anything, so hopefully someone here can help me out!
From what I understand, I'm getting the error because my list is returning no values, and that isn't allowed. How would I get around this in my particular case?
Here is my CLASS
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];
for (user x: users){
Account Accountx = [SELECT ID
FROM account];
Contact userCon = new contact(
AccountID = AccountX.id,
FirstName = x.FirstName,
LastName = x.LastName,
email = x.Email);
insert userCon;
usercon.account.Name = 'Company Internal';
}
}
}`
Here is my TRIGGER
trigger NewUserCreatedTrigger on User (after insert) {
user[] users = trigger.new;
UserContact.createUserContact(Trigger.newMap.keySet());
Here is my TEST CLASS
@isTest
private class UserContactTest {
testmethod static void userCreationTest(){
//User u = UserContact.createUserContact(User.id); //Commented as it is not required
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;
}
I'm still learning, going through the trailhead modules and other online tutorials, but going through these errors is helping me learn a lot, so I'm trying to work through them at the same time.
Thanks!
List<Account> accounts = [SELECT Id FROM Account];
Or add a where clause or LIMIT clause...System.QueryException: List has more than 1 row for assignment to SObject
! So one way of guarding against this would be to useAccount accountx = [SELECT Id FROM Account LIMIT 1]'