How to avoid creating multiple Roles User, Manager and Executive while creating Experience Cloud Users in Salesforce?

How to avoid creating multiple Roles User, Manager and Executive while creating Experience Cloud Users in Salesforce?

There are scenarios where we need only one role or just two roles for an account. When we enable users for the Experience Cloud Site, it will create User, Manager and Executive roles for the Account if the Number of roles is set to 3 in Digital Experience Settings.

To avoid creating all the roles, you can create the UserRole using Apex.

Check the following sample codes where it creates only one Role User and avoids creating other Roles Manager and Executive.

1. Create Account and Contact.
Account objAccount = new Account(
Name = ‘Testing Experience Cloud Partner Site 1’
);
insert objAccount;

objAccount.IsPartner = true;
update objAccount;
System.debug( ‘Account Id is ‘ + objAccount.Id );

Contact objContact = new Contact(
FirstName = ‘Test 1’,
LastName = ‘Test2 ‘,
Email = ‘[email protected]’,
AccountId = objAccount.Id
);
insert objContact;
System.debug( ‘Contact Id is ‘ + objContact.Id );

2. Use the Account and Contact Ids from the above and create UserRole and the User.
/*
If no roles exist, adds User Role
If 1 role (User) exists, adds Manager Role
If 2 roles (Manager, User) exist, adds Executive Role
*/
UserRole objRole = new UserRole();
objRole.PortalType = ‘Partner’;
objRole.PortalAccountId = ‘0018c00002MBW7OAAX’;
objRole.CaseAccessForAccountOwner = ‘Edit’;
objRole.OpportunityAccessForAccountOwner = ‘Read’;
insert objRole;
System.debug( ‘Role Id is ‘ + objRole.Id );

Profile experienceCloudProfile = [
SELECT Id
FROM Profile
WHERE Name = ‘Partner Community User’
LIMIT 1
];

User objUser = new User(
UserName = ‘[email protected]’,
FirstName = ‘Test 1’,
LastName = ‘Test2 ‘,
Email = ‘[email protected]’,
ContactId = ‘0038c00002orChTAAU’,
Alias = ‘partner1’,
ProfileId = experienceCloudProfile.Id,
EmailEncodingKey = ‘UTF-8’,
CommunityNickname = ‘partner1’,
TimeZoneSidKey = ‘America/Los_Angeles’,
LocaleSidKey = ‘en_US’,
LanguageLocaleKey = ‘en_US’
);
insert objUser;
System.debug( ‘User Id is ‘ + objUser.Id );

Leave a Reply