I'm trying to build a component that render fields from an sObject FieldSet dynamically - The feature I'de like to add is the ability to navigate from component UI into the FieldSet page layout in order to add/remove fields as needed. Is this possible to do using standard sObject fieldSet in lightning experience mode active? is there a better approach for this functionality in lightning ? as seems like FieldSet are not completely supported within lightning... at least not yet?
-
1FieldSet being a setup configuration, do you want end users to be able to change FieldSet from a LC? Or will users accessing that LC be only sys admin users? What's your use case here?– Jayant DasCommented Aug 10, 2018 at 13:00
-
They're not supported in Lightning, but are supported in Classic. Regardless, you'll need a server side controller to run your query and pass the info back & forth to your component.– crmprogdevCommented Aug 10, 2018 at 13:22
-
Yes functionality will available only for system admins that has access to configure which fields will be available on screen. The use case is mainly just to ease the access - currently to get there you must switch to classic > go to setup > search object and look for the fieldset in order to edit which fields will be shown.– sfdx bombCommented Aug 10, 2018 at 13:49
3 Answers
After searching a bit more seems like the only way currently to navigate to FieldSet Edit/View page is by enforcing navigation to classic with a URL hack.
Edit/View page by Fieldset Id :
"https://<your instance url>.my.salesforce.com/<FieldsetId>"
All FieldSets by sObject Id :
"https://<your instance url>.my.salesforce.com/_ui/common/config/entity/FieldSetListUI/d?retURL=%2F0IX%2Fe%3Fsetupid%3DCustomObjects&setupid=CustomObjects&tableEnumOrId=<EntityDefinitionId>"
To Get the base URL dynamically in APEX:
private static String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
To Get the FieldSet Id - Quickest way would be to ask the admins to copy paste it once initially and store it in a place that can be query (eg.custom metadata type, sObject..) - then the FieldSetId or EntityDefinitionId can be merged into that FieldSetURL string. which from JS I just allowed the navigation onclick to :
window.location.href = FieldSetURL;
In case someone wish to go the extra mile and fetch either the FieldSetId or EntityDefinitionId - Apparently only way to get those dynamically will be via REST Tooling API - below post shows example : https://success.salesforce.com/answers?id=90630000000Cjf2AAC
string fieldSetName = 'xxx'; // replace to the Name of your fieldSet
string query = 'Select Id,ManageableState,MasterLabel,DeveloperName,NamespacePrefix,Description,EntityDefinitionId from FieldSet Where DeveloperName=\''.replace(' ','+') +fieldSetName +'\'';
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
String toolingAPIString = '/services/data/v43.0/tooling/query/?q='+query;
String reqMethodType ='GET';
Httprequest request =new HttpRequest();
request.setEndpoint(baseUrl + toolingAPIString);
request.setMethod(reqMethodType);
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setHeader('Content-Type', 'application/json');
Http httpReq = new Http();
HttpResponse res = httpReq.send(request);
System.debug(res.getBody());
But if the goal is to be able to move this code between orgs- Using the Tooling API will still brings extra admin efforts as setup of Remote Sites settings will be required in each org (which takes more or less same amount of time to configure as the hard coded Id copy paste solution and much less code - but it's an option).
Another approach that I am using, is using an update record
action.
In short - you add an update record action to your object, and select the fields that you want.
Then you add this action as a Related Record
component in the app builder record home of this object.
I have written a detailed answer on how to do this here:
UPDATE
With Winter '19, Field Sets are now available in Lightning Experience.
As of now, accessing the Field Set is not possible in LEX and you need to Switch to Classic to do so. There is an idea for this feature to be included in LEX.
Further, based on your comment which details out your requirement where you want to facilitate this from a custom component:
Yes functionality will available only for system admins that has access to configure which fields will be available on screen. The use case is mainly just to ease the access - currently to get there you must switch to classic > go to setup > search object and look for the fieldset in order to edit which fields will be shown
As I observed with a quick test, it seems you can do what you want to achieve. I am just not sure if you should be using this approach or not because if this is for system admin only then my take will be to switch to classic instead of going this route.
But on the idea for a component, you will need to know the URL to the Field Set of the object that you are trying to reach to in Classic and then simply provide a link to it from your component, which will navigate you to the Field Set setup screen in Classic.
The example that I used was as below to open the Account's Field Set page from a custom lightning component.
Sample Component:
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myUrl" type="String" />
<a href="{!v.myUrl}">Account Field Set</a>
In the JS Controller, populate the URL (the hard coded URL in the component was not saving the component, thus moved it to the JS)
doInit : function(cmp,event,helper){
cmp.set("v.myUrl", "https://<my instance url>/_ui/common/config/entity/FieldSetListUI/d?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DAccount&tableEnumOrId=Account&setupid=AccountFieldSets");
....
}
-
Thanks. Yes I did figure out something similar currently where I get the base URL with a saved given Hard Coded FieldSet Id on APEX and then on my JS controller merge into URL with => window.location.href = baseUrl + '/' + fieldSetId +'?source=lex'; It is working BUT this is not really the recommended way to do it in lightning or not really scalable between orgs due to 'hard coded Id' - Hope there is another approach that might can either navigate directly via event that can handle lightning to classic or somehow to fetch dynamically the fieldSetId. Commented Aug 10, 2018 at 18:02
-
There will be trade-offs for sure and that's where I mentioned not really confident in using this. However with the link that I mentioned, it can take you to the field set page at least. And then you can always maintain how to render those links, you don't need an Id in this case at least. By this way you at least have an option of what you want to achieve. Commented Aug 10, 2018 at 18:04
-
right. nice one did not noticed that - will try it - this can maybe help avoid the fieldset hard coded Id. will it work on custom object? + Just wondering does <a href="" supposed to be same as window.location.href - is there a way to workaround it somehow? Commented Aug 10, 2018 at 18:14
-
Haven't really tried it, I just hard coded this in my test. But I will assume it to be the same. You may like to try that out. Commented Aug 10, 2018 at 18:29
-
played with it a bit but and seems like custom object will not support your URL format. from what I tried => The URL hack will only work by navigating directly to baseUrl + '/' + hard code fieldSetId. Thanks for your suggestion though. Commented Aug 10, 2018 at 20:18