2

When Customer Community Users are trying to create a contact, they are facing an error:

Cannot Assign Portal Customer or Contact to Portal User
You cannot assign a Portal Customer or Contact to a Portal user. Please choose a different user.

This is not a licence limitation as I have checked that already:
https://help.salesforce.com/articleView?id=users_license_types_communities.htm&language=en_US&type=0

I think it is a bug that does not allow external users to be owners. But when I tried to update the Owner to an internal user before insert using trigger as well as using workflow, still I faced the same error.

Is there any way to circumvent this?

1

Yes, the system acts inconsistently. Customer Community users cannot create contacts due to the ownership issue, but Customer Community Plus users can create contacts because the system automatically changes the contact owner to that of the portal account owner.

A before insert trigger does work to change the contact owner and allow the contact creation to occur for Customer Community license users. This code worked for me:

Trigger:

trigger ContactTriggers on Contact (before insert) {
    // Change ownership if created by Customer Community.
    ContactDomain.setOwner(Trigger.new);
}

Handler class:

public with sharing class CM_ContactDomain {
    public static void setOwner(List<Contact> newList) {
        if (UserInfo.getUserType() == 'CSPLitePortal') {
            Id theOwnerId;

            // Use the portal account owner if active, otherwise get a random active standard user.
            User u = [SELECT Account.OwnerId, Account.Owner.IsActive FROM User WHERE Id = :UserInfo.getUserId()];
            if (u.Account.Owner.IsActive) {
                theOwnerId = u.Account.OwnerId;
            } else {
                List<User> users = [SELECT Id FROM User WHERE IsActive = true AND UserType = 'Standard' LIMIT 1];
                if (!users.isEmpty()) {
                    theOwnerId = users[0].Id;
                }
            }

            if (theOwnerId == null) {
                // Throw exception here.
            }

            for (Contact con : newList) {
                con.OwnerId = theOwnerId;
            }
        }
    }
}

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.