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
mangiamangia 

How to create a trigger programmatically?

I would like to be able to create a trigger programmatically. Is this possible and if so, using APEX code, APEX Web Service code, or AppExchange Web Service API? Example code would be helpful too. Thanks very much.
RickyGRickyG
You cannot create a trigger programmatically.  However, you could create a trigger that would only fire when certain conditions are met.

Hope this helps.


SuperfellSuperfell
You can create a trigger programmatically via the API using the compileTriggers operation in the Apex WSDL/API. You cannot create a trigger from Apex Code.
mangiamangia
Thanks Rick and Simon for your responses. I am using the enterprise10.wsdl and I do not see the compileTrigger method nor is it documented in the Apex Web Services API. However, I do see the compileTrigger breifly explained in the Apex Code Language Reference. How do I reference and call the compileTrigger method? What am I missing here?
SuperfellSuperfell
There's a separate service for managing apex code, goto the Apex API page in setup and download the Apex WSDL.
SunukrishSunukrish

Hi Simon!

 

I have imported the apex wsdl to eclipse and using the setter and getter methods avaliable in the corresponding classes trying to create a trigger. The classes in the apex wsdl CompileTriggerResult and compileTriggers compiles the existing triggers and does not create a trigger from scratch. So, this would mean that we cannot programmatically create trigger even by using apex wsdl.Please correct, if I am wrong.

SuperfellSuperfell

you call compileTriggers passing in the new trigger(s) you want to compile as the parameter.

SunukrishSunukrish

Thanks Simon!

 

We were able to create triggers programmatically.

SethuramanSethuraman

Hi Sunitha,

 

Can you please explain the steps to create triggers programatically ??

 

Our scenario is, User will Select the Object, then select the fields in it (custom + standard) and also the trigger condition (that is after,before create/update/delete). Our Apex code should create a trigger with all the above said details.

 

It would be great if you can help us by explaining the steps to create triggers programatically. Thanks.

 

Regards,

Sethuraman.AR

 

 

Santhi PrabhuSanthi Prabhu
Hi @Sunukrish, 

Can you provide me the solution approach pls?