Apex Class
public class DistanceRecord {
public class FlowInputs {
@invocableVariable public String AccountId;
}
@InvocableMethod
public static void getAcct(List<FlowInputs> requests){
Account a = [
SELECT BillingLatitude,BillingLongitude
FROM Account
WHERE Id =: requests[0].AccountId
];
Location loc = Location.newInstance(a.BillingLatitude,a.BillingLongitude);
List<Account> accountList = [SELECT BillingLatitude,BillingLongitude FROM Account WHERE Id!=:requests[0].AccountId];
Integer numAccLess30=0;
Integer numAccMore50=0;
Integer numAccBetween3050=0;
for(Account acc:accountList){
if(Location.getDistance(loc, Location.newInstance(acc.BillingLatitude,acc.BillingLongitude), 'km')>=30 && Location.getDistance(loc, Location.newInstance(acc.BillingLatitude,acc.BillingLongitude), 'km')<=50){
numAccBetween3050++;
}else if(Location.getDistance(loc, Location.newInstance(acc.BillingLatitude,acc.BillingLongitude), 'km')<30){
numAccLess30++;
}else{
numAccMore50++;
}
}
a.Less_than_30__c=numAccLess30;
a.More_than_50__c=numAccMore50;
a.Between_30_50__c=numAccBetween3050;
update a;
System.debug('Less than 30 ' +numAccLess30);
System.debug('More than 50 ' +numAccMore50);
System.debug('Between 30 to 50 ' +numAccBetween3050);
}
}
This is an apex class I have written to find the accounts within 50 km range to a closedWon account. Here I have obtained the closedwon opportunity account through a flow and passed the Id of the account to the apex class as an input in the apex action. And I have written a code to fetch the Ids. Later I have set three counter values and for each distance range within my closedwon Account(<30,30-50,>50)the counter value increments. Then I have stored these counter values into three custom fields in my Account object.
Now I have to write a test class for this.
Test Class
@isTest
public class TestClass {
@isTest static void distancerecord(){
String accountNameNew = 'TEST' ;
String AccountName='Acme' ;
Account testAcct = new Account() ;
testAcct.Name = AccountName;
testAcct.Phone = '(415)555-1212';
testAcct.NumberOfEmployees = 100;
Insert testAcct ;
Account acctQuery = [select Id, Name from Account where Id = :testAcct.id ] ;
My question is how to call the methods and variables from the apex class. I am bit confused on how to invoke those and continue the testing. I have written the test class according to the things I know. Any suggestions please.