3

I am trying to inactive Apex Trigger using tooling API. I don't see any errors, I am getting "Success" message, but Trigger is not getting updated with inactive flag.

String json= '{ "MetadataContainerId":"1dcS0000000fGp7IAE", "ContentEntityId": "01qS0000000YCKZIA4", "Metadata": { "status": "Inactive"} }';
Httprequest req =new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v43.0/tooling/sobjects/ApexTriggerMember/');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','OAuth ' + UserInfo.getSessionId());
req.setMethod('POST');
req.setBody(json);
Http httpReq =new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());

System.HttpRequest[Endpoint=https://yearup--Staging.my.salesforce.com/services/data/v43.0/tooling/sobjects/ApexTriggerMember/, Method=POST] System.HttpResponse[Status=Created, StatusCode=201] DEBUG|{"id":"401S0000000ANl3IAG","success":true,"errors":[],"warnings":[]}

// Create a Trigger
String json = '{ "Name" : "AccountTrigger", "TableEnumOrId" : "Account", "Body" : "trigger AccountTrigger1 on Account (before insert) { for(Account objacc : trigger.new){}}" }';
Httprequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v27.0/sobjects/ApexTrigger');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setBody(json); 
Http httpReq = new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());
// 01q54000000ShJ9AAK

// Create container
Httprequest req =new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v28.0/tooling/sobjects/MetadataContainer/');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','OAuth ' + UserInfo.getSessionId());
req.setMethod('POST');
req.setBody('{"Name":"TriggerMContainer"}');
Http httpReq =new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());
// 1dc54000000bs2bAAA

// Update Trigger
Httprequest req =new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v28.0/tooling/sobjects/ApexTriggerMember/');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','OAuth ' + UserInfo.getSessionId());
req.setMethod('POST');
req.setBody('{"MetadataContainerId":"1dc54000000bs2bAAA","ContentEntityId" : "01q54000000ShJ9AAK", "Metadata": { "status": "Inactive"} }');
Http httpReq =new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());


// Create Async
Httprequest req =new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v28.0/tooling/sobjects/ContainerAsyncRequest/');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','OAuth ' + UserInfo.getSessionId());
req.setMethod('POST');
req.setBody('{"MetadataContainerId" : "1dc54000000bs2bAAA", "isCheckOnly": "false"}');
Http httpReq =new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());
// 1dr54000001zhB4AAI

// Async Status
Httprequest req =new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v28.0/tooling/sobjects/ContainerAsyncRequest/1dr54000001zhB4AAI');
req.setHeader('Content-Type','application/json');
req.setHeader('Authorization','OAuth ' + UserInfo.getSessionId());
req.setMethod('GET');
Http httpReq =new Http();
HttpResponse res = httpReq.send(req);
System.debug(res.getBody());

Error:

enter image description here

2

1 Answer 1

0

Your request to update the ApexTriggerMember of this MetadataContainer succeeds, but that doesn't actually perform a deployment - it stages it in the container. The ApexTrigger Tooling API object itself does not support CRUD operations in an active org.

From the Tooling API documentation,

Use a MetadataContainer as a package for your tool’s workspace. As a user works in the tool, update the ApexClassMember, ApexTriggerMember, ApexPageMember and ApexComponentMember objects in the MetadataContainer and use a ContainerAsyncRequest object to save and deploy changes to the current organization.

You need to use a ContainerAsyncRequest to request a deployment of your container (by supplying its Id in the MetadataContainerId field).

5
  • Thank you, I am trying to creating ContainerAsyncRequest, however, I keep getting deployment error as attached screenshot. do you think, we need to contact support or it just that I am missing anything here?
    – Ajay
    Commented Dec 11, 2019 at 15:15
  • @Ajay Yeah, that's a gack. Nobody outside of Salesforce has the access to tell you exactly what it means.
    – David Reed
    Commented Dec 11, 2019 at 15:29
  • this is what I received from salesforce, "Unfortunately, this behavior is WAD per the current design of the tooling API. Our Product team has confirmed that this is a document BUG and the document needs to be updated."
    – Ajay
    Commented Dec 12, 2019 at 18:50
  • Oh interesting. Do you have a sense of what they're saying they'll update in the docs?
    – David Reed
    Commented Dec 12, 2019 at 19:27
  • I am also surprised, they gave me the link and said that they will be updating this document. developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/…. Did you ever know about this?
    – Ajay
    Commented Dec 12, 2019 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .