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

How to pass an object field into a method for execution in a query

I am currently running this query which runs fine:


List<AggregateResult> lst =[select  Max(DistrictOneRecordCnt__c)maxvalue from Utility_Permit__c];


However, I want to refactor the code so that I can pass the "DistrictOneRecordCnt__c" field into the method that has this query and thereby make the method more generic.  Any and all help in this matter is appreciated.




Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal



I think you are aksing for below.


Public Static List<AggregateResult> getMaxValue(String strField, String strObject){
	String query = 'select  Max(' + strField + ')maxvalue from ' + strObject;
	List<AggregateResult> lst = Database.query(query);
	return lst;