String fieldSetName = 'Account_FieldSet';
String ObjectName = 'Account';
I wanted get list of all fields from this fieldset.
I know if fieldset name is hardcoded in the apex then following syntax works -
SObjectType.Account.FieldSets.Account_FieldSet.getFields();
But here main issue is Object name and fieldset name will come at runtime.
Attribution to: Prafulla Patil
Possible Suggestion/Solution #1
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 SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
//system.debug('====>' + DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName));
Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
//List<Schema.FieldSetMember> fieldSetMemberList = fieldSetObj.getFields();
//system.debug('fieldSetMemberList ====>' + fieldSetMemberList);
return fieldSetObj.getFields();
}
You can use result as follows -
List<Schema.FieldSetMember> fieldSetMemberList = Util.readFieldSet('Account_FieldSet','Account');
for(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList)
{
system.debug('API Name ====>' + fieldSetMemberObj.getFieldPath()); //api name
system.debug('Label ====>' + fieldSetMemberObj.getLabel());
system.debug('Required ====>' + fieldSetMemberObj.getRequired());
system.debug('DbRequired ====>' + fieldSetMemberObj.getDbRequired());
system.debug('Type ====>' + fieldSetMemberObj.getType()); //type - STRING,PICKLIST
}
Attribution to: Prafulla Patil
Possible Suggestion/Solution #2
There is a slightly shorter way to deal with this. The following method returns null if the field set is undefined, or a list of field set members otherwise:
public static List<Schema.FieldSetMember> getFieldSetMembers(String objectTypeName,
String fieldSetName) {
DescribeSObjectResult[] describes = Schema.describeSObjects(new String[] {
objectTypeName
});
if (describes != null && describes.size() > 0) {
// There should only be the one match for the one object type name
Schema.FieldSet fs = describes[0].fieldSets.getMap().get(fieldSetName);
return fs.fields;
} else {
return null;
}
}
Attribution to: Phil W
Possible Suggestion/Solution #3
I hope this is the code you are looking. Dynamic mapping with field set to clone list of records.
// test1 is the filled set on contact
List<Schema.FieldSetMember> schemaset= SObjectType.contact.FieldSets.test1.getFields();
// clisttoUpdate: fill this list with the records for which you wanted to clone by using dynamic soql with filed set
List<contact> clisttoUpdate=new List<contact>();
//to store mapped records to insert at the end
List<account> acc=new list<account>();
List<contact> ListWithData=new List<contact>();
List<contact> listForClone=new List<contact>();
for(integer i=0;i<ListWithData.size();i++){
system.debug(schemaset[i].getFieldPath()+'YYYYYYYYY');
system.debug(x.get(schemaset[i].getFieldPath())+'FFFFFFFFFFFFF');
contact cloneRec=new contact();
for(integer a=0;a<schemaset.size();a++){
cloneRec.get(schemaset[a].getFieldPath()) =clisttoUpdate[i].get(schemaset[a].getFieldPath())
}
listForClone.add(cloneRec);
}
insert listForClone;
Attribution to: shiva
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1474