Okay, after running into a plethora of errors, I tried attacking this a different way. This is the error I'm currently getting:
System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []
Here is the new TRIGGER:
trigger UserOnHoldTrigger on User (after update, after insert) {
//Creates an array to hold the users caused by trigger firing
user[] usersList = trigger.new;
UserOnHoldClass.IfOnHold(Trigger.newMap.keySet());
}
Here is the new CLASS (Created a class and had the trigger reference it, since triggers could not do API calls)
public class UserOnHoldClass {
public static void IfOnHold(set<ID> recordIDs){
//Get the required fields for user
list<user> usersList = [SELECT ID, Firstname, Lastname, email, name, username, street, city, state, postalcode, country,
UserRoleId, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId,
LanguageLocaleKey
FROM user
WHERE ID in :recordIDs];
for(user x : usersList){
Id profId = [SELECT id FROM profile WHERE Name = 'Customer Community - On Hold'].Id;
String userProfile = x.profileID;
if(x.profileID == profId){
Messaging.SingleEmailMessage mail = new messaging.SingleEmailMessage();
String emailAddress = [SELECT Email
FROM User
WHERE ID = :usersList].email;
String[] toAddresses = new String[] {emailAddress};
mail.setToAddresses(toAddresses);
mail.setSubject('Community User Account Placed on Hold');
Mail.setPlainTextBody('Your Courion Community User account has been placed on hold');
messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}
}
Here is the new TEST CLASS
@isTest
public class OnHoldTest {
testmethod public static void onHoldMethod(){
Account a = new Account(Name = 'Internal');
insert a;
Contact contactToInsert = new Contact();
contactToInsert.FirstName = 'Test';
contactToInsert.Lastname = 'Contact';
Id profId = [SELECT id FROM profile WHERE Name = 'Customer Community - On Hold'].Id;
insert contactToInsert;
User u = new User();
u.Firstname = 'mike';
u.LastName = 'Smith';
u.email = 'msmith@email.com';
u.Alias = 'alias';
u.Username = 'msith@email.com';
u.CommunityNickname = 'commnick';
u.EmailEncodingKey = 'UTF-8';
u.TimeZoneSidKey = 'America/Denver';
u.LocaleSidKey = 'en_US';
u.EmailEncodingKey = 'UTF-8';
u.LanguageLocaleKey = 'en_US';
u.street = 'street drive';
u.City = 'CityVille';
u.State = 'Georgia';
u.PostalCode = '30000';
u.Country = 'USA';
u.ProfileId = [SELECT id FROM profile where Name = 'Customer Community - On Hold'].Id;
u.ContactId = contactToInsert.id;
insert u;
update u;
}
}