3

I'm writing a test class right now, and I'm calling a method in the class that I am wanting to test. The method in the main class that I'm wanting to test is called getTruckerAvailability(). Here is what it looks like.

@auraEnabled 
public static List<Truck_Availability__c> getTruckerAvailability(String[] bundleids, String region, Decimal arr){ 

 //Code in here...
  return ...
 }

The issue I'm having is I don't know the syntax for passing in bundles to the string array. Bundle__c is an object in my org, and there are bundle records. So the array in that method above is receiving bundle record Ids as strings.

In my test class, what is the syntax for passing in actual ids that are strings? Here is what I have.

  @IsTest static void testTruckAvailability(){
    //Create new bundle 1
    Bundle__c b = new Bundle__c();
    b.Name = 'Test bundle';
    b.Active__c = TRUE;
    insert b;

    //Create new bundle
    Bundle__c b2 = new Bundle__c();
    b2.Name = 'Test bundle';
    b2.Active__c = TRUE;
    insert b2;

    List<Truck_Availability__c> newClassCall = myClass.getTruckAvailability( {b.Id, b2.Id}, 'North America', 12000.00);

     }

I get errors when I format the array of strings as {b.Id, b2.Id}. I've tried to create a 2 new string variables and set the ids to those string variables with String.ValueOf(b.Id) or String.ValueOf(b2.Id). I just can't find anywhere online what the correct syntax would be for passing in an array of strings to a method call in a test class.

2 Answers 2

5

Unlike some other languages you may be familiar with, you're required to construct a new List:

List<Truck_Availability__c> newClassCall = myClass.getTruckAvailability( 
    new String[] {b.Id, b2.Id}, 'North America', 12000.00);

Note: it is generally never necessary to convert a String to an Id via String.valueOf or Id.valueOf, as conversion happens automatically. Do be aware, of course, that assigning a String that is not an Id to an Id is a runtime error:

Id i = ''; // runtime error.
Id i2 = '001000000000ACB'; // Okay
Id i3 = '001000000000123AAA'; // Also okay
String s1 = i3; // Okay, no conversion necessary
4
  • 1
    But if you do want to check that a String is a valid Id, you can use instanceof, e.g. if (myIdString instanceof Id) doSomething();
    – Charles T
    Commented May 23, 2018 at 2:44
  • @CharlesT This only works on later API versions; there's a versioned bug where some non-ID values would identify as an ID. I think version 34 or so.
    – sfdcfox
    Commented May 23, 2018 at 2:51
  • Hah good to know! Fortunately I have not had any need to write code any earlier than API 36 which is the version that came out the year I started Salesforcing :)
    – Charles T
    Commented May 23, 2018 at 2:54
  • That was exactly what I needed. Thanks for the very clear explanation on this! I really appreciate it. Commented May 23, 2018 at 14:28
2

You could also use the following syntax

List<Truck_Availability__c> newClassCall = myClass.getTruckAvailability(new List<String>(){b.Id, b2.Id}, 'North America', 12000.00);

Id and String datatypes in Apex do not require an explicit typecasting from one to another and vice versa using String.valueOf(Id) or Id.valueOf(String). However, one should be careful while leveraging Id as String particularly in the context of an API based integration using Javascript.

An API always always responds with a 18 digit ID. An Id type to hold the 18 character ID is capable of implicitly converting it to its 15-digit counterpart when required but String types are not capable of doing so. Hence, its best to use Salesforce Record Ids as an Id type unless there is a real reason to use them as Strings such as in cases where only primitive types may be supported.

1
  • I'm pretty sure the reason, though, is that Lightning still has issues with the ID type in certain scenarios. It's been a while, though, so I'm not sure if they ever got to fixing it.
    – sfdcfox
    Commented May 23, 2018 at 2:59

You must log in to answer this question.

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