I am already aware that you cannot directly create Apex classes in production orgs (have to use either the Force.com IDE or changesets). But if I include test cases can the Tooling API do it?

Also related question; can the Tooling API define Security Settings for classes/pages it creates as well?

  • I suspect this maybe possible providing you create a ApexClassMember for the two classes and utilise an MetadataContainer to deploy them together. I'll try and give this a go today and confirm. In the meantime, i have done this successfully with the Metadata API here, github.com/afawcett/declarative-lookup-rollup-summaries/blob/… – Andrew Fawcett Apr 8 '14 at 10:17
  • I assume btw you want to do this from Apex, as such I'll be trying this out with the Apex Tooling API, github.com/afawcett/apex-toolingapi, do let me know if this is not what your after, but either way the steps will be the same in other languages. – Andrew Fawcett Apr 8 '14 at 10:18
  • I've added a confirmation comment to the answer you've received below, sadly the answer is no from the Tooling API. – Andrew Fawcett Apr 8 '14 at 14:27

Apologies if this is not the most definitive or conclusive answer, however I notice on page 5 of the Force.com Tooling API Developer's Guide It states:

Use create() to compile Apex classes or triggers in Developer Edition or sandbox organizations.

This would suggest to me that it may not be possible to create APEX classes in production with the Tooling API- but that is only my inference it could just refer to the specific method - No doubt someone more knowledgeable on the subject would be able to clarify this.

  • But at the bottom of the guide for ApexExecutionOverlayAction it says: "Use ApexExecutionOverlayAction to overlay a diagnostic output on an Apex class or trigger without compromising production code." I'm not clear if this is the only class that works in production. – Mossi Apr 7 '14 at 5:25
  • 2
    I can confirm this is indeed the case, the Tooling API can only be used to update or delete existing ApexClass's. The only way to create an ApexClass or ApexTrigger for that matter in a production org dynamically that i know is via the Salesforce Metadata API 'deploy' operation, as per this example, github.com/afawcett/declarative-lookup-rollup-summaries/blob/…. It is unclear if the new Spring'14 Metadata API 'createMetadata' operation will support this (it's async version 'create' does not). – Andrew Fawcett Apr 8 '14 at 14:26

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.