6

We build a new napiliy commmunity site (Spring '17) and are using two profiles:

  1. Customer Community Plus and
  2. Customer Community Login

Users with the Community Plus profile should be able to create new "customer users".

The build-in UI for creating new customer users is still in the old vf-page style, so I am building a lightning custom component for creating and enabling customer users.

My code is working with my own user and admin profile, but with a "Customer Community Plus"-Profile-User i get this error:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Profile]: [Profile]

The User parameter comes from the ajax-call of my component and there IS a profileId supplied with the insert statement.

@AuraEnabled
public static boolean insertCustomerUser(User usr) {

    System.debug(usr);

    Profile p = [SELECT Id, Name FROM Profile where Name = 'Portal Profilename'];

    User portalUser = new User(ProfileId=p.Id,
                                ContactId=usr.contactId,
                                Username=usr.Username,
                                Firstname=usr.FirstName,
                                Lastname=usr.LastName, 
                                Email=usr.Email, Phone=usr.Phone,
                                CommunityNickName=usr.CommunityNickName,
                                Alias=usr.Alias,
                                Title=usr.Title,
                                TimeZoneSidKey=usr.TimeZoneSidKey,
                                LocaleSidKey=usr.LocaleSidKey,
                                EmailEncodingKey=usr.EmailEncodingKey,
                                LanguageLocaleKey=usr.LanguageLocaleKey);

    System.debug(portalUser);

    Database.SaveResult newPortalUser = Database.insert(portalUser);

    return true;
}

Output of my first system.debug:

User:{Email=apeters@scsoft.de, LocaleSidKey=de_DE, TimeZoneSidKey=Europe/Berlin, LanguageLocaleKey=en_US, FirstName=fn, Title=, ContactId=00358000005HMeMAAW, EmailEncodingKey=ISO-8859-1, CommunityNickname=NickName, Username=userEmailname, Phone=+49 (0)69, Alias=alias, LastName=ln}

output of my second debug and insert-statement:

User:{ProfileId=00e58000000cXDXAA2, ContactId=00358000005HMeMAAW, Username=userEmailname, FirstName=fn, LastName=Caicedo, Email=apeters@scsoft.de, Phone=+49 (0)69, CommunityNickname=NickName, Alias=alias, Title=, TimeZoneSidKey=Europe/Berlin, LocaleSidKey=de_DE, EmailEncodingKey=ISO-8859-1, LanguageLocaleKey=en_US}

I think there must be some security option missing, because this code is working with my admin userprofile.

  • any update on this? I'm coming across this currently. – James Loghry Feb 17 '17 at 18:33
  • I've submitted a ticket about this issue. This has been confirmed by Salesforce as a bug and I'm still waiting for a fix on this. – apeters Feb 20 '17 at 10:23
  • Any updates on this bug ? Any bug number ? – Chirag Mehta Apr 21 '17 at 11:30
  • We are also looking for an update, if one is found. – ATA1k Jun 20 '17 at 12:33
  • hi, Any update on this issue? I came across this issue while creating a user of profile type "Partner Community Login User". – Isha Arora May 28 at 6:08
2

We found a workaround for this problem.

Add a new field in user object: profileId__c and fill with the profileid.

Then build a trigger to transfer the profileId and clear the temp-Field:

 if (trigger.isInsert) {
     if (trigger.isBefore) {
            // u2 is null here!
                if (u.ProfileId__c != null) {       
                    u.ProfileId = u.ProfileId__c;
                    u.ProfileId__c = '';
                }
            }
  }
1

We encountered this bug today. We have an apex class that generated test users from an older SF Org (created in 2013). When we copied it into an org we created in 2017, it gave us the error you mentioned. I found a fix for our particular case.

The TestDataFactory class was declared as global with sharing. When I removed with sharing, the error went away.

global class TestDataFactory {

    public static void insertTestUsers() {
        User user = createUser('user1');
        insert user;
    }

    public static User createUser(String testName) {
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        String alias = EncodingUtil.convertToHex(Crypto.generateDigest('SHA1', Blob.valueOf(    testName))).substring(0, 8);
        return new User(
                Alias = alias,
                Email = 'test' + testName + '@ourcompany.example.com',
                UserName = 'test' + testName + '@ourcompany.example.com',
                EmailEncodingKey = 'UTF-8',
                LastName = 'Testing',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                ProfileId = p.Id,
                TimeZoneSidKey = 'America/Chicago'
        );
    }
}

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.