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
Kathryn BullockKathryn Bullock 

Expecting { but was 'for'

Hi,

I am trying to write a class that will find the driving distance to a lookup on the account.  I am recieving this error:

Expecting '{' but was: 'for'

Here is the class:
public class DrivingDistance1 {
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
List<Account> accList = [SELECT id, 
                         distance__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c, 
                         geopointe__Geocode__r.geopointe__Latitude__c, 
                         geopointe__Geocode__r.geopointe__Longitude__c FROM Account LIMIT 1];
List<Account> origins = new List<Account>();
List<Account> destinations = new List<Account>();
    
    for(Integer i = 0; i < accList.size(); i++){
        if(Math.mod(i,2) == 0) {
            origins.add(accList.get(i));
        } else {
            destinations.add(accList.get(i));
        }
    }
    for(Integer i = 0; i < origins.size(); i++){
        ds.add((Double)origins.get(i).geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)origins.get(i).geopointe__Geocode__r.geopointe__Longitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Longitude__c);
    }
    for(Integer i = 0; i < origins.size(); i++){
        Double distance = ds.getDistanceAtIndex(i);
        origins.get(i).distance__c = distance;
        destinations.get(i).distance__c = distance;
    }
    update accList;
}

 
Best Answer chosen by Kathryn Bullock
Raj VakatiRaj Vakati
You need to use  in your code in the method as shown below
 
public class DrivingDistance1 {
	public static void m1(){
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
List<Account> accList = [SELECT id, 
                         distance__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c, 
                         geopointe__Geocode__r.geopointe__Latitude__c, 
                         geopointe__Geocode__r.geopointe__Longitude__c FROM Account LIMIT 1];
List<Account> origins = new List<Account>();
List<Account> destinations = new List<Account>();
    
    for(Integer i = 0; i < accList.size(); i++){
        if(Math.mod(i,2) == 0) {
            origins.add(accList.get(i));
        } else {
            destinations.add(accList.get(i));
        }
    }
    for(Integer i = 0; i < origins.size(); i++){
        ds.add((Double)origins.get(i).geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)origins.get(i).geopointe__Geocode__r.geopointe__Longitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Longitude__c);
    }
    for(Integer i = 0; i < origins.size(); i++){
        Double distance = ds.getDistanceAtIndex(i);
        origins.get(i).distance__c = distance;
        destinations.get(i).distance__c = distance;
    }
    update accList;
}
}

 

All Answers

Raj VakatiRaj Vakati
You need to use  in your code in the method as shown below
 
public class DrivingDistance1 {
	public static void m1(){
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
List<Account> accList = [SELECT id, 
                         distance__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c, 
                         UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c, 
                         geopointe__Geocode__r.geopointe__Latitude__c, 
                         geopointe__Geocode__r.geopointe__Longitude__c FROM Account LIMIT 1];
List<Account> origins = new List<Account>();
List<Account> destinations = new List<Account>();
    
    for(Integer i = 0; i < accList.size(); i++){
        if(Math.mod(i,2) == 0) {
            origins.add(accList.get(i));
        } else {
            destinations.add(accList.get(i));
        }
    }
    for(Integer i = 0; i < origins.size(); i++){
        ds.add((Double)origins.get(i).geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)origins.get(i).geopointe__Geocode__r.geopointe__Longitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Latitude__c,
              (Double)destinations.get(i).UCO_Service_Provider__r.geopointe__Geocode__r.geopointe__Longitude__c);
    }
    for(Integer i = 0; i < origins.size(); i++){
        Double distance = ds.getDistanceAtIndex(i);
        origins.get(i).distance__c = distance;
        destinations.get(i).distance__c = distance;
    }
    update accList;
}
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
Please use your logic inside method .. that solves your problem