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 

Trying to update road miles

Good day all,

I am trying to create a class and a trigger that will return the road miles between the Facility associated with the Account and the account.  Unfortunately, it is not working.  Any ideas on what I am doing wrong?

Class:
public class GeoCodeRoadMiles {
    public void RoadMiles(List<Account> Id) {
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
List<Account> accList = [SELECT id, distance__c, geopointe__Geocode__r.geopointe__Latitude__c, geopointe__Geocode__r.geopointe__Longitude__c FROM Account LIMIT 1];
List<Account> origins = [SELECT id, Facility__c  FROM Account];
List<Account> destinations = new List<Account>();

// Split up data into origins and destinations
    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));
   }
    }

// Add the latitude and longitudes for the origin destination pairs
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).geopointe__Geocode__r.geopointe__Latitude__c,
        (Double)destinations.get(i).geopointe__Geocode__r.geopointe__Longitude__c);
}

// Get the distance results and update desired fields
for(Integer i = 0; i < origins.size(); i++){
    Double distance = ds.getDistanceAtIndex(i);
    String.valueof(origins.get(i).distance__c = distance);
    String.valueof(destinations.get(i).distance__c = distance);
}
update accList;
    }}

Trigger:
trigger RoadMiles on Account (after insert, after update, before insert, before update) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
        	GeoCodeRoadMiles RoadMiles = new GeoCodeRoadMiles();
        	RoadMiles.RoadMiles(Trigger.new);
    	}
    }
    if(System.IsBatch()  == false && System.isFuture()  == false){
        if(Trigger.isAfter){
            if(Trigger.isUpdate){
                GeoCodeRoadMiles RoadMiles = new GeoCodeRoadMiles();
                RoadMiles.RoadMiles(Trigger.new);
            }
        }
    }
}

Gururaj BGururaj B
There are few issue i observed in your code:
1. The method definition is expecting the list of account id but you are passing the list of account from the trigger.
2. The Id that is expected as input in your method is not used anywhere but you are querying on Account without any condition which will return entire account records.
3. I don’t understand the way you are defining the list of origin and destination. You are just making 2 list of accounts picking alternate records from initial accList. 
4. If i assume correctly the destination list of account should be the facility account list. So you have to get the facility list and use that id to get the facility account list.
5. updating the distance__c field on origin and destination list but you are using update on accList which has no updates to be made.

I hope if you address these you should get what you wanted. Please mark as best answer if it helped you in any ways.
Thanks