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 

Need help with a trigger

Hi,

I am trying to write a trigger that will call the class whenever an update is made to the record.  I believe I am doing this wrong and I have many problems.  How do I write this trigger?

My class: 
public class DrivingDistance1 {
    public static void m1() {
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 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).distance1__c = distance;
        destinations.get(i).distance1__c = distance;
    }
    update accList;
    }
}

My trigger:
trigger DrivingDistanceTrigger on Account (after update) {
  geopointe__Geocode__r.geopointe__Latitude__c[] = Trigger.new;
    DrivingDistance1.m1(distance);
}
Raj VakatiRaj Vakati
trigger DrivingDistanceTrigger on Account (after update) {
  List<geopointe__Geocode__c>  lisOfGeo= Trigger.new;
    DrivingDistance1.m1(lisOfGeo);
}
 
public class DrivingDistance1 {
    public static void m1(List<geopointe__Geocode__c>  lisOfGeo) {
geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
//  Write Other logic here 
//Loop through all the List Of geopointe__Geocode__c 
}

 
RatanRatan
I am not sure about the logic that you are implementing but few things need to be noticed. 
first since your trigger on Account. Trigger.New will return List of Account so you have to pass list of account from trigger to handler 

 
trigger DrivingDistanceTrigger on Account (after update) 
{
    DrivingDistance1.m1(Trigger.New);//pass trigger.new it is list of account
}

And your method should accept the list of account
 
public class DrivingDistance1 
{
    public static void m1(List<Account>  lstAccount) 
   {
        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 
                                    WHERE Id IN: lstAccount];

        //  Write your logic here.
   }
}

 
Kathryn BullockKathryn Bullock
I have tried both methods and the code is still not updating.  Is there something wrong with my logic?