I need to run the below query for 200 fields changing the field each time and then copy the resulting most recent date.Is there any way to do this quicker than doing it 1 by 1?
SELECT name, CreatedDate FROM Account WHERE name!= null ORDER BY createdDate DESC NULLS FIRST
I tried to do this using an apex class:
public class Allfields {
public void getallFieldValues(){
Account account = new Account();
// get all account fields from database schema
Schema.SObjectType objectType = account.getSObjectType();
set<String> accountFields = objectType.getDescribe().fields.getMap().keySet();
system.debug(accountfields);
string soql = '';
for(String fld1:accountFields){
soql = 'select '+fld1+',createddate from account ORDER BY CreatedDate DESC NULLS FIRST';
getvalues(soql);
}
}
public void getvalues(String query){
list<Account> newList=new list<Account>();
newList = database.query(query);
System.debug(newList);
}}
There are 500 different fields in the Account object and I am looking for a way to find the last time each of those fields were populated in a record so that I can go ahead and delete the fields that are never used.
But I am getting an error "duplicate field selected: CreatedDate". What am I doing wrong in my code here and is this how it's usually done? Thank you you for your time
CreatedDate
cannot be null, right?