2

Getting this error in Test Class

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: []

Here is the specific method

@future
 public static void createUser(Id ContactId, 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'];
        User u = new User(alias = 'standt', email=email, 
            emailencodingkey='UTF-8', lastname=lastname, languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id , contactId=contactId, 
            timezonesidkey='America/Los_Angeles', //IsPortalEnabled = false,
            username=email);

    u.setOptions(dmo);
    insert u;

        IsPortalEnabled = true;
        system.setPassword(u.Id, '');

    update u;
}

TEST CLASS

static testMethod void createCustomerPortalUserTest() {
    customerPortalCls cpTest = new customerPortalCls();
    List<SelectOption> profileOptions = cpTest.getCustomerPortalProfiles();
    cpTest.a= new Account(name='TestMethod Account Name');
    cpTest.c = new Contact(firstName='TestFirstName', lastName='TestLastName', email='test@testorg.com');
    cpTest.u = new User(username='testusername@abc.com');
    Profile[] profiles = [Select p.name, p.id From Profile p where p.UserLicense.Name like '%Partner Community User Custom%'];
    //cpTest.selectedProfileId = profiles[0].id;

    Test.startTest();
        cpTest.checkForNewUser();
        PageReference testPageRef= cpTest.createPortaluser();

    Test.stopTest();
    PageReference testPageRef2 = cpTest.reset();
    cpTest.checkForNewUser();

}

static testMethod void createCustomerPortalUserTestNullUsernameError() {
    Boolean errorFound = false;
    customerPortalCls cpTest = new customerPortalCls();
    cpTest.a= new Account(name='TestMethod Account Name');
    cpTest.c = new Contact(firstName='TestFirstName', lastName='TestLastName', email='test@testorg.com');
    cpTest.u = new User();
    Profile[] profiles = [Select p.name, p.id From Profile p where p.UserLicense.Name like '%Partner Community User Custom%'];
    //cpTest.selectedProfileId = profiles[0].id;

    Test.startTest();

        PageReference testPageRef= cpTest.createPortaluser();

    Test.stopTest();
    ApexPages.Message[] mssgs = ApexPages.getMessages();
    for(ApexPages.Message m : mssgs){
       if(m.getDetail()=='Username is required: Please a username for the new user')
          errorFound=true;
    }
    System.assert(errorFound==false);
}

 static testMethod void createCustomerPortalUserRequiredFieldErrors() {
    Boolean errorFound = false;
    customerPortalCls cpTest = new customerPortalCls();
    cpTest.a= new Account();
    cpTest.c = new Contact(firstName='TestFirstName', lastName='TestLastName', email='test@testorg.com');
    cpTest.u = new User();
    Profile[] profiles = [Select p.name, p.id From Profile p where p.UserLicense.Name like '%Partner Community%'];
    //cpTest.selectedProfileId = profiles[0].id;

    Test.startTest();
        PageReference testPageRef= cpTest.createPortaluser();
    Test.stopTest();

}

Any help on this would be greatly appreciated! I'm kinda stumped at this point

Also, not sure why some of the code isn't displaying in the right format. Sorry about that

1

Is it your intent to create a Portal User or a Standard User? If you wish to create a Portal user, you need to query for the Profile that uses a portal user license. Depending on your org (old orgs could still be using Customer Portal vs. new orgs can only enable Communities, etc.), the name of the Profile is different. (and obviously you could have cloned one of the standard ones and have your own customized portal Profile)

Alternatively, you can find any old portal Profile by specifying the UserType in your Profile query

SELECT Id FROM Profile WHERE UserType = 'CspLitePortal' LIMIT 1
  • Yeah so I had that originally in the query, however in this sandbox the customer doesn't have any community licenses & doesn't wish to pay for more, so I was trying to find a way around that – Christian Jan 15 '15 at 2:31
  • How old is the sandbox? If it was created within the past ~6 months or so, the license types should propagate from Production -> Sandbox via a button. Prior to Summer '14, you had to do a refresh. See releasenotes.docs.salesforce.com/en-us/summer14/release-notes/… – daveespo Jan 15 '15 at 2:36
  • Gotcha, Ill try this! It's older than 6 months old, and they do not want to do a refresh. It's weird I know, but they are very particular about their sandbox. I'm gonna test it tomorrow and see if it works. This issue might not be closed after I try this so an admin might want to keep it open until tomorrow – Christian Jan 15 '15 at 2:41
  • So now i'm getting another on this, I've included the updated code and the test class. System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: – Christian Jan 16 '15 at 23:42

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.