0

I wrote a piece of code to Concatenate certain words but I'm having issues with the code itself. Basically what I'm trying to do is Concatenate a field with "ABC" n times where n = number in another field.

public class AgeCounter {

public static void fillAgeCounter(List<CustomObject__c> records) {
    for(CustomObject__c record : records) {
        record.Age_Counter__c = 'ABC';
        for(Integer i = 1; i < record.ABC__c; i++) {
            record.Age_Counter__c = record.Age_Counter__c + ';' + 'ABC';
        }
        for(Integer i = 1; i < record.XYZ__c; i++) {
            record.Age_Counter__c = record.Age_Counter__c + ';' + 'XYZ';
        }
    }
    update records;
}}

This gives me the error: Line: 3, Column: 24 static can only be used on methods of a top level type

Removing static runs the code but no records are updated in the Custom object.

EX: Custom field ABC = 4 and Custom field XYZ = 2

Expected output after running this class: Field Age Counter gets updated as ABC;ABC;ABC;ABC;XYZ;XYZ

2
  • 1
    is AgeCounter an inner class? that would be why the static method can't be used. You'll need new AgeCounter().fillAgeCounter(myrecords); to invoke an inner class method
    – cropredy
    Jan 20, 2023 at 20:17
  • @cropredy no it's not an inner class. I'm running the code as is in the Execute anon window
    – SWP
    Jan 20, 2023 at 21:53

1 Answer 1

1

Your issue is trying to execute your code inside of Execute Anonymous

Here's a simple example

class MyClass {
  static void myMethod() {}
}

when pasted into Execute Anonymous in Developer Console or (in your IDE), it fails with static can only be used on methods of a top level type.

  • MyClass is actually an inner class to a synthetically-generated top level class.
  • From this answer ...

Note If you're working within an Execute Anonymous window, all declared classes are virtual inner classes.

Thought experiment: Imagine if you just entered into the Developer Console:

Integer i = 0;

and clicked Run. This works. But, by itself in a file, this won't execute because it is not valid Apex. Statements need to be inside of methods or initializers. But if wrapped inside of a class by the tooling, it can execute via the anonymous apex tooling.

To test your code in anonymous apex, paste this:

    CustomObject__c[] records = [SELECT .... FROM CustomObject__c ...];
    for(CustomObject__c record : records) {
        record.Age_Counter__c = 'ABC';
        for(Integer i = 1; i < record.ABC__c; i++) {
            record.Age_Counter__c = record.Age_Counter__c + ';' + 'ABC';
        }
        for(Integer i = 1; i < record.XYZ__c; i++) {
            record.Age_Counter__c = record.Age_Counter__c + ';' + 'XYZ';
        }
    }
    update records;
4
  • pasting the above code results in the error: Loop must iterate over collection. Isn't it already going over the collection of records pulled from the Custom Object?
    – SWP
    Jan 21, 2023 at 0:37
  • my bad - a typo -- fixed
    – cropredy
    Jan 21, 2023 at 5:39
  • Thanks it works. Also I forgot to add the records in the list. That's why it wouldn't iterate over them. Changed CustomObject__c[] records = [SELECT .... FROM CustomObject__c ...]; To List<CustomObject__c[]> records = [SELECT .... FROM CustomObject__c ...];
    – SWP
    Jan 23, 2023 at 19:23
  • xxx[] is the same as List<XXX>; I'm glad this works and apologies again for the typo. As a service to the Community, you should mark the answer as accepted (check mark) if you're satisfied
    – cropredy
    Jan 23, 2023 at 19:29

You must log in to answer this question.

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