I am working on the Metadata API to create an Apex trigger dynamically. I have used Apex Wrapper Salesforce Metadata API from Github- https://github.com/financialforcedev/apex-mdapi.

The error is- System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_TYPE: This type of object is not available for this organization faultcode=sf:INVALID_TYPE faultactor=

  • 2
    Questions about implementation issues need to include a specific description of the problem (including exact errors and stack traces, if any) and the code or metadata to reproduce the problem. – identigral Apr 3 at 7:43

The Tooling API is simpler to use because it is JSON string based and so does not require loads of generated Apex code to be used. Note though that it does not quite support everything that the Metadata API does.

But the good news here is that the Tooling API does support trigger creation e.g.:

private String createTrigger(String name, String sourceCode, SObjectType type) {

    Map<String, String> body = new Map<String, String>{
        'Name' => name,
        'TableEnumOrId' => String.valueOf(type),
        'Body' => sourceCode

    String url = ''
        + Url.getSalesforceBaseUrl().toExternalForm()
        + '/services/data/'
        + 'v48.0'
        + '/sobjects/'
        + 'ApexTrigger';

    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');

    HttpResponse res = new Http().send(req);

    return String.format(
        'Result of {0} POST is statusCode={1} body={2}',
        new Object[] {url, res.getStatusCode(), res.getBody().abbreviate(1000)}
| improve this answer | |
  • Thank you very much, Keith. It worked perfectly and I learned a new thing. :) – Sunad Rasane Apr 4 at 8:07
  • Glad it helped @SunadRasane. – Keith C Apr 4 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.