Say the user is running an App called Sales. How do I get that information via Apex?

My use case is that I have a Visual Force redirect. It calls a method on initialization that returns a page reference. This is great except that I'd like to use the same page redirect code within multiple apps and have it send a different redirection link depending on the App.

For example I could have my code determine the name of the App (i.e. Sales) and then look for a Custom Meta Data record with the name matching the App and look for a URL field on that record to feed back as part of the page reference that is returned.

Where I am stuck is figuring out how to get the App I am in so I can then do something dynamic based on that.

  • Why do you need this information in Apex? It seems like something you could pass to your controller as a parameter to some method call, but it is hard to give good advice when your question contains so little detail. – Adrian Larson Sep 15 '18 at 1:19
  • @AdrianLarson I added some additional context to my question. – Joseph U. Sep 15 '18 at 1:24

You can get the app's name by using its TabSetDescribe. Something like this should work:

// Get the tabset describes for all apps
List<Schema.DescribeTabSetResult> tabSetDesc = Schema.describeTabs();

String currentAppName;

// Loop through the tabset describes until we get to the current app's tabset
for(Schema.DescribeTabSetResult tsr : tabSetDesc){
        currentAppName = tsr.getLabel();

return currentAppName;
| improve this answer | |
  • 1
    Whoa, this is something I never even knew about. Kudos. – Charles T Sep 16 '18 at 1:14
  • @Joseph, did this work for you? Curious about the end result! – Shane Steinfeld Sep 30 '18 at 21:36
  • This won't work for lightning apps as Schema.describeTabs() only lists classic apps, not lightning apps. – James Aug 28 '19 at 14:43

Following code can be used for Lightning apps:

 UserAppInfo userAppInfo = [SELECT Id, AppDefinitionId FROM UserAppInfo WHERE UserId = :UserInfo.getUserId() LIMIT 1];
 AppDefinition appDefinition = [SELECT DurableId, Label FROM AppDefinition Where DurableId = :userAppInfo.AppDefinitionId LIMIT 1];
 system.debug( '***'+ appDefinition.Label );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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