Update StaticResource via APEX ToolingAPI
One option is to use the Rest version of the ToolingAPI to create the StaticResource:
string name = 'testjs';
string contentType = 'text/javascript';
string body = 'alert("Hello World");';
// The static resource is expected to be base64 encoded.
string base64EncodedBody = EncodingUtil.base64Encode(Blob.valueof(body));
System.debug(base64EncodedBody);
HttpRequest req = new HttpRequest();
req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v29.0/tooling/sobjects/StaticResource');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
// JSON formatted body
req.setBody(
'{"Name":"'+name+'"'+
',"ContentType":"'+contentType+'"'+
',"Body":"'+base64EncodedBody+'"'+
',"CacheControl":"Public"}'
);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res);
System.debug(res.getBody());
System.debug(res.getHeaderKeys());
To update it you will need the StaticResource Id (starting with 081 key prefix) appended onto the end of the endpoint URL. Change the HTTP method to 'PATCH'.
Using this method with Workbench I as able to change the contents of the StaticResource by altering the base64 encoded body.
A note on changing the HTTP request method to PATCH. If you just change it directly against as req.setMethod('POST');
you will end up with the error:
System.CalloutException: Invalid HTTP method: PATCH
Instead keep the POST in the HttpRequest and use the `_HttpMethod=PATCH' query string parameter documented from Update a Record
string name = 'testjs';
string contentType = 'text/javascript';
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm() +
'/services/data/v29.0/tooling/sobjects/StaticResource/08170000000GuZVAA0'+
'?_HttpMethod=PATCH');
req.setMethod('POST');
//System.CalloutException: Invalid HTTP method: PATCH
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
// JSON formatted body
req.setBody('{"Name":"'+name+'", "ContentType":"'+contentType+'", "Body":"YWxlcnQoIkhlbGxvIFdvcmxkIik0"}');
HttpResponse res = http.send(req);
System.debug(res);