I am trying to update a class using REST API which i created before using same REST API .
public static void createClass() {
// Note the escaping on newlines and quotes
String classBody = 'public class MyNewClass {\\n'
+ ' public string SayHello() {\\n'
+ ' return \'Hello World\';\\n'
+ ' }\\n'
+ '}';
HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl + 'sobjects/ApexClass');
req.setMethod('POST');
// OAuth header
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setBody('{'+
'"Body" : "' + classBody +'"'+
'}');
Http h = new Http();
HttpResponse res = h.send(req);
System.debug('*****'+res.getBody());
// Response to a create should be 201
if (res.getStatusCode() != 201) {
System.debug(res.getBody());
}
}
I could successfully create a new class with above code. Now i want to update the same class with PATCH method.
public static void updateClass() {
// Note the escaping on newlines and quotes
String classBody = 'public class MyNewClass {\\n'
+ ' public string SayHello() {\\n'
+ ' return \'Hello World update\';\\n'
+ ' }\\n'
+ '}';
HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl + 'sobjects/ApexClass/01p9000000417SnAAI?_HttpMethod=PATCH');
req.setMethod('POST');
// OAuth header
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setBody('{'+
'"Body" : "' + classBody +'"'+
'}');
Http h = new Http();
HttpResponse res = h.send(req);
System.debug('*****'+res.getBody());
// Response to a create should be 201
if (res.getStatusCode() != 201) {
System.debug(res.getBody());
}
}
I have hardcoded the ID of apex class for now (for testing).
Request string in debug log looks like this :
System.HttpRequest[Endpoint=https://c.ap1.visual.force.com/services/data/v28.0/tooling/sobjects/ApexClass/01p9000000417SnAAI?_HttpMethod=PATCH, Method=POST]
and response says "BAD request" and the error string looks like this:
[{"fields":[],"message":"insufficient access rights on cross-reference id","errorCode":"INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY"}]
Am I missing something here when trying to update?
Or is there any better way to to these operations like Tooling API for which i don't find good explanation or documentation with examples.
And i want to update a class which need not be compiled because i want to update it with previous compiled/saved code .