1

I am using Rest API to update the Apex trigger using ApexTriggerMember. I am using the below json string as body in request.

String json = {"MetadataContainerId" : "1dc7F00000DD9UYQA1", "FullName" : "Suggestion__c" , "Body": "{"Name":"RSLC_Suggestion","Body":"trigger RSLC_Suggestion on Suggestion__c (before insert, before update, after insert, after update){ \n    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isDelete) )\n    { \n      RollUpSummaryGenerator.associationOdChildRecordsToParentRecords(trigger.new,'Suggestion','SuggestionNumber','Contact','SuggestioNumbers'); \n    } \n}","tableEnumOrId":"Suggestion__c"}"}

I am receiving the below response

String response = "[{"message":"No such column 'MetadataContainerId' on sobject of type ApexTrigger","errorCode":"INVALID_FIELD"}]"

Can anyone take a look at this issue?

public static void createApexTriggerMember(String metadataContainerId, String apexClassId, string classBody)
    {
       // String body = '{"MetadataContainerId" : "'+metadataContainerId+'", "ContentEntityId" : "'+apexClassId+'", "FullName" : "'+'\'Suggestion__c\' , "Body": "'+classBody+'"}';
        String body = '{"MetadataContainerId" : "'+metadataContainerId+'", "FullName" : "'+'Suggestion__c" , "Body": "'+classBody+'"}';
        Http objHTTP = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:Tooling_API_Credential/services/data/v44.0/tooling/sobjects/ApexTriggerMember');
        req.setBody(body);
        req.setMethod('POST');
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        Http h = new Http();

        String response = h.send(req).getBody();
        System.debug('response= '+response);
        Map<String,Object> rmp = (Map<String,Object>)JSON.deserializeuntyped(response);
        String apmid = (String)rmp.get('id');
      //  createContainerAsyncRequest(metadataContainerId);
    }

I am getting an additional error as below:

{"message":"Unexpected character ('N' (code 78)): was expecting comma to separate OBJECT entries at [line:1, column:15]","errorCode":"JSON_PARSER_ERROR"}

Is there no one in this salesforce ecosystem who can help me out?

I tried correcting my json. Now I am receiving the below error.

DEBUG|response= [{"message":"No such column 'ContentEntityId ' on sobject of type ApexTriggerMember","errorCode":"INVALID_FIELD"}]

1

From the documentation of ApexTriggerMember usage section:

To edit, save, or compile an Apex trigger, create an ApexTriggerMember object that references it. To create a trigger, use the REST API or the Metadata API.

You cannot create a Trigger using Tooling API. you can only update it.

Creating a trigger using Rest Api is even easier, don't have to create a MetadataContainer or create ApexTriggerMember with complex parameters. Just Insert ApexTrigger with Name, Body and TableEnumOrId and you are sorted:

Sample Post Request :

Endpoint: /services/data/v44.0/sobjects/ApexTrigger

{"Name" : "MyTrigger" , "Body": "Trigger MyTrigger on Account(after insert){}" ,"TableEnumOrId":"Account"}

Apex Code :

    String body = '{"Name" : "MyTrigger" , "Body": "Trigger MyTrigger on Account(after insert){}" ,"TableEnumOrId":"Account"}';
    Http objHTTP = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:Tooling_API_Credential/services/data/v44.0/sobjects/ApexTrigger');
    req.setBody(body);
    req.setMethod('POST');
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');
    Http h = new Http();

    String response = h.send(req).getBody();
    System.debug('response= '+response);
    Map<String,Object> rmp = (Map<String,Object>)JSON.deserializeuntyped(response);
    String apmid = (String)rmp.get('id');

Src : https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_apextriggermember.htm


UPDATE : To update the trigger, you have to provide the id of the existing trigger in ContentEntityId

So your request to create ApexTriggerMember will be :

{"MetadataContainerId" : "1dc1l000000BN8IAAW", "FullName" : "MyTrigger" , "Body": "Trigger MyTrigger on Account(after insert,after update){}" , "ContentEntityId": "01q1l000000CnSoAAK"}

This gets success respone, and then you can post to ContainerAsyncRequest endpoint to update trigger.

  • Hey I am able to create trigger using tooling API. I am facing issues updating the trigger using ApexTriggerMember in tooling API. Below is the response I am getting [{"message":"No such column 'ContentEntityId ' on sobject of type ApexTriggerMember","errorCode":"INVALID_FIELD"}] – Yogesh Patil Jan 23 at 5:47
  • @YogeshPatil shown an example to update trigger. – Pranay Jaiswal Jan 23 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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