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
Pooja GargPooja Garg 

How to use Tooling API to create dynamic Triggers in Apex code

Hi, 

   I have a requirement of creating dynamic triggers for Custom Objects which are also being created dynamicaly through MetaData API . 

 

I search and came to know that Tooling API can help me creating dynamic triggers for REST API but  I am not getting how to configure this to my project and use it.....................

 

 

Can I get some example of creating dynamic Triggers through Tooling API..........................

 

Well I tried 

 

String json = '{"Name" : "COTrigger", \'"TableEnumOrId" : "Inventory__c"\',\'"Body" : "trigger COTrigger on Inventory__c (after insert) { }"\'}'; 

LoginResult loginResult = binding.login('pooja.avantsoft59@gmail.com', 'Test1234@');

        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://ap1.salesforce.com/services/data/v28.0/tooling/sobjects/ApexTrigger');
req.setMethod('POST');        
req.setHeader('Content-Type','application/json');
       
        req.setHeader('Authorization','OAuth 00D90000000nlxu!AQIAQCuO8z08yJ1hMR_4.OEL_RYoBwKaWMK4ttOpLevsJBRqFIptV2lkenMZIXObFS3n3EtJmNBjjyWGF8ZM7xp_DX9IEHNw');
         
req.setBody(json);

        Http httpReq = new Http();
        HttpResponse res = httpReq.send(req); 
        System.debug('hii'+res.getBody());

 which always give me error as "16:24:52:435 USER_DEBUG [16]|DEBUG|hii[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]"

 

Although I checked and copy pasted Session ID correctly still it gives me this error again n again.... 

 

Any kind of help would be appreciated.

 

Thanks in advance...!!!

Pooja

parashermohitparashermohit

its authentication issue,you need to change parameter oauth .Its issue with your parameter.there is some other parameter while connecting with rest.