You need to sign in to do that
Don't have an account?
Jun Gao5
Issue with creating apex class using tooling api
I am using Tooling API to create an Apex class, I noticed that if the class body has a reference to any object (standard or custom), it throws a compilation error.
For example, if the class body has:
Account a;
the error is: no access to account.
How do I get around this?
For example, if the class body has:
Account a;
the error is: no access to account.
How do I get around this?
Does the profile for the user you're have permission to access any objects at all?
webservice static String createClass(String sClassName, String sClassBody)
{
JSONGenerator body = JSON.createGenerator(false);
body.writeStartObject();
body.writeStringField('Name', sClassName);
body.writeStringField('Body', sClassBody);
body.writeEndObject();
HttpRequest createReq = new HttpRequest();
createReq.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v30.0/tooling/sobjects/ApexClass');
createReq.setBody( body.getAsString() );
createReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
createReq.setHeader('Content-Type', 'application/json');
createReq.setMethod('POST');
createReq.setTimeout(30000);
Http h = new Http();
HttpResponse createRes = h.send(createReq);
Logger.dbg(createRes.getStatusCode());
}
then call this from a button (OnClick JavaScript), the user who clicks the button is System Administrator
Account a;
I get the compilation error. If I manually create the class with the exact same code, it works
I was using 30.0, I just tried 31.0, same behavior...
I had a lot of trouble knowing when to use \\n vs\n,etc.