-3

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.

4
  • 4
    You've given us some code, a brief description of what it does, and the beginnings of a test class, but what is your question? Where are you stuck? edit your post to add that information.
    – Derek F
    Jan 3, 2022 at 13:42
  • @Derek All the corrections you've asked has been made in the question.
    – Nithin
    Jan 3, 2022 at 16:22
  • What is your question? Is there a problem with your test class? Please edit your post to include more detail. Jan 3, 2022 at 16:26
  • You should ask for your accounts to be merged. Because you've used an alternate account here, your edit needs to be approved.
    – Derek F
    Jan 3, 2022 at 16:29

2 Answers 2

1

As with most things "testing", there's not much that's special about testing this (invocableMethod/invocableVariable) compared to "normal" code.

You still need to insert the appropriate records so that your test can run.
You still need to call the method that you are stressing with this particular test.
You still need to make assertions to validate the results of running your code.

So you'll be calling DistanceRecord.getAccount(), and your test needs to create and pass a List<DistanceRecord.FlowInputs>. When using inner classes outside of the class it's defined in (like in a unit test), you need to add the outer class name.

So in the end, all you really need to do is something like

List<DistanceRecord.FlowInputs> inputs = new List<DistanceRecord.FlowInputs>();
DistanceRecord.FlowInputs input = new DistanceRecord.FlowInputs();
input.AccountId = <your test account id>;
inputs.add(input);

DistanceRecord.getAccount(inputs);
1

Nithin just wanted to know if it is possible to call methods from the Apex Class if he/she wants to test them. Response yes. Nevertheless, some errors can impact "cross method". You just need to Class.Method(parameters); and it works.

this is my code, I used that and it worked.

my class:

 public without sharing class Convert15to18 {
  //beginning
  public static String convert15to18DigitId(String fifteenDigit) {
    //String fifteenDigit = '0SO90000000PBDu';
    if (String.isEmpty(fifteenDigit))
      return null;
    if (fifteenDigit.length() != 15)
      return '-1';
    return fifteenDigit + stringToSubstring(fifteenDigit);
  }

  public static String stringToSubstring(String myString) {
    List<String> myList = new List<String>();
    for (Integer i = 0; i < 11; i += 5) {
      myList.add(myString.substring(i, i + 5));
    }
    return stringToNumber(myList);
  }

  public static String stringToNumber(List<String> myList) {
    List<String> listNum = new List<String>();
    for (String str: myList) {
      String temp = '';
      for (Integer i = 0; i < 5; i++) {
        temp += ((str.substring(i, i + 1).isAllUpperCase()) ? '1' : '0');
      }
      listNum.add(temp);
    }
    return calculate5bits(listNum);
  }

  public static String calculate5bits(List<String> myList) {
    List<Integer> bitsCalc = new List<Integer>();
    Integer count;
    String temp;
    for (String str: myList) {
      count = 0;
      for (Integer i = 0; i < 5; i++) {
        temp = str.substring(i, i + 1);
        count += Integer.valueOf(temp) * math.pow(2, i).intValue();
      }
      bitsCalc.add(count);
    }
    return numToString(bitsCalc);
  }

  public static String numToString(List<Integer> listNum) {
    String converterString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
    String result = '';
    for (Integer num : listNum) {
      result += converterString.substring(num, num + 1);
    }
    return result;
  }
}

my test class:

 @isTest
private with sharing class Convert15to18Test {
  @isTest
  public static void testConverter15to18() {
    String fifteenDigit = '0SO90000000PBDu';
    String eighteenDigit = Convert15to18.convert15to18DigitId(fifteenDigit);
    System.assertEquals(
      '0SO90000000PBDuGAO',
      eighteenDigit,
      'Returned string does not match with expected string.'
    );

    String fifteenDigits = '0055f000005PM7z';
    String eighteenDigits = Convert15to18.convert15to18DigitId(fifteenDigits);
    System.assertEquals(
      '0055f000005PM7zAAG',
      eighteenDigits,
      'Returned string does not match with expected string.'
    );
  }
}

I hope that will help you !

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .