728x90
현재 사용자 ID를 가져오는 방법을 알아 보겠습니다.
Apex Class
// Apex Class
public static Id getCurrentUserIdByApex() {
return System.UserInfo.getUserId();
}
System.debug('# userId : ' + getCurrentUserIdByApex());
결과
# userId : 0056F00000A1jLGQAZ
Aura Component
<aura:component implements="force:appHostable,flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,force:hasSObjectName,lightning:isUrlAddressable"
access="global">
<aura:attribute name="userId" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<div class="slds-box slds-box_small" style="background-color: white;">
<b>Aura Component</b>
<p>User Id : {! v.userId }</p>
</div>
</aura:component>
({
doInit : function(component, event, helper) {
let userId = $A.get("$SObjectType.CurrentUser.Id");
console.log(userId);
component.set('v.userId', userId);
}
})
LWC
<template>
<div class="slds-box slds-box_small" style="background-color: white;">
<b>LWC</b>
<p>User Id : {currentUserId}</p>
</div>
</template>
import { LightningElement } from 'lwc';
import UserId from '@salesforce/user/Id';
export default class CurrentUserIdLwc extends LightningElement {
currentUserId = UserId;
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>54.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Current User Id (LWC)</masterLabel>
<description>Current User Id (LWC)</description>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Visualforce Page
<apex:page>
<apex:slds />
<div class="slds-scope">
<div class="slds-box slds-box_small" style="background-color: white;">
<b>Visualforce Page</b>
<p>User Id : {!$User.Id}</p>
</div>
</div>
</apex:page>
적용된 모습
728x90
'Salesforce > Development' 카테고리의 다른 글
Apex 소개 (0) | 2023.02.07 |
---|---|
Salesforce OAuth 2.0 - password Type (0) | 2022.06.21 |
SOQL 쿼리에서 여러 값 like 사용 (0) | 2022.04.20 |
Apex에서 종속 선택 목록 값 가져오기 (Get Dependent Picklist Values in Apex) (0) | 2022.04.20 |
Safe Navigation Operator 안전 탐색 연산자(?.) (0) | 2022.04.19 |