0

I have created Apex Class that use tooling api and have one method that generate One class dynamically

public with sharing class SalesforceTool
{
    final String endpoint = 'https://na1.salesforce.com/services/data/v39.0/tooling/sobjects';

    // SomeValues to be store
    public String oid ;
    public String cid ;
    public String apmid ;
    public String carid ;

    public SalesforceTool()
    {
    }

    // used receive response by sending a http request
    private String getResponse(HttpRequest req)
    {
        try
        {
            Http
            httpreq = new Http();
            HttpResponse
            res = httpreq.send(req);
            String reqresponse = res.getBody();
            return
            reqresponse;
        }
        catch (Exception e){return
        'Error:' +e.getMessage();}
    }

    // create a http request with required endpoint and request method
    private HttpRequest createHttpRequest(String endpoint, String method)
    {
        HttpRequest req = new HttpRequest();
        endpoint += '';
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint(endpoint);
        req.setMethod(method);
        return req;
    }

    public void createApexClass()
{
    // create a Http request with post method
    HttpRequest req = createHttpRequest(endpoint+'/ApexClass','POST');
    req.setBody( '{"Name":"NewApexClass1","Body":"public class NewApexClass1 {}"}');

    String response = getResponse(req);

}
}

When I called this method from developer console .

SalesforceTool sf = new SalesforceTool();
sf.createApexClass();

New class is not created

when I try to run end point url

https://ap1.salesforce.com/services/data/v39.0/tooling

it says error

Errors>
<Error>
<errorCode>INVALID_SESSION_ID</errorCode>
<message>Session expired or invalid</message>
</Error>
</Errors>

I also try to run command

curl -H "Authorization: Bearer 00D90000000lZg3!ASAAQORxuDb4zTM4yHCL6C5KrgI8Za.r4Hpzkya.Ztl7odMxm_wJqnPaW82_Jp28hDyPrQxxGWx.EdHGDjX5DYaJP7pW6td_" "https://ap1.salesforce.com/services/data/v28.0/tooling/sobjects"

it is giving output

{"runTestsAsynchronous":"/services/data/v28.0/tooling/runTestsAsynchronous","search":"/services/data/v28.0/tooling/search","composite":"/services/data/v28.0/tooling/composite","query":"/services/data/v28.0/tooling/query","completions":"/services/data/v28.0/tooling/completions","sobjects":"/services/data/v28.0/tooling/sobjects","executeAnonymous":"/services/data/v28.0/tooling/executeAnonymous","runTestsSynchronous":"/services/data/v28.0/tooling/runTestsSynchronous"}

But New class is not creating . it this problem in end point url or I am skipping steps to be done ?

  • 1
    Can you print the response variable in system.debug to see what are you getting? – Ashwani Apr 6 '17 at 17:27
1

I am the author of this code here. I am not sure from where you got this.

Please make sure you have added 'https://ap1.salesforce.com' in Remote Site settings.

Try following code snippet. I corrected indentation and added a system.debug only and it worked.

public with sharing class SalesforceTool
{
    final String endpoint = 'https://huddle-idp-dev-ed.my.salesforce.com/services/data/v28.0/tooling/sobjects';

    // SomeValues to be store
    public String oid ;
    public String cid ;
    public String apmid ;
    public String carid ;

    public SalesforceTool()
    {
    }

    // used receive response by sending a http request
    private String getResponse(HttpRequest req)
    {
        try
        {
            Http httpreq = new Http();
            HttpResponse res = httpreq.send(req);
            String reqresponse = res.getBody();
            return reqresponse;
        }
        catch (Exception e){
            System.debug(' ==> Error: '+e.getMessage());
            return 'Error:' +e.getMessage();
        }
    }

    // create a http request with required endpoint and request method
    private HttpRequest createHttpRequest(String endpoint, String method)
    {
        HttpRequest req = new
        HttpRequest();
        endpoint += '';
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint(endpoint);
        req.setMethod(method);
        return req;
    }

    public void createApexClass()
    {
        // create a Http request with post method
        HttpRequest req = createHttpRequest(endpoint+'/ApexClass','POST');
        req.setBody( '{"Name":"NewApexClass1","Body":"public class NewApexClass1 {}"}');
        // send request and receive response
        String response = getResponse(req);
        System.debug(' ==> '+response);
        // These two lines have no role in creating apex class. So they can be ignored for now.
        //Map<String,Object> rmp = (Map<String,Object>)JSON.deserializeuntyped(response);
        //oid = (String)rmp.get('id');
    }
}


Execute Anonymous:

SalesforceTool sf = new SalesforceTool();
sf.createApexClass();

Response: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.