0

Reading Can you use generics in Apex? generics are supported in apex. When I attempt to run the below trigger in developer console:

trigger TotalTriggerAccount on Account (after update) {
 
    System.debug('Updating account');
    
    String accountId = ApexPages.currentPage().getParameters().get('id');

    List<Contact> allContacts = [
      SELECT
        *
      FROM
        Contact
      WHERE
        AccountId = :accountId
    ];
    
    for (Contact contact : allContacts) {
      System.debug(contact.Phone);
    }
    
}

to access all contacts for a given account I receive multiple Problems related to the use of generics and the declaration of the array such as:

Unexpected token '<'.
Unexpected token '>'.
Unexpected token ']'.

Is there a stop required in order to use generics and array elements in apex for developer console ?

Update:

By developer console, I refer to:

enter image description here

To add a new trigger from the developer console:

enter image description here

7
  • 1
    You're using some odd terminology (well, odd as in it's not really used in Apex or the documentation or community). Lists, Sets, and Maps are collections (or collection types). About the only thing I see wrong here is your SELECT *. Apex/SOQL doesn't allow wildcard field definition. You need to specify each and every field you want to pull. I'd suggest going through some developer-focused modules on trailhead.salesforce.com to help you get your bearings in Apex. – Derek F Sep 12 '20 at 12:11
  • 1
    Also, you probably aren't going to get any value from ApexPages.currentPage().getParameters().get('id'); inside of a trigger (fetching page parameters is typically used when you're writing a controller or controller extension for a Visualforce page). This is turning into an X-Y problem, I think. – Derek F Sep 12 '20 at 12:19
  • 1
    My point is that they're not called "generics" on the Salesforce platform. It's a term you've picked up from elsewhere/another programming language. It'd be like talking about "templates" in Java. Most people could probably figure out that you really mean "generics", but the harder you make people work to understand your problem, the fewer people are going to bother trying to answer your questions. – Derek F Sep 12 '20 at 13:24
  • 1
    Again, I suggest going through at least a few modules on trailhead.salesforce.com to get your bearings. The Apex Developer Guide is another option if you find trailhead too hand-holdy. – Derek F Sep 12 '20 at 13:24
  • 1
    @DerekF I'm 'going to file -> new -> apex trigger to create a new trigger', please see question update for clarification. – blue-sky Sep 12 '20 at 15:29
3

You cannot define a trigger in Execute Anonymous, which is what causes the other compilation errors. You are generally allowed to use parameterized typing in Execute Anonymous and code units (classes and triggers).

Also, you may be receiving errors because of the * in your field list. SOQL does not support the "all fields" operator found in SQL; you must explicitly list each field you want to retrieve. Because SOQL does not explicitly support this wildcard, it often causes unrelated compilation errors, such as the ones you've mentioned. In fact, writing invalid SOQL almost always causes unrelated compilation errors.

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.