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 

Something wrong with my logic?

Hi,

I am trying to return the road miles with the Geopointe Distance Services.  Unfortunately, I believe my logic must be wrong.  The code is not working the way I was hoping.  What am I doing wrong?

Class:
public class DrivingDistance1 {
    public static void m1(List<geopointe__Geocode__c> lisofGeo) {
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
List<Account> accList = [SELECT id, 
                         distance1__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];
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).distance1__c = distance;
        destinations.get(i).distance1__c = distance;
    }
    update accList;
    }
}

Trigger:
trigger DrivingDistanceTrigger on Account (before insert) {
	DrivingDistance1.m1(Trigger.New);
}

Chandra@AtlantaChandra@Atlanta
Hi Kathryn Bullock,

Yes, there is an issue with logic,

 for(Integer i = 0; i < origins.size(); i++)

i = 0, 2, 4, 6, 8 , 10 etc. contains the origin

i = 1, 3, 5, 7, 9, 11 etc., contains the destination

so, for "ds.add" you need to populate source, descriptionation that is i, i+1, also your "for loop" must have a logic to handle the # of account records (example, what happens when you have 9 accounts, that means last origin will not have destination right?). once your do this correction and execute the logic, it will work fine.

By Falcon100 (If this was helpful, please give feedback)

 
Kathryn BullockKathryn Bullock
So I am hoping that this code will pull the origin as the account and the destination as the lookup to the UCO Service Provider.  The number of accounts should be 1, is this correct?  Should I change my SOQL query to only reference those FROM Account LIMIT 1 or FROM Account WHERE Id IN: Account?
Chandra@AtlantaChandra@Atlanta
Kathryn,

Just to make sure, I understand right.

you are trying to calculate the distance from origin (trigger account) to destination (UCO Service Provider another account) right?

if yes,

   1)  then query the UCO Service Provider account record with filter in SOQL to get one record.

   2) you don't need loops, because your origin is accessible through trigger new and destination is only one record.

   3) calculation distance.

if no,

   please provide me more details

By Falcon100 (if this is helpful, please provide feedback)
 
Kathryn BullockKathryn Bullock
The UCO Service Provider is a lookup field to a custom object called a Facility.  Will this work the same as the Account?
Chandra@AtlantaChandra@Atlanta
Kathryn,

I created a sample Facility custom object with lookup to Account and used below query to access fields from both the tables

select id, name, Account__r.id, Account__r.Name
from
facility__c
where
 Account__r.Name like 'E%'

with the above query, you can get fields from UCO Service Provider, then use the API to get the distance. you no need to loop through the accounts.

I hope this helps.

By Falcon100 (if this is helpful, please provide feedback)