3

If I create a class in Anonymous window and use it's object in a method.

Will it force to run all the test classes in production?

I need to schedule a class, so I have to implement schedulable. However, I am not in position to "deploy" this class, as it will run the test classes.

Thanks..

2
  • I first suggest you try it in sandbox as I don't believe e you can use a class that implements scheduable that only exists is exec anon but would be interesting if you could. This concerns me though "However, I am not in position to "deploy" this class, as it will run the test classes." And seems to be your real problem
    – Eric
    May 20 '17 at 13:00
  • I tried that, it does the job.. Class is schedules.. and also no class is created. I can use it's object as well.. Yes you are right, that's my concern
    – mvk1991
    May 20 '17 at 14:05
3

Will it force to run all the test classes in production?

No. Execute Anonymous code is compiled, immediately executed, and discarded. No unit tests are run.

I need to schedule a class, so I have to implement schedulable. However, I am not in position to "deploy" this class, as it will run the test classes.

Schedulable, Batchable, Queueable, and @future all require an ApexClassId. Execute Anonymous code does not have an Id, because they are compiled, executed, and immediately discarded, without ever being committed to the database.

You will need to deploy your code if you want a new Schedulable class in production. You would need to fix any problems that are preventing you from deploying to production.

Depending on what you need to do, you could build a Process Builder to fire off periodically to do whatever it is you want to do. This could include calling a flow to perform slightly more complicated logic if you needed to, but if you can't build it in a flow, then you may not have any other choice but to fix your deployment.

4
  • According to the OP the scheduler class works and schedules the batch so the OP is saying it can be done which seems to be in direct conflict with my understanding and your answer as well. Hmmmm...
    – Eric
    May 21 '17 at 0:03
  • Yes, Eric.. I could see the job in Scheduled Jobs.. and they are queued as well.. and running fine as well... showing completed in the queue... (I am doing this in login.salesforce.com .. but in a developer liscence created for trailhead)
    – mvk1991
    May 21 '17 at 2:09
  • @mvk1991 You may see it "scheduled" but does it actually execute? I somehow doubt that the class being called will still exist when the scheduled time arrives. You say it runs "fine", but is that immediately or at a later time? I'd view "immediately" as comparable to a test class executing upon test.stopTest & 15 minutes or an hour from now differently. Please share your code.
    – crmprogdev
    May 21 '17 at 12:53
  • I can see it queued at the scheduled time, and completed at the scheduled time. You may also try doing this.
    – mvk1991
    May 21 '17 at 14:43
0

Let's rephrase your question a bit before I try to answer it.

"Can I create a class in Execute Anonymous, skip creating a test class & running all test classes to get test class coverage, and then run a Schedulable class that call methods from it to use it's objects?"

I hope I've paraphrased your question reasonably well.

When one runs code using Execute Anonymous, new code only exists during the execution context of the operation. It doesn't persist and isn't intended to persist.

Allowing you to create a Class that persisted beyond the execution context of the running code would be counter to the purposes of Execute Anonymous. It would allow you to completely bypass the security integrity of the platform. If a class doesn't exist in the code base that's been compiled when a scheduled code is run "in the future" (as in at a different time than the current execution context), it won't exist in the org to be called from a schedulable class.

If you were permitted to do this, you could create any kind of malicious code and schedule it to run in system mode on a periodic basis that would hardly leave a trace of the actual code that ran. To me, this would be the equivalent of allowing someone to set a virus loose to run inside an org!

As an example, one could easily create code in a class to delete the last 10 accounts accessed, it's related opportunities, related contacts and other related records according to when the "anonymous class" is scheduled to run. The code for the class that does this wouldn't be visible for inspection. Only the schedulable that calls it would be visible in the code base. There'd be no test method and no running user. The security exposure would be enormous!

2
  • This is something Salesforce should think on :)
    – mvk1991
    May 21 '17 at 2:14
  • @mvk1991 I think they have. Thus the reason they don't allow these things to happen. See my comments below fox's answer.
    – crmprogdev
    May 21 '17 at 12:55

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.