+ Start a Discussion
RamkumarVTRRamkumarVTR 

Visual Force page with controller

I have a requirement where i need to get the object name and field api name as a input and get the agreegate results where the values doesnt exist in system with counts. i have written the code as below mentioned, can you please evaluate and confirm is it right.
 
<apex:page standardController="InvalidPicklistObjectType__c" extensions="InvalidPicklistReportController">  
    <style type="text/css">
        body {background: #F3F3EC; padding-top: 15px}
    </style>
    <apex:form >
        <apex:pageBlock title="Search for Invalid Picklist Value" id="block" mode="edit">
            <apex:pageMessages />
				<apex:pageBlockSection >
					<apex:pageBlockSectionItem >
						<apex:selectList value="{!Object API Name}" size="1" id="Object_API_Name__c">
							<apex:actionSupport event="onchange" reRender="newvalue" />
							<apex:selectOptions value="{!InvalidPicklistObjectType__c.Object_API_Name__c}"/>
						</apex:selectList>
						<apex:selectList value="{!Field API Name}" size="1" id="Field_API_Name__c">
							<apex:actionSupport event="onchange" reRender="newvalue" />
							<apex:selectOptions value="{!InvalidPicklistObjectType__c.Field_API_Name__c}"/>
						</apex:selectList>
                    <apex:commandButton value="Search" action="{!search}" rerender="resultsBlock" status="status"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:actionStatus id="status" startText="Generating Report... please wait..."/>
            <apex:pageBlockSection id="resultsBlock" columns="1">
                <apex:pageBlockTable value="{!searchResults}" var="o" rendered="{!NOT(ISNULL(searchResults))}">                                      
                    </apex:column>
                    <apex:column value="{!o.Object_API_Name__c}"/>
                    <apex:column value="{!o.Field_API_Name__c}"/>
                    <apex:column value="{!o.InvalidPicklistValues__c}"/>
					<apex:column value="{!o.Count__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class InvalidPicklistReportController {

//added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
    // the actual account
    private Account a;
    // the results from the search. do not init the results or a blank rows show up initially on page load
    public List<ObjectFieldType__c> searchResults {get;set;}

    // the text in the search box
    public string searchText {
        get {
            if (searchText == null) searchText = 'Acme'; // prefill the serach box for ease of use
            return searchText;
        }
        set;
    }

    public InvalidPicklistReportController(ApexPages.StandardController controller) {

        //initialize the stanrdard controller
        this.controller = controller;
        this.a = (InvalidPicklistObjectType__c)controller.getRecord();

    }

    // fired when the search button is clicked
    public PageReference search() {
        if (searchResults == null) {
            searchResults = new List<ObjectFieldType__c>(); // init the list if it is null
        } else {
            searchResults.clear(); // clear out the current results if they exist
        }
        // Note: you could have achieved the same results as above by just using:
        // searchResults = new List<categoryWrapper>();

        // use some dynamic soql to find the related opportunities by name
		
		String ObjectAPIName = InvalidPicklistObjectType__c.Object_API_Name__c; 
		String FieldAPIName= InvalidPicklistObjectType__c.Field_API_Name__c
		
		List <String> getPicklistValues(ObjectAPIName,FieldAPIName);
		
		AggregateResult[] groupedResults=[SELECT Specialty_1_vod__c, count(Id) FROM Account GROUP BY Specialty_1_vod__c];
		 
		for (AggregateResult ar : groupedResults){

			for (String availablepickval: getPicklistValues){
			
			 if(ar.get('Specialty_1_vod__c') != availablepickval){
			 
				List <ObjectFieldType__c> objectType = new List <ObjectFieldType__c>();
				
                System.debug('Picklist values' + ar1.get('Specialty_1_vod__c'));
                System.debug('Picklist count' + ar.get('expr0'));
                integer myDecimal1 = integer.valueOf(ar.get('expr0'));
                objectType.Count__c=myDecimal1 ;
				objectType.InvalidPicklistValues__c=ar1.get('Specialty_1_vod__c');
				objectType.Field_API_Name__c=ObjectAPIName;
				objectType.Object_API_Name__c=FieldAPIName;
                searchResults.add(objectType);            
                
				}            
			}
		
		}
		
		insert searchResults;
        return null;
}

Public static List<String> getPicklistValues(String Object_API_Name_vod__c,String Field_API_Name_vod__c){ 

			List<String> lstPickvals=new List<String>();
			Schema.SObjectType targetType = Schema.getGlobalDescribe().get(Object_API_Name_vod__c);//From the Object Api name retrieving the SObject
			Sobject Object_name = targetType.newSObject();
			Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
			Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
			Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
			List<Schema.PicklistEntry> pick_list_values = field_map.get(Field_API_Name_vod__c).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
			for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
			lstPickvals.add(a.getValue());//add the value  to our final list
		}

		return lstPickvals;
  
	} 


}