2

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;
    }
}
1

The reason for this error is, the Account will be created with the user who runs the test. When you create the portal user, the portal user role will be determined based on role of the Account owner which is the user who runs the test.

I could see that the contact you assign to contactId of User is not related to any Account. So add contactToInsert.accountId = a.Id;

So assign a user yourself and run the test. You will be able to run it without any issues.

Hope it helps.

  • Thanks for the reply! Where am I assigning the user? – Daniel Sassone Apr 8 '16 at 15:18
  • Check whether you have role assigned to you in your user detail page. I strongly suspect its not. – Vigneshwaran G Apr 8 '16 at 15:19
  • while creating contact, you doesn't associate it with any account. Try assigning the account to the contact. – Vigneshwaran G Apr 8 '16 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.