0
        public class DynamicSOQL 
    {

            public static List<Sobject> Soql (String ObjectName, List<String> apiNames,Set<Object> IdsToQuery )
        {
            String query;
            String SobjectApiName = objectName;
            String allstring = string.join(apiNames,',');

            query = 'SELECT ' + allstring + ' FROM ' + SobjectApiName + ' WHERE ID = :IdsToQuery';
            Sobject[] sObjectList = Database.query(query);
            return sObjectList;
            }
    }

I am calling this class in the following way:

    List<Account> accountsToMerge = (List<Account>)DynamicSOQL.SOQL('Account', apiNames, listOfIds);

This is returning the following error message

Method does not exist or incorrect signature: void SOQL(String, List<String>, List<String>) from the type DynamicSOQL (24:72)

I have a hunch I am not casting correctly

5
  • 1
    It's right there in the error, isn't it? void SOQL(String, List<String>, List<String>) (what you're trying to call) is different from void SOQL(String, List<String>, Set<Object>) (the method signature you have). The last parameter should be a Set instead of a List, no?
    – Derek F
    Feb 27 '20 at 2:12
  • @DerekF Correct! Feb 27 '20 at 2:28
  • I thought that the Object type would be dynamic Feb 27 '20 at 2:30
  • 1
    Object is one of the top-level types in Apex (not sure where null and the special Any type fit in the type system), and is thus abstract to the point of practical uselessness...at least to us mere mortal users.
    – Derek F
    Feb 27 '20 at 2:38
  • The fact that Object is an abstractish base class doesn't actually matter much because the concrete collection type is different. List<anything> != Set<anything>.
    – David Reed
    Feb 27 '20 at 2:53
2

The issue is that the signature of your method void SOQL(String, List<String>, Set<Object>)
is different from the signature of your call void SOQL(String, List<String>, List<String>)

There is a constructor for Set that takes a List (and vice-versa), but you cannot assign a Set to a List, nor a List to a Set, nor will explicit type casting work.

With all of that in mind, and a little extra, the minimum amount of change you'd need to make here is to alter the signature of your call so that the third parameter is a Set<Object>.

List<Account> accountsToMerge = (List<Account>)DynamicSOQL.SOQL(
    'Account', 
    apiNames, 
    // We need to first cast listOfIds as a List<Object>
    // We can then pass it into the constructor for a Set<Object>
    new Set<Object>((List<Object>)listOfIds)
);

That said, I don't think using a Set<Object> has any advantage here over, say, Set<Id>. I'd reconsider your use of Set<Object>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.