11

I found you can get the current user id like this:

import id from '@salesforce/user/Id';

I have a really simple component where all I need are the User Id and the current Org Id. I haven't been able to find any documentation on any simple way to get the Org Id without having to go to Apex. Is this possible? It would be quite unfortunate if you really have to create an apex class and a method just to get the current org id...

2
  • 1
    I guess $Organization global variable only works in Visualforce and not in lightning. In lightning, you need to create an apex controller and fetch the org id in the controller. String orgId = UserInfo.getOrganizationId(); Commented Oct 20, 2022 at 20:23
  • Oh no, this cannot still be the way to do it... -.-
    – Schnaps
    Commented Jan 18, 2023 at 12:16

1 Answer 1

1

You have to create an apex class and a method just to get the current org id. An example:

UserUtility.apxc

public with sharing class UserUtility {

    @AuraEnabled(cacheable=true)
    public static String getOrganizationId() {
        return UserInfo.getOrganizationId();
    }
}

getOrganizationId.js

import { LightningElement, wire } from 'lwc'; 
import getOrganizationId from '@salesforce/apex/UserUtility.getOrganizationId';

export default class GetOrganizationId extends LightningElement {

    @wire(getOrganizationId) 
    organizationIdResponse;
}

getOrganizationId.html

<...>
<p>{organizationIdResponse.data}</p>
<...>
6
  • 2
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – identigral
    Commented Jun 23, 2023 at 18:20
  • @identigral "What you're asking for cannot be done" is a valid answer.
    – Derek F
    Commented Jun 23, 2023 at 19:37
  • 2
    @DerekF Considering that a comment of the same nature has been made in 2022, the answer might be valid but it's not useful. A useful answer would show the few required lines of Apex + JS.
    – identigral
    Commented Jun 23, 2023 at 20:34
  • @identigral, sorry for my error. I'll do it better next time... I hope. Commented Jun 26, 2023 at 10:16
  • @DerekF, thanks for your support. Commented Jun 26, 2023 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .