4

i was trying create a visualforce page from Developer Console using Tooling API ( REST Service ). I checked the documentation it doesn't clarify about the parameters which needs to be present. Whenever i make a call i am getting Status as "Bad Request". I guess i am missing to send some information but i am not able it figure it out.

Please find the code below using which i'm trying to create Visualforce Page.

String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();

String url =  salesforceHost + '/services/data/v29.0/sobjects/ApexPage';

HttpRequest req = new HttpRequest();

String classBody = '<apex:page></apex:page>';

req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setBody('{'+
        '"Body" : "' + classBody +'"'+ '"Name" : "MyNewVFPage"' + '}');            

Http http = new Http();

HTTPResponse res = http.send(req);
System.debug(res);

1 Answer 1

7

You can use the below code to create a Page. ApexPage and ApexComponent have markup defined and not body. Also there are other fields that are required while creating a page like the masterLabel and controllerType (this specifies what type of controller is associated with the page i.e., 0=> no Controller, 1=>StandardController, 2=>StandardSet and 3=>custom controller).

String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();

String url =  salesforceHost + '/services/data/v29.0/sobjects/ApexPage';

HttpRequest req = new HttpRequest();

req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
//for controllerType = >0 -- no controller
req.setBody('{"Name" : "TestPageFromRest","Markup" : "<apex:page>hello</apex:page>","ControllerType" : "0","MasterLabel":"TestPageFromRest","ApiVersion":"29.0"}');

//for controllerType => 1 -- Standard controller + extensions        
//req.setBody('{"Name" : "TestPageFromRestCase","Markup" : "<apex:page standardController=\'case\' extensions=\'displaycase\'>hello</apex:page>","ControllerType" : "1","MasterLabel":"TestPageFromRestCase","ApiVersion":"29.0"}');

//for controllerType => 3 --custom Controller
req.setBody('{"Name" : "TestPageFromRestCase1","Markup" : "<apex:page controller=\'displaycase\'>hello</apex:page>","ControllerType" : "3","MasterLabel":"TestPageFromRestCase1","ApiVersion":"29.0"}');            
Http http = new Http();

HTTPResponse res = http.send(req);
System.debug(res.getBody());
7
  • Ah great!! Thanks...i tried with Name and Markup ( By looking into ApexPage Class) it didn't work...I Guess other parameters are mandatory then...Is there any document where i can access the information regarding this? Jan 7, 2014 at 7:05
  • Actually I figured the mandatory fields by looking at the error message (res.getBody()). :)
    – Sam
    Jan 7, 2014 at 7:08
  • Oops...My bad!! I Actually tried it in workbench it was throwing parse error i didn't try to print that in console :) Jan 7, 2014 at 7:10
  • Could you please share how we can set the value for ControllerType in case "Standard Controller", "Customer Controller" and "Extension"?...I tried with different set of number as mentioned above and putting appropriate syntax in the markup but it doesn't work Jan 16, 2014 at 6:13
  • Even added the added the ControllerKey value to controller name which will be fine...But it doesn't add that in the page attribute. If i try to add then it'll throw parse error. Jan 16, 2014 at 6:33

You must log in to answer this question.

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