質問

I want to create trigger dynamically in my apex class. Can anyone here help me.. Please guide me for this. I am fresher for visual force pages

役に立ちましたか?

解決

You cannot created trigger dynamically in Apex. Because Apex code has no access to the Trigger object So, you can not create triggers programmatically. Anyways we never need to create trigger dynamically. Look here: http://boards.developerforce.com/t5/Apex-Code-Development/Create-Trigger-dynamically/td-p/667868

Sample apex code to create a trigger by Tooling API endpoint using REST callout:

        String json = '{ "Name" : "COTrigger", \'+
                             '"TableEnumOrId" : "Custom_Object__c",'+
                             '"Body" : "trigger COTrigger on Custom_Object__c (after insert) { // Do Something }" }'; // JSON format to create trigger

            Httprequest req = new HttpRequest();
            req.setEndpoint('https://[salesforce instance].salesforce.com/services/data/v27.0/sobjects/ApexTrigger');
            req.setMethod('POST');
            req.setHeader('Content-Type':'application/json');
            req.setHeader('Authorization':'Bearer: '+sessionId);
            req.setBody(json);

        Http httpReq = new HttpReq();
        HttpResponse res = httpReq.send(req); 
        System.debug(res.getBody());

Correct some syntax error, Tooling API is basically a set of Objects, components accessible through it. Try this code, Actually I used this code to create Apex class not Apex Trigger and here i just changed body & endpoint to make it work for trigger. If it doesn't work it means creating Trigger from Tooling API is still not supported.

Read this guide http://www.salesforce.com/us/developer/docs/api_toolingpre/api_tooling.pdf It has all about tooling API and not any complex configuration is required to do this. You only need to REST callout on endpoint url to create trigger. Endpoint url are provided in guide, of which link i have given.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top