Strip comments from anonymous apex before executing (if document exceeds the allowed maximum with comments)

Issue #2211 new
Mark Mindenhall created an issue

I had a fairly complex operation I needed to perform on ~1000 records. I decided that trying to make the modifications with Data Loader would be much more time consuming and painful than just doing it with anonymous Apex. So I decided to do this:

  1. Execute a DB query in another system, getting back two columns of UUIDs that need to be remapped
  2. Paste the data into my text editor, and use regular expressions to format it for use in the Apex Map literal
  3. Copy and paste the whole set of map keys/values into a file that I can execute as anonymous Apex
  4. Write and debug the code to manipulate the data as required
  5. Let 'er rip!

The code would look something like this:

Map<String, String> idMap = new Map<String, String>{
    '88c9e6cf-b36d-48da-ba69-21770fabff15' => 'b0c1817e-de8b-4a91-90d2-16f09ae4b965',
    // 1000+ more rows!
};
System.debug('Map create succeeded!');

// Some Apex code to query for records matching map keys, 
// then iterate and update the results with with map values

When I first tried to run the code, I got this error:

Exceeds the maximum allowed anonymous Apex script length: 96611 > 32000

OK, I thought. I’ll just have to comment out a bunch of rows in the Map literal, and do this in smaller batches. However, when I commented out the rows, I still got the same result.

Getting to the point now. When running anonymous Apex, it would be awesome if you could strip out comments before sending the code to Salesforce for execution. This would allow me to have all of the data I’m working with in a single large file, but then comment out sections of the data to run smaller batches that fit within the ~32k limit.

Comments (3)

  1. Scott Wells repo owner

    Hi. It’s quite unlikely I’d get to this in the near future given how specific the audience would be, but in the interim you can use the IDE’s native regular expression search-and-replace to get rid of line comments very quickly even in the Anonymous Apex tool window. Just use Ctrl/Cmd+R to start a search and replace, toggle regular expression to enabled, and enter a search pattern of //.*$ and an empty replacement string, then replace all. It won’t remove blank lines, but it will remove the line comment text which is likely spilling you over the Anonymous Apex limit.

    Obviously you wouldn’t want to do that with your “master” version since you’re selectively commenting things you otherwise want, but that should help you to do what you need pretty quickly.

  2. Log in to comment