Saturday 16 January 2021


Access Client Form Factor

To access the form factor of the hardware the browser is running on, import the @salesforce/client/formFactor scoped module.

import formFactorPropertyName from '@salesforce/client/formFactor'

  • formFactorPropertyName—A name that refers to the form factor of the hardware running the browser. Possible values are:
    • Large—A desktop client.
    • Medium—A tablet client.
    • Small—A phone client.
html
<template>
    <lightning-card title="formfactorName" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            <p>FormFactor:</p>
            {formFactor}
        </div>
    </lightning-card>
</template>

js
import { LightningElement } from 'lwc';
import FORM_FACTOR  from '@salesforce/client/formFactor'
export default class ClientFormFactor extends LightningElement {

    formFactor = FORM_FACTOR;
}

xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>50.0</apiVersion>
    <isExposed>true</isExposed> 
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Get Information About the Current User

To get information about the current user, use the @salesforce/user scoped module.

import property from '@salesforce/user/property';

  • property—The supported properties are Id, which is the user’s ID, and isGuest, which is a boolean value indicating whether the user is a community guest user. Use the isGuest property to check whether or not the user is authenticated.

This sample code imports the current user ID and assigns it to the userId property to provide access in the HTML template.

html

<template>
    <lightning-card title="currentuserId" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            <p>User Id:</p>
            {userId}
        </div>
    </lightning-card>
</template>

js

import { LightningElement } from 'lwc';
import Id from '@salesforce/user/Id';
export default class CurrentUser extends LightningElement {
    // Assign to variable
    userId = Id;
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>50.0</apiVersion>
    <isExposed>true</isExposed> 
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>

</LightningComponentBundle>