1

so I'm getting this error below. Seems like its pretty explanatory right? The thing is that I have another class that actually inserts the user, and the test class on that works fine. All this trigger is doing is inserting a contact that matches the info of the Case. I've included my test class & trigger below, I figured this would be an easy fix for someone that knows more about this than me, maybe my eyes are just failing me!

 trigger CreateContactonCase on Case (before insert, before update) {
if(Trigger.isInsert){

List<Contact> ct = new List <Contact>();

for(Case acc : trigger.new){

    Contact c = new Contact
                (LastName = acc.Contact_Last_Name__c,
                Account=acc.Account,
                FirstName = acc.Contact_First_Name__c,
                //AccountId=acc.id,
                //Account= acc.Account_Name_If_not_in_Salesforce__c,
                MailingStreet=acc.Account_Address__c,
                /* MailingCity=acc.BillingCity,
               /* MailingState=acc.BillingState,
                MailingPostalCode=acc.BillingPostalCode,
                MailingCountry=acc.BillingCountry,*/
                //Name=acc.Contact,
                Role__c=acc.Contact_Role__c,
                Title=acc.Title__c,
                Phone=acc.Contact_Phone__c,
                CIS_Contact__c= false,
                Contact_Not_Verified_del__c = false,
               // ManageExternalUser  = true, 
                Email=acc.Contact_Email__c);
                //update c;


    ct.add(c);
    acc.Status ='Closed';
    acc.Portal_User_Created__c = true;
    //acc.Completed_By__c = System.user;
    acc.Completed_Date__c = System.today();
}

insert ct;
update ct;


}

}

 @IsTest
public class CreateContactonCase_TST {

static testmethod void CreateContactonCase () {
  Account a = new account (name ='testaccount');
  insert a;
  Case acc = new Case
                (Contact_Last_Name__c = 'TestLastname',
                Account= [SELECT id, name FROM Account WHERE name = 'test account'],
                Contact_First_Name__c = 'TestFirstName',
                //AccountId=acc.id,
                //Account= acc.Account_Name_If_not_in_Salesforce__c,
                Account_Address__c='TestAddress',
                /* MailingCity=acc.BillingCity,
               /* MailingState=acc.BillingState,
                MailingPostalCode=acc.BillingPostalCode,
                MailingCountry=acc.BillingCountry,*/
                //Name=acc.Contact,
                Contact_Role__c='TestRole',
                Title__c='TestTitle',
                Contact_Phone__c='12345678',
                //CIS_Contact__c= false,
                //Contact_Not_Verified_del__c = false,
               // ManageExternalUser  = true, 
                Contact_Email__c='testemail@email.com');
                insert acc;


}
List<Contact> c = new List <Contact>();
List<Case> cs = new List <Case>();

ERROR MSG

System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []

ADDED

thanks for the reply!!

So I went ahead and made some editions to both the trigger and the method where the error was reporting from. And now i'm getting this error. Basically I added to my Test Class:

UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];

User portalAccountOwner1 = new User(

UserRoleId = portalRole.Id,

ProfileId = profile1.Id,

Username = System.now().millisecond() + 'test3@test.com',

Alias = 'batman',

Email='bruce.wayne@wayneenterprises.com',

EmailEncodingKey='UTF-8',

Firstname='Bruce',

Lastname='Wayne',

LanguageLocaleKey='en_US',

LocaleSidKey='en_US',

TimeZoneSidKey='America/Chicago'

);

This was to insert a user so I could use the system run as method

Next I put this in my actual method:

 @future public static void createUser(Id ContactId, string UserRoleId, String email, String firstName, String lastName, String userName, String profileId, boolean IsPortalEnabled) {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.EmailHeader.triggerUserEmail = true;
    Profile p = [SELECT Id FROM Profile WHERE Name='Partner Community User Custom'];
    **UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];**
    User u = new User(alias = 'standt', email=email, CommunityNickname =lastname,
        emailencodingkey='UTF-8', lastname=lastname, languagelocalekey='en_US', 
        localesidkey='en_US', profileid = p.Id , contactId=contactId, **UserRoleId=PortalRole.Id**, 
        timezonesidkey='America/Los_Angeles', //IsPortalEnabled = false,
        username=email);

    u.setOptions(dmo);
    insert u;

(I only added the SOQL statement and the UserRoleId = PortalRole.Id)

I thought this would be sufficient, but I am now getting this error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, High Volume Portal Users cannot have a user role: [UserRoleId]

2

That error means what it says: a portal account (that is, an account with a contact that is enabled as a portal user) must be owned by a user that has a role. I don't immediately see why you are getting this error; it's also not clear which insert is throwing the error; it's probably the contact insert in the trigger, but checking the error line number in the log is probably a good idea.

aside: Where I usually see this error is when setting up a community user for the first time in a DE org; for whatever reason the DE org default (sysadmin) user doesn't get a Role assigned automatically, and that user owns all the demo accounts & contacts. Create a community, pick an existing contact, choose Manage External User->Enable Customer User, and you'll see the error. I'm not sure if there's another trigger in the org that's getting involved once the contact is inserted or what; again, check the log and pin down the code line throwing the error.

One other thing I noticed, in your test method, your account names don't match: Account a = new account (name ='testaccount'); but Account= [SELECT id, name FROM Account WHERE name = 'test account'] ("testaccount" != "test account"). If you have @SeeAllData=true in your test class you may be getting a different account than the one you expect.

One other general comment. I don't know what your case create flow is like, this may not be an issue for you; but you should probably check that Case.ContactID == null (that is, there's not already a contact on the case) before creating a new contact.

Update: in your new createUser method you have these two lines:

Profile p = [SELECT Id FROM Profile WHERE Name='Partner Community User Custom'];
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];

You then create a user with this profile and role. As the error message points states, "High Volume Portal Users cannot have a user role". In other words, remove the portalRole from the new User statement, you don't need it. Going back to my original answer, the user that needs a role is the Internal User who owns the Account to which the Contact belongs.

  • thanks for the reply!! So I went ahead and made some editions to both the trigger and the method where the error was reporting from. And now i'm getting this error. Basically I added to my Test Class: (I only added the SOQL statement and the UserRoleId = PortalRole.Id) I thought this would be sufficient, but I am now getting this error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, High Volume Portal Users cannot have a user role: [UserRoleId] – Christian Jan 18 '15 at 22:13
  • Plz see code above, I cannot post updated code in comments because character limits : ) – Christian Jan 18 '15 at 22:19
  • So in that case, could I just theorhetically set the OwnerId field of the account that im inserting in the test class to this role? – Christian Jan 18 '15 at 23:14
1

The user that is trying to create the portal user needs a role. In this case it is a script that is trying to create that user and that script runs with your permissions, so YOUR OWN PROFILE needs to have a role.

I ran into this a few times.

This is the utility method I use to create a test portal user:

public static User portalUserWithProfile(String profileName, String accountName, String contactName) {

    Contact c = NewContact(NewAccount(accountName), contactName);

    Profile p = [select id from profile where name = :profileName];

    User u = new User(Alias='ptest', email='portaltestuser@testorg.com',
        emailencodingkey='UTF-8', lastname=contactName, languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id,
        timezonesidkey='America/Los_Angeles', username='portaltestuser@testorg.com',
         ContactId = c.Id, IsActive=true);
    insert u;

    c.User_Account__c = u.Id;
    update c;

    return u;
}

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.