1
@wire(getRecord, { recordId: currentUserId, optionalFields: ['User.Profile.Name', 'User.AccountId', 'User.Account.Name','User.UserRole.Name'] })
    userData({ error, data }) {
        if (data) {
            console.log('user data ====> ', data);

Here you can see that I'm requesting various fields with @wire next I get them in the console.log.

Except for the role that never shows up.

I precise that the current user in the community site is assigned a role. (I think that's always the case.)

If I set the field in field and not optional field like this:

@wire(getRecord, { recordId: currentUserId, fields : ['User.UserRole.Name']  })

I get this error :

{
    "status":400,
    "body": {
        "message": "INVALID_FIELD:
                    SELECT LastModifiedDate, UserRole.Name, Id, LastModifiedById
                    ERROR at Row:1:Column:26
                    Didn't understand relationship 'UserRole' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.",
        "statusCode":400,
        "errorCode":"INVALID_FIELD"
    },
    "headers":{}
}

Do someone know if there is a way to get the current user role in experience cloud?

5
  • How is currentUserId being set? What happens if you make it reactive? Are the other fields showing up?
    – sfdcfox
    Feb 11, 2022 at 13:08
  • The currentUserId is set like this : import currentUserId from '@salesforce/user/Id'; What do you mean by make it reactive if I may @sfdcfox? Yes the Profile and the Account.Name also. I was wondering if it is because of the fact the role for a community user is specialy created but I don't know.
    – Jojosfdc
    Feb 11, 2022 at 13:15
  • Hmm, no, that's not the problem. What happens if you move the field from optionalFields to just fields? Do you get an error?
    – sfdcfox
    Feb 11, 2022 at 13:16
  • yes indeed @sfdcfox: error ====> {"status":400,"body":{"message":"INVALID_FIELD: \nProfile.LastModifiedById, Profile.Id, Profile.Name, UserRole.Name, Id, Profile.SystemModstamp\n ^\nERROR at Row:1:Column:104\nDidn't understand relationship 'UserRole' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.","statusCode":400,"errorCode":"INVALID_FIELD"},"headers":{}}
    – Jojosfdc
    Feb 11, 2022 at 13:19
  • That suggests that the user can't view the UserRole object, or that field. I think it's due to the "View Roles and Role Hierarchy" permission, which I don't think you can enable for such a user. Check the user's profile permissions. I guess you could probably use Contact.Account.Name as a close alternative.
    – sfdcfox
    Feb 11, 2022 at 13:51

0

You must log in to answer this question.