We’re rewarding the question askers & reputations are being recalculated! Read more.
48

How to get FieldSet fields in Apex Dynamically (fieldset name is not static) Here is the method I came up with lots of trial and errors - public static List<Schema.FieldSetMember> readFieldSet(String fieldSetName, String ObjectName) { Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); Schema.SObjectType ...


46

As Doug states, I started looking into this, and ended up with a bigger test/demo/etc. than I originally planned. I took the "wrapper" class approach, starting with an AuraEnabled version of FieldSetMember in FieldSetMember.apx: public class FieldSetMember { public FieldSetMember(Schema.FieldSetMember f) { this.DBRequired = f.DBRequired; ...


15

I am not sure if you could use the field set directly to compare but you can use the sObject's get method to do the job set<String> fieldSet = new Set<String>(); //dynamically get the fields from the field set and then use the same for comparison in the trigger. for(Schema.FieldSetMember fields :Schema.SObjectType.Account.fieldSets.getMap().get(...


14

Let me start with saying thank you for posting this helpful information. It helped me with learning some of Lightning's more advanced features and gave me a jumping off point. Using @bafuda's component as a starting point, here's another field set form variation that lets one save the record back to Salesforce using an upsert. It supports adding a new ...


13

Its not possible to create a Fieldset within a test to avoid hard coding. Nor is it possible to dynamically create Schema.FieldSetMember (I've just tried!). So with that confirmed, here are a couple of options... Conditional Test Execution. Based on Dynamic Discovery of a Test Fieldset. If you not happy hard coding a reference to a Fieldset (of course this ...


12

Here's a modified version of @Skip Saul's code that works for API version 37. This version allows binding the form field values to the 'record' JS object passed in to the component. As in @Skip Saul's version the form is dynamically created using the fields in the field set. Example app using the component <aura:application > <aura:attribute ...


9

Long introduction talk This would probably be much easier for you if you have ever played with "Dynamic Apex" and "Dynamic SOQL". Sometimes we need to work on the level of generic sObjects rather than concrete Account, User or Custom_Object__crecords. Maybe running this in Execute Anonymous will help: Map<String, Schema.SObjectField> M = Schema....


9

This is indeed possible via the Salesforce Metadata API. While this is a Web Service it is available through Apex like any other Web Service. There is a prebuilt Apex wrapper with samples here. The FieldSet metadata type can be used with the 'create' and 'update' operations on this API. As if by magic it appears someone has just commited a FieldSet example ...


7

SObjects have map-like methods available including get that takes a String or SObjectField argument. So you can write a trigger of this form: trigger MyTrigger on MyObject__c (before update) { String[] fieldNames = new String[] {'Field__c', 'Field2__c', ...}; for (MyObject__c mo : Trigger.new) { MyObject__c old = Trigger.oldMap.get(mo.Id); ...


7

Yes, this is the default behavior, and no there's no "direct" workaround. The reason why is because the Map returned by the call is actually a special map that I'll call CaseInsensitiveKeyMap. A normal map would more appropriately be called a CaseSensitiveKeyMap, because "Hello" and "hello" are two unique keys (because they use hashCode(), which will ...


7

First, as of this writing, fieldsets aren't supported in Lightning. They could potentially be created in Classic and then "applied" to Lightning situations. However, that wouldn't be easy to do. I know they're on the roadmap. That having been said, one of the places where using them is especially helpful is when making queries. Have you ever experienced ...


6

There are several examples if you google "Fieldsets in Visualforce". Here's a nice tutorial from our own SSE guru Bob Buzzard: http://bobbuzzard.blogspot.com/2011/02/visualforce-field-sets.html <apex:repeat value="{!$ObjectType.Account.FieldSets.General}" var="field"> <apex:inputField value="{!Account[field]}"...


6

If f is the reference to a field within the fieldset, then all you need is f.label. Example straight from the docs: <apex:pageBlockTable value="{!$ObjectType.Contact.FieldSets.properNames}" var="f"> <apex:column value="{!f}"> <apex:facet name="header">Name</apex:facet> </apex:column> <apex:column value="...


5

You need to use getRequired() method of the FieldSetMember: {!IF(f.Required, '*', '')} So in your case it wiil look like this: <tr> <td class="tdStyleFirst"><apex:outputText value="{!f.Label}" /></td> <td> <apex:outputText value="*" style="color:red;" rendered="{!f.Required}"/> ...


5

Yes, if the field is custom to their org then they can add it to the field set. If the field is part of your managed package they can only add it if it is in the Available Fields list when you create / update your package..


5

The big thing about fieldsets is that they're declarative. If you define a fieldset, and use that in visualforce/apex, you can change the fields that are displayed in visualforce, or processed in apex, without modifying code. This also means that, in my org, I can (safely) change behavior in production without the need for a deployment. Probably the best ...


4

Okay, after some guidance from eyescream, I came up with the following: Using a suggestion found on this developerforce article and some off the wall advice. I came up with a decent but hacky solution. Using the document.getElementById function, I was able to pass parameters like so: In VisualForce <apex:inputField value="{!Task[TaskField]}" required="...


4

SO for me this did not looked possible through standard REST API .So went ahead and built a custom logic as Apex REST which i unit tested and looks ok to me @RestResource(urlMapping='/sample') global class SampleRestWebservice{ global class fieldsetmainwrapper{ public List<fieldsetwrapper> fsetwrap; public string fieldsetname; } public ...


4

To get quotes round the field names so you have a JavaScript array of strings, you need to turn the Apex array into a JavaScript array by for example using an apex:repeat: <apex:page> <script> var fields = [ <apex:repeat var="f" value="{!$ObjectType.Customization__c.FieldSets.Fields_for_fieldChooser}"> '{!f}', </apex:repeat> ]; &...


4

Got it! At least in parts.... Expression of this type {!$ObjectType.Object__c.FieldSets.SetName} in Visualforce evaluate to Schema.FielSetMember[] and not FieldSet. So when i changed my component attribute to <apex:attribute name="displayed" type="Schema.FieldSetMember[]" assignTo="{!displayedFields} ../> everything the page compiles. But it ...


4

Access with getFieldPath() should work for you, I'm surprised you're saying it doesn't compile? The problems occur when the fieldset contains items from referenced records (dots in fieldPath). You need to use something similar to Using sObject.get() to fetch a field value from a related parent object sObject acc = [SELECT Owner.Manager.Profile.Name ...


4

If you want a tool that can understand cross object relationships, you are going to need a sophisticated approach indeed. It may be worth separating into the following tasks: Get the referenceTo results for each field. Depending on how many times you are reusing this functionality in one context, it may be worth caching. Differentiate if you are at a root ...


4

Try binding the input text to a List On the controller and use that to persist to the DB. (NOTE: I can't see your wrapper definitions, and I am having trouble following all the code, so you may want to pick the correct variable. I am just trying to give you a quick sample) <apex:repeat value="{!oliList}" var="oneOli"> <apex:column headerValue="...


4

It is completely possible: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_field_sets.htm <apex:page standardController="Account"> <apex:repeat value="{!$ObjectType.Opportunity.FieldSets.mySpecialFields}" var="field"> <apex:outputText value="{!field}" /> </apex:repeat> </apex:...


4

Field sets in managed packages require the developer to explicitly mark each one as eligible to be added to the field set. https://salesforce.stackexchange.com/a/63416/55332 There is an idea to be able to set this at the object level, since many managed package owners overlook this. https://success.salesforce.com/ideaView?id=08730000000aKz1AAE


4

It's just a matter using the right functions, namely get and put, from the sObject class. for(String s : listOfFieldsViaAPIname){ singleLead.put(s, soqledLead.get(s)); } Could also stand to make your query use your fields, could also make those fields into a fieldset, but thats out of scope of this question.


3

I believe the problem is that you are binding to the data, which, when rendered with no results, is not there to reference. Normally, without a field set you would bind to something in metadata. For instance like this: <!-- meta describe example <apex:column ... headerValue="{!$ObjectType.MyObj__c.fields.Field__c.label}"/> <!-- custom label ...


3

I'm fairly sure that you can't get help text in this scenario. I believe it automatically renders only when InputField is used in the context of a PageBlockSection (where the label and input field are rendered automatically, with the help bubble in between). I haven't seen a way to get mouseover help bubbles automatically in a PageBlockTable. The solution ...


3

Well they are both same. Have you tried doing {!f} in the visualforce page? This will give you the API name of the field in the fieldset i.e. the fieldpath. And also when you are doing this {!$ObjectType.My_Object__c.FieldSets.MyFieldSet} you are actually describing the fieldset and is equivalent to something like this in apex : List<Schema....


3

Have you looked at Schema.FieldSet? It looks like it would do the trick for you. The MerchandiseDetails example at the end of the page uses the Field Set without the standard controller.


Only top voted, non community-wiki answers of a minimum length are eligible