function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
osf_teamosf_team 

Creating Apex Class and Trigger using SOAP API

Hi,

My requirement is to create ApexTrigger in Salesforce dynamically after taking the required inputs from the user.

Is it possible using the SOAP API "create" operation since ApexClass and ApexTrigger are standard objects in Salesforce, 

Regards
Shalindra Singh
Rahul SharmaRahul Sharma
Do you want them to be created from apex or outside Salesforce?
Shailendra Singh ParmarShailendra Singh Parmar
Hi 
You can do this operation using metadata api, ant, eclipse and other tool use that api.
https://developer.salesforce.com/page/An_Introduction_to_Force.com_Metadata

Thanks!
Rahul SharmaRahul Sharma
Programatically, you can use metadata or tooling API.
osf_teamosf_team
I want to do it using Java code. Can you post some sample code on how to do the same.
Rahul SharmaRahul Sharma
You might have some seen most of them already,
Here are some links:
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/intro_soap_overview.htm
http://salesforce.stackexchange.com/questions/57478/can-i-save-a-new-apex-class-using-tooling-api-in-java
http://salesforce.stackexchange.com/questions/31734/how-to-get-the-content-of-an-existing-apex-class-using-metadata-api
osf_teamosf_team
Hi,

Thanks for your reply. I am able to create Apex Class and Apex Trigger dynamically using Metadata API.

I am implementing the following things:
  1. I'll take input from the user, such as version no., object name (eg, Account, Contact) trigger event (eg, before insert, after insert etc.), url for publishing the messages etc.
  2. Based on the input, I'll create an Apex Class and Apex Trigger in Salesforce.
  3. Whenever any event occurs in Salesforce (such as a new Account is created), the trigger will be executed, and it will publish the message to the url that user has provided.
 I want to know what are the permission levels for Apex Trigger and Apex class ? For example:
  1. If the user is integrating salesforce with my application and he is sales person and not the administrator of salesforce account, would he still be able to create trigger and classes?
  2. If my application ask for salesforce credential and user enters the credential of salesforce which is in production, would my application still be able to create apex trigger and apex class on behalf of user?
  3. Also, Is there any other limitation that Metadata API exposes on creating Apex Classes/Apex Triggers?
Thanks
Shalindra Singh
Dheeraj SharmaDheeraj Sharma
I have mentioned my comment below ur queries please let me know if it helps or u have something to add.
  1. If the user is integrating salesforce with my application and he is sales person and not the administrator of salesforce account, would he still be able to create trigger and classes?
    • I guess apex code execute in system mode so that particular user has access to that Vf and he able to execute that piace of code then, yes apex class will get generated.
  2. If my application ask for salesforce credential and user enters the credential of salesforce which is in production, would my application still be able to create apex trigger and apex class on behalf of user?​
  • In production environment I don't think user will able to create apex class. or you can check below link(https://www.sundoginteractive.com/blog/creating-classes-and-triggers-in-apex-using-the-tooling-api)
       3. Also, Is there any other limitation that Metadata API exposes on creating Apex Classes/Apex Triggers?
  • Using metadata api you can create any type of metadata. but I guess it won't allow us to update any managed code file which is quite understandable.