function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VivoVivo 

Ordering of fields when pulling fieldnames dynamically

Hi,

 

I have a dynamic object edit page that takes an object's name and then displays all of the fields that it has dynamically. However, it seems like the order in which these fields are displayed is somewhat random (It is not alphabetically ordered like in the schema, and it is not ordered the same that it is displayed in the object itself).

 

Does anyone know where this ordering is coming from? I want to retain a dyamic edit page without manually having to re-order the fields for each object (Which destroys the utility of a dynamic object edit page anyways). 

 

Thanks,

Vivo

JiahJiah

If this a standard edit page, then please edit the page layout. If it is a custom page, then how are you fetching the fields? If using field set then please rearrange the order in the field set.

VivoVivo

I'm actually pulling the fields by using:

 

listObjectFields =  new List<String>();  
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();  
        
        Schema.sObjectType sObjType = globalDescription.get(Object_Type);  
          
        sObjectToBind = sObjType.newSObject(null, true);  
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
         
          
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        Integer i = 0;  
        fieldnames='';
        for(Schema.SObjectField field : mapFieldList.values())  
            {  
                Schema.DescribeFieldResult fieldResult = field.getDescribe();  
                fieldnames+=field + ' ,';
                if(fieldResult.isNameField()){
                        namefield= fieldResult.getName();  
                        }
                        else{
                        if(fieldResult.isAccessible() && fieldResult.isUpdateable() && !fieldResult.isDefaultedOnCreate())  
                    {  
                             listObjectFields.add(fieldResult.getName());  
                        }
                    }  
                
            
          }    
           fieldnames=fieldnames.substring(0,fieldnames.length()-1);
           soql = 'SELECT ' + fieldnames+ ' FROM '  + sObjType +  ' WHERE id=: ObjId';
         
            sObjectToBind = Database.query(soql);
          

 

So it is actually using neither field set or standard page. I'm just curious where the ordering from the map is coming from. It seems to be arbritrary when I look at it.

JiahJiah

1. Create one wrapper class that will keep field API name and label.

2. Use a map which will have the label(keyset) to api name of the field.

3. Now iterate a list which contains the field labels in the order you want to display in page.

4. Then create the object of wrapper class and put into the list of the wrapper class which you are going to use in the page.

 

Hope you got it. Let me know. If not I shall send you a sample.

VivoVivo

Yeah this was the solution that I reached in the end too. Seems like the best way to proceed. I just wanted to ask on here if anyone knew where that order was coming from originally because it seems like there would be no reason for it to be random. 

 

Thanks though!

-Vivo

sfdcfoxsfdcfox
It is most likely based on the created date/time of the field (with some predefined order for standard fields). This is the default ordering method for most databases, and salesforce.com is no exception.
VivoVivo

I've finally went back to this problem and realized that the system was pulling in the fields at reverse creation order! So all I had to do was reorder the map in reverse and then get the order that I want (As long as I made the fields in the correct order.)