I am trying to edit an ApexPage using a ToolingAPI and containing a ToolingAPI.ApexPageMember. I have created a ToolingAPI class based on @AndyFawcett (https://salesforce.stackexchange.com/users/286/andrew-fawcett) the author of ToolingAPI.cls. I have changed the code for ApexPageMember class as suggested by another user here.

public class ApexPageMember extends SObject_x  implements ISerialize {
    public String            body;
    public String            content;
    public ApexPage          contentEntity;
    public String            contentEntityId;
    public DateTime          lastSyncDate;
    public Metadata          metadata;
    public MetadataContainer metadataContainer;
    public Id                metadataContainerId;
    public ApexPageMember() {
    public override void serialize(JSONGenerator jsonGen) {
            jsonGen.writeStringField('body', body);
            jsonGen.writeStringField('content', content);
            jsonGen.writeObjectField('contentEntity', contentEntity);
            jsonGen.writeStringField('contentEntityId', contentEntityId);
            jsonGen.writeDateTimeField('lastSyncDate', lastSyncDate);
            jsonGen.writeObjectField('metadata', metadata);
            jsonGen.writeObjectField('metadataContainer', metadataContainer);
            jsonGen.writeStringField('metadataContainerId', metadataContainerId);

In order to call this class, I need three Ids, MetadataContainerId, ContentEntityId and Body. As far as I understand ContentEntityID is the ID of the Visualforce page to be updated, Body is the body of the Visualforce page. My questions are:

  1. How can I get the MetadataContainerID?
  2. If I call the class like this, I should put the Body of the VF page in apexPageMember.Body and id of the page in ContentEntityID. How to get the MetadataContainerID?

    ToolingAPI.ApexPageMember apexPageMember = new ToolingAPI.ApexPageMember(); // Create ApexPageMember and associate them with the    MetadataContainer     
    apexPageMember.Body                = fileSaveBody;
    apexPageMember.ContentEntityId     = item.id;
    apexPageMember.MetadataContainerId = containerId;      
    ToolingAPI.SaveResult apexClassMemberSaveResult =  tool.createSObject(apexPageMember);

Please explain in simple steps and in detail since I am new to Tooling API.

1 Answer 1


You need to create the MetadataContainer via a separate API call. Example in Apex from Going Native with the Apex UML Tool and Tooling API!:

string yourContainerName = 'ApexNavigator';

ToolingApi tooling = new ToolingApi();
List<MetadataContainer> containers = (List<MetadataContainer>)
        'SELECT Id, Name FROM MetadataContainer WHERE Name = \''+yourContainerName+'\'').records;
if(containers!=null &&  containers.size()>0) {
    tooling.deleteSObject(ToolingAPI.SObjectType.MetadataContainer, containers[0].Id);

// Create MetadataContainer
ToolingAPI.MetadataContainer container = new ToolingAPI.MetadataContainer();
container.name = yourContainerName;
ToolingAPI.SaveResult containerSaveResult = tooling.createSObject(container);
if(!containerSaveResult.success) {
    throw makeException(containerSaveResult);
Id containerId = containerSaveResult.id;

Then use the containerId for your MetadataContainerId.

  • Thank you so much for your answer.It is very helpful. I was able to generate the MetadataContainer ID with this code. However, the Visualforce page I am trying to update is not getting updated. Can you tell me what I am doing wrong? I am calling this class from the developer console in the following way:
    – SFDC_CRM
    Jan 19, 2017 at 7:50
  • The code I am using to call: ToolingApi tool = new ToolingApi(); ToolingAPI.ApexPageMember apexPageMember = new ToolingAPI.ApexPageMember(); apexPageMember.Body = 'abc'; //updated body apexPageMember.ContentEntityId = '06690000005eAf5';//id of page apexPageMember.MetadataContainerId = '1dc90000000NRkqAAG'; //id of metadata container ToolingAPI.SaveResult apexPageMemberSaveResult = tool.createSObject(apexPageMember);
    – SFDC_CRM
    Jan 19, 2017 at 7:51
  • @SFDC_CRM What does the apexPageMemberSaveResult say happened? Jan 19, 2017 at 20:13
  • 1
    I found out that it is necessary to have a createContainerAsyncRequest method and call it with the MetadataContainerID to finally update the page. Thank you so much for your help.
    – SFDC_CRM
    Jan 20, 2017 at 9:01

You must log in to answer this question.

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