0

I am trying to pass a dynamic value in the List to store the data from a respective object. But, this code is throwing me an error - 'Unexpected <'. Please help.

public static void delRecords(List<Object> sfdata, string objApi){

   List<Schema.getGlobalDescribe().get(objApi)> sData = sfdata;
   
   delete sfdata;
   System.debug(sfdata.Id);
}

1
  • You cannot use dynamic typing in apex; it is a statically typed language. The best you can do is hold a list of a base class. Here that would be a List<SObject>. If possible provide the list as List<SObject> to the method otherwise you will have to write a loop converting each entry in the input list of Object to an SObject before adding it to the List<SObject>, which is relatively slow and inefficient but necessary.
    – Phil W
    Commented Jul 28, 2022 at 6:36

2 Answers 2

3

You cannot use dynamic typing in Apex; it is a statically typed language. The best you can do is hold a list of a base class. Here that would be a List<SObject> to pass to the delete DML operation. SObject is a base class for every SObject type (such as Account or MyCustomObject__c). Note that SObject is a subclass of Object (the ultimate base class for all Apex classes) which is why, if the data in appropriate, you can cast Object to SObject.

If possible provide the list as List<SObject> to the method otherwise you will have to write a loop converting each entry in the input list of Object to an SObject before adding it to the List<SObject>, which is relatively slow and inefficient but necessary.

public static void delRecords(List<Object> sfdata) {
   List<SObject> sData = new List<SObject>();

   for (Object entry : sfdata) {
       sData.add((SObject) entry);
   }
   
   delete sData;
}

Note that if the sfdata includes any instances that are NOT some form of SObject this function will throw an exception when trying to perform the cast in the for loop.

1
  • As always, you ROCK @Phil!!!!. I got what I exactly wanted. You are truly an Expert. It takes ages for me to get to the solution. :( Commented Jul 28, 2022 at 8:23
1

Although it doesn't help in this situation, and Phil W's answer is the correct one, I got here when looking at how to List with a dynamic type, and it turns out it IS possible by using Type.forName.

For example, the following is possible:

Type.forName( 'List<' + objApi + '>' ).newInstance()

This means you can, for example:

List<Object> dynamicList = (List<Object>)Type.forName( 'List<' + objApi + '>' ).newInstance();

If you know a common ancestor for all the possible types, you could cast to that type, for example, if you know that objApi is the name of an SObject:

List<SObject> dynamicList = (List<SObject>)Type.forName( 'List<' + objApi + '>' ).newInstance();

However, what you can't do is dynamically specify the type of the variable itself.

I.E. that's nothing equivalent to:

List<objApi> dynamicList = (List<SObject>)Type.forName( 'List<' + objApi + '>' ).newInstance();
2
  • Thanks Rob.. It helps. But unfortunately, I can mark only 1 answer :( Commented Mar 13, 2023 at 12:33
  • 1
    No worries - it's not about the ticks, it's about being helpful! Commented Mar 14, 2023 at 18:23

You must log in to answer this question.

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