I have a custom lightning component. The same component is used within my Salesforce org in a lightning tab. Also it is used in my community.

Now, my requirement is to navigate on two different pages, on click of save(Save button within lightning component) depending on whether save is clicked in community or from tab.

I am using navigateToURL event.

Is there any possible way to do the above.

  • 1
    That should be possible by detecting whether the user's session is in the Community or via Salesforce UI. – Dave Humm Dec 3 at 10:33

There are 2 ways to solve this. You can detect whether the user is in the Community context or not like Dave Humm suggests, or you can use design parameters to set the URL depending on where the Lightning Component is placed.

Detecting whether user is on the Community.

You can use the Site.getSiteId() method. This will have a value if the user is in a Community, but will be null if it is in Internal Salesforce.

Using Design Attributes

A better solution in my opinion would be using design attributes. This way, you can reuse the button even in more use cases, regardless of which user interface they are on.

Add a attribute to your component:

<aura:attribute name="url" type="String" />

Then add a design attribute to the design part of the component.

<design:attribute name="url" label="Redirect URL" required="true" />

This will map the the earlier created attribute and allows you to enter a value when you use the Lightning App Builder to put it on a page.
In your component you can read the value of this attribute when the button is clicked.

  • First create a design attribute in your component lets say "FromWhere"
  • In Community Builder you set the design attribute "FromWhere"="Community"
  • In Lightning Builder you set the design attribute "FromWhere"="Lex"
  • In your lightning component controller check the associated component attribute to know where it was invoked either Community or Lex
  • Put if else accordingly to redirect to the page using navigateToURL.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.