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
vishal yadav 62vishal yadav 62 

AggregateResult

Hi,
this is code in php 

function get_car_manufacturer()
    {
        $data['car_manufac'] = $this->cardata_model->get_car_manufacturer();
            
        for($i=0; $i<count($data['car_manufac']); $i++){
                
            $fin_data[] = array(
                            'value'=>$data['car_manufac'][$i]['manufacturer'],
                            'label'=>$data['car_manufac'][$i]['manufacturer']
            );
                
        }
            
            echo json_encode($fin_data);
    }
    
    function get_model()
    {
        $data['car_model'] = $this->cardata_model->get_car_model($_POST['manu']);
        
        for($i=0; $i<count($data['car_model']); $i++){
            
                $fin_data[] = array(
                        'value'=>$data['car_model'][$i]['model'],
                        'label'=>$data['car_model'][$i]['model']
                );
            
        }
        
        $_SESSION['car_models'] = json_encode($fin_data);
        
        echo json_encode($fin_data);
    }
    
    public function get_variant()
    {
        $data['car_variant'] = $this->cardata_model->get_car_variant($_POST['model']);
        
        for($i=0; $i<count($data['car_variant']); $i++){
            
                $fin_data[] = array(
                        'value'=>$data['car_variant'][$i]['model_type'].'@@'.$data['car_variant'][$i]['fuel_type'],
                        'label'=>$data['car_variant'][$i]['model_type'].' '.$data['car_variant'][$i]['fuel_type']
                );
            
        }
        
        $_SESSION['car_variant'] = json_encode($fin_data);
        
        echo json_encode($fin_data);
    }
------------------this is apex class---
global with sharing class ExchangeRemoter{
    
    
    public static list<jud_old_cars__c> oldCars{get; set;}
    
    public ExchangeRemoter(){ 
    
    }
    
    @RemoteAction
    global static Map<String, String> get_car_manufacturer(){
        
        //Set<Id> makersId = new Set<Id>();
        //Set<jud_old_cars__c> retCars = new list<jud_old_cars__c>();
        List<Map<String,String>> oldCarList = new  List<Map<String,String>>();
        
        
            list<jud_old_cars__c> oldCars = [Select manufacturer__c from jud_old_cars__c group by manufacturer__c];
        
                                      
        for(jud_old_cars__c manu : oldCars){
           
          //AggregateResult manus = [select manufacturer__c FROM jud_old_cars__c GROUP BY manufacturer__c];
          
            oldCarList.add(new Map<String, String> { 
                                         'value'=>manu.manufacturer__c,
                                         'label'=>manu.manufacturer__c
                                         });
          
            
            

        }
        return oldCarList;
    }
}



i'm getting error this . i have create object and field ..i want to create a class and give remote .please help me out friends 
Illegal assignment from List<AggregateResult> to List<jud_old_cars__c>
Illegal conversion from List<Map<String,String>> to Map<String,String>
Suraj TripathiSuraj Tripathi

Hi Vishal,

You are returning List<Map<String,String>> to Map<String,String>

If you change your method with this below code then it will work fine.

global static List<Map<String,String>> get_car_manufacturer(){

OR if you want to return like this

global static Map<String, String> get_car_manufacturer(){


then you have to change some code.

global with sharing class ExchangeRemoter{
    public static list<jud_old_cars__c> oldCars{get; set;}
    
    public ExchangeRemoter(){ }
    
    @RemoteAction
    global static Map<String, String> get_car_manufacturer(){
        
        Map<String, String> oldCarList = new  Map<String, String>();
        list<jud_old_cars__c> oldCars = [Select manufacturer__c from jud_old_cars__c group by manufacturer__c];           
        for(jud_old_cars__c manu : oldCars){
			oldCarList.put('value'=>manu.manufacturer__c);
			oldCarList.put('label'=>manu.manufacturer__c);
        }
        return oldCarList;
    }
}

Hope it will help you.

Please mark as a best if It helps you.

Regards,

Suraj

vishal yadav 62vishal yadav 62
Hi Suraj,
it's not wofking .i'm getting error

Unknown method 'ExchangeRemoter.show()'
Illegal assignment from List<AggregateResult> to List<jud_old_cars__c>
Illegal conversion from List<Map<String,String>> to Map<String,String>
help me out
Suraj TripathiSuraj Tripathi

Hi vishal,

Your given code there is no show() method

problem is occuring in your show() method please check have you created this or not.
 

vishal yadav 62vishal yadav 62
Hi suraj,

this is my compleet code 
global with sharing class ExchangeRemoter{

    public static list<jud_old_cars__c> oldCars{get; set;}
    
    public ExchangeRemoter(){ 
    
    }
    
    
    
    @RemoteAction
    global static List<Map<String,String>> get_car_manufacturer(){
        
        //Set<Id> makersId = new Set<Id>();
        //Set<jud_old_cars__c> retCars = new list<jud_old_cars__c>();
        List<Map<String,String>> oldCarList = new  List<Map<String,String>>();
        
         AggregateResult manus = [select manufacturer__c FROM jud_old_cars__c GROUP BY manufacturer__c];
            list<jud_old_cars__c> oldCars = [Select manufacturer__c from jud_old_cars__c group by manufacturer__c];
                for(jud_old_cars__c manu : oldCars){
           
          
            oldCarList.add(new Map<String, String> { 
                                         'value'=>manu.manufacturer__c,
                                         'label'=>manu.manufacturer__c
                                         });
          
        }
        return oldCarList;
    }
}
-----------------------------------
i'm getting two error.
Unknown method 'ExchangeRemoter.show()'  
Illegal assignment from List<AggregateResult> to List<jud_old_cars__c>

not getting how to do?
pls help me out asap
srlawr uksrlawr uk
This line: 
 
list<jud_old_cars__c> oldCars = [Select manufacturer__c from jud_old_cars__c group by manufacturer__c];

Will return List<AggregateResult> - because you have a "Group By" clause in your query.

You cannot therefore put it into a List<jud_old_cars__c> as this is different.

You must put it into a List<AggregateResult>
 
list<AggregateResult> oldCars = [Select manufacturer__c from jud_old_cars__c group by manufacturer__c];

Then in your loop / future code etc. you will have to GET the AggregateResult row from your results and process it accordingly with the methods available as per the docs here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm


 
vishal yadav 62vishal yadav 62
Hi srlawr uk,
still i'm getting another two  error .pls help me out friends.
error is 
Unknown method 'ExchangeRemoter.show()'
Invalid loop variable type expected AggregateResult was jud_old_cars__c
srlawr uksrlawr uk
Well, as Suraj Tripathi said, you don't have a method called show() in your controller, which I can only imagine you are calling from somewhere completely different in this scenario (as you don't make reference to the call even in your posted code examples) !!!

I would have a look about for where you are invoking this method from and post another question perhaps to work out what that is doing.

Regarding the invalid loop variable, you can't just change the definition of "oldcars" - you will need to change the type of the for loop assignment, and read the documentation I linked to on AggregateFunctions

Here's the next line...
for(AggregateResult manu : oldCars){

bear in mind, manu is now NOT a jud_old_cars__c it is an AggregateResult object! ;)