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
Milan VadhiaMilan Vadhia 

How to write test class for apex class which has Geopointe Distance service api?

Hello I have written test class for following apex trigger. But it is giving an error. Can anyone help me with the same?

Trigger
 
trigger AccountTrigger on Account (after insert, after update, before insert, before update) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            AccountTrigger_Helper obj_AccountTrigger_Helper = new AccountTrigger_Helper();
            obj_AccountTrigger_Helper.getGeoLocation(Trigger.new);
        }
    }
 if(System.IsBatch() == false && System.isFuture() == false){ 
  if(Trigger.isAfter){
        if(Trigger.isupdate){
            AccountTrigger_Helper obj_AccountTrigger_Helper = new AccountTrigger_Helper();
            obj_AccountTrigger_Helper.getGeoLocation(Trigger.new);
        }
    }
    }
}
Trigger Handler Apex:
 
global class AccountTrigger_Helper {
    
    public void getGeoLocation(List<Account> listAccountNew){
          getGeoLocationfuture(listAccountNew[0].Id); 
    }
    
    @future (callout=true)
    public static void getGeoLocationfuture(Id AccountId){
        
        //Get all the account records available in salesforce, using Id received in parameter of future method.
        String account_query = 'SELECT id, distance__c, geopointe__Geocode__r.geopointe__Latitude__c, geopointe__Geocode__r.geopointe__Longitude__c, description'; 
                account_query += ' FROM Account WHERE Id != \''+AccountId+'\' LIMIT 10000';
        
        List<Account> accList = database.query(account_query);
        
        //Get Newly inserted Account Record, which fired this trigger, using Id received in parameter of future method.
        List<Account> NewListAccount = [SELECT id,
                                               distance__c,
                                               geopointe__Geocode__r.geopointe__Latitude__c,
                                               geopointe__Geocode__r.geopointe__Longitude__c,
                                               description 
                                          FROM Account
                                         WHERE Id =: AccountId];
        
        Account objAccount = NewListAccount[0];
        
        //geopointe instance
        geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
        
        //Add Newly inserted Account as origin for all instances. and add other accounts as Destination using for loop variable.
        for(Integer i = 0; i < accList.size(); i++){
            ds.add((Double)objAccount.geopointe__Geocode__r.geopointe__Latitude__c,
                        (Double)objAccount.geopointe__Geocode__r.geopointe__Longitude__c,
                        (Double)accList.get(i).geopointe__Geocode__r.geopointe__Latitude__c,
                        (Double)accList.get(i).geopointe__Geocode__r.geopointe__Longitude__c);
        }
        
        /******************************************************************/
        List<Nearest_Accounts__c> nearest_Accounts_insert_list = new List<Nearest_Accounts__c>();
        List<Account_Distance_Wrapper> get_Distance_Sorted_list = new List<Account_Distance_Wrapper>();
        
        for(Integer i = 0; i < accList.size(); i++){
            System.debug('=====accList======'+accList[i]);
            Double Distance = ds.getDistanceAtIndex(i);
            System.debug('=====Distance======'+Distance);
            Account_Distance_Wrapper AccDistanceWrapper = new Account_Distance_Wrapper(accList[i].Id, AccountId, Distance);
            
            get_Distance_Sorted_list.add(AccDistanceWrapper);
        }
        
        get_Distance_Sorted_list.sort();
        
        for(Integer i = 0; i < 10; i++){
            Nearest_Accounts__c objNearestAccount = new Nearest_Accounts__c();
            
            objNearestAccount.Account__c = get_Distance_Sorted_list[i].Account_Id;
            objNearestAccount.Near_By_Account__c = get_Distance_Sorted_list[i].NearAccountId;
            objNearestAccount.Distance__c = get_Distance_Sorted_list[i].Distance;
            
            System.debug('=====objNearestAccount======'+objNearestAccount);
            nearest_Accounts_insert_list.add(objNearestAccount);
        }
        
        if(nearest_Accounts_insert_list.size() > 0){
            insert nearest_Accounts_insert_list;
        }
    }
}
Test class which I wrote but giving an error:
 
@isTest
private class Test_Account_Trigger {

	private static testMethod void Account_Trigger_Test() {
	    
	    List<Account> listAccount = new List<Account>();
	    List<geopointe__Geocode__c> listGeopointe = new List<geopointe__Geocode__c>();
	    
	    Account acc = new Account(Name='mapAccount');
	    insert acc;
	    
	    for(Integer i = 0; i < 20; i++){
	        Account objAccouont = new Account();
	        objAccouont.Name = 'Test Account '+i;
	       // objAccouont.geopointe__Geocode__c = listGeopointe[i].Id;
            
            listAccount.add(objAccouont);
	    }
	    insert listAccount;
	    
	    for(Integer i = 0; i < 20; i++){
    	    geopointe__Geocode__c insert_geopointe = new geopointe__Geocode__c();
    	    insert_geopointe.geopointe__Latitude__c = 23.0645200 + i - 0.5;
    	    insert_geopointe.geopointe__Longitude__c = 72.5427600;
    	    insert_geopointe.geopointe__Map_Object__c = 'account_all';
    	    insert_geopointe.geopointe__Parent_Record_Id2__c = listAccount[i].Id;
    	    insert_geopointe.geopointe__Parent_Record_ID__c = listAccount[i].Id;
    	    
    	    listGeopointe.add(insert_geopointe);
	    }
	    insert listGeopointe;
	    
	    for(Integer i = 0; i < listAccount.size(); i++){
	        listAccount[i].geopointe__Geocode__c = listGeopointe[i].Id;
	    }
	    update listAccount;
	    
	    test.startTest();
	    
	    Account insertAccount = new Account();
	    
	    insertAccount.Name = 'Test Insert';
	    insertAccount.geopointe__Geocode__c = listGeopointe[10].Id;
	    
	    insert insertAccount;
	    
	    insertAccount.Name = 'Test Update';
	    update insertAccount;
	    
	    test.stopTest();
	}

}

Please, Help me to solve this error. Thank you.

 
Sushil Josan 3Sushil Josan 3
Hi Milan,

Any luck on this ?
I am having the same kind of requriement.

Please help if you got the solution.

Thanks,
Sushil
sushil@co-frame.com