5

I come to know that to update Apex component it is required to use Metdatacontainer object with ApexClassMember/ApexTrigger member. In Force.com Tooling API Dev guide given all examples to use Metadatacontainer and updating ApexClassMember and other members, but there all given examples uses SOAP API. In REST API only syntax and hints are provided for creating, deleting, describe but no implementation. I am able create a Metadatcontainer object and receive its id as response and by same way ApexClassMember, but don't know how to use it with Metadatacontainer.

My Question is how to include and work with ApexClassMember with Metadatcontainer or associating ApexClassMember/ApexTriggerMember with a Metadacontainer object using REST API?

1 Answer 1

3

If your using Java, there are two blogs you can refer to and reference code which creates an ApexClassMember and set its MetadataContainerId property via the REST API.

Here is a code sample from first...

//First, the MetadataContainer
JSONObject metadataContainerRequest = new JSONObject();
metadataContainerRequest.put("Name", "SaveClass" + id); // Any unique name
JSONObject metadataContainerResponse = ToolingApi.post(
        "sobjects/MetadataContainer", metadataContainerRequest);
System.out.println("MetadataContainer id: "
        + metadataContainerResponse.get("id"));

// Then an ApexClassMember with our updated code
JSONObject apexClassMemberRequest = new JSONObject();
apexClassMemberRequest.put("MetadataContainerId",
        metadataContainerResponse.get("id"));
apexClassMemberRequest.put("ContentEntityId", id);
apexClassMemberRequest.put("Body", body);
JSONObject apexClassMemberResponse = ToolingApi.post(
        "sobjects/ApexClassMember", apexClassMemberRequest);
System.out.println("ApexClassMember id: "
        + apexClassMemberResponse.get("id"));

Or using POJO's (serialised to JSON) instead of the JSONObject class from the second...

MetadataContainer container = new MetadataContainer();
container.setName("UnusedApexMethods");
List saveResults = port.create(new ArrayList(Arrays.asList(container)), sessionHeader);
String containerId = saveResults.get(0).getId();

ApexClassMember apexClassMember = new ApexClassMember();
apexClassMember.setBody(apexClass.getBody());
apexClassMember.setContentEntityId(apexClass.getId());
apexClassMember.setMetadataContainerId(containerId);
apexClassMembers.add(apexClassMember);
8
  • (+1) Basically I want to use Tooling API from the raw coding because my implementation is always language independent. I am able to create/delete tooling object but unable to update. Your answer is helpful to me. If you can provide reference for any article/blog where it is implemented from raw code in any language, then please share. Thanks @Andrew
    – Ashwani
    Sep 8, 2013 at 13:27
  • I will try to help, can you explain what you mean by raw coding? Do you mean the actual JSON data representation? Sep 8, 2013 at 15:32
  • Yes, I mean what mappings should be used to update a ApexTriggerMember or ApexClassMember. In mean while I was reading redcurrantscloud.blogspot.in/2013/09/… here details updating a apex class in Salesforce provided, but json mappings are creating confusion. If you can help in mappings of Json data than it will be very helpful to me. Thanks @Andrew
    – Ashwani
    Sep 8, 2013 at 15:40
  • 1
    The two samples above map by name, its how they translate themselves to JSON internally, so when you see apexClassMemberRequest.put("MetadataContainerId", 'idvalue') or apexClassMember.setMetadataContainerId('idvalue') they will result in metadataContainerId : 'idvalue' being output. Sep 8, 2013 at 16:49
  • Thanks for answer. I understood, basically apexClassMemberRequest.put("key", "value"); internally creates a json like {"key" : "value" } which is to be send in request body. Am I correct?
    – Ashwani
    Sep 10, 2013 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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