1

I am able to create a trigger from a class using toolingAPI. Can I update this trigger using the Tooling API? Also, is it possible to update an existing visualforce page using the Tooling API? If so, can you please share the relevant code. I am sharing the code I used to create the trigger.

public class ToolingTrigger{

    public ToolingTrigger(VF_Page_Update_Controller controller) {

    }

    private static String baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/tooling/';

    public static void createTrigger() {

        String json = '{ "Name" : "TestAccountTrigger", "TableEnumOrId" : "Account", "Body" : "trigger TestAccountTrigger on Account (before insert) {}" }'; 

        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()); 
    }
}
3

Tooling API support update also. It is regardless of Component type.

All you need to do is create "MetdataContainer" using Tooling API.

  • Create Metadata Container
  • Get the Trigger Id

  • Change the body and add it to MetadataContainer.

  • Perform the update call to MetdataContainer.

Look at following blog post section "Update Apex Class". Same way you can update Triggers. It is a long post so can't post everything here.

Below is the way to create MetdataContainer which hold all components to update (copied from blog):

public void createMetadataContainer()
{
    HttpRequest req = createHttpRequest(endpoint+'/sobjects/MetadataContainer','POST');
    req.setBody('{"Name":"ApexContainer"}');
    String response = getResponse(req);
    Map<String,Object> rmp = (Map<String,Object>)JSON.deserializeuntyped(response);
    String cid = (String)rmp.get('id'); // used to save containerId
}
  • Regardless of component type? Surely not all of them are updateable? – Adrian Larson Jan 17 '17 at 18:18
  • All the components which you can update via other deployment tools and Tooling API support. However, Tooling API support all components. @AdrianLarson – Ashwani Jan 17 '17 at 18:20
  • Hi Ashwani, thanks for your answer, but the link to your blog post is not working. Can you please check once and provide me the correct link. When I click on this one, it says Page not found. I would really like to go through the blog.Kindly provide me the correct link and where to find the section defined. – SFDC_CRM Jan 18 '17 at 4:45
  • @SFDC_CRM try this wp.me/p4aLv9-13 – Ashwani Jan 18 '17 at 4:49
  • Hi Ashwani, When I am trying to createMetadataContainer using above code, I am getting the error Error: Compile Error: unexpected token: 'Map' Can you please help me out? – SFDC_CRM Jan 18 '17 at 7:46
0

I've experimented with this in our DX project and created a plugin for the SFDX CLI. I've posted it in this gist.

For our use case it did not do exactly what we wanted, but perhaps someone else can benefit from it. In a DX project, this mechanism will result in changes in the scratch org (changed timestamps), which have to be pulled into source control, which is an undesirable step. We've opted for the mechanism of first tweaking the source on disk, then pushing that code, doing the import, and then reverting back the changes using Git and pushing the original code again:

$ sed -i "s/<status>Active<\/status>/<status>Inactive<\/status>/" app/main/default/triggers/*-meta.xml
$ sfdx force:source:push -u org
# run import tool
$ git checkout -- app/main/default/triggers/*-meta.xml
$ sfdx force:source:push -u org

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.