I am trying to connect from my c# client side app and execute some apex code.

Here is what I've done so far (which maybe wrong):

  1. Went to Setup/Develop/API.
  2. Clicked Generate Apex WSDL
  3. Consumed the WSDL in my c# app.

Let's say I want to run the following:

private String IdToObject(Id someId) {
    Schema.SObjectType objectType = someId.getSObjectType();
    return String.valueOf(objectType);

How would I get the return value of IdToObject('001L000000kbReV')?

  • function is not an apex keyword ... is your goal to pass in anonymous apex and execute it from a soap client? or do you have existing apex methods defined as webservices that you want to execute? – cropredy Sep 29 '15 at 22:41
  • @crop1645 I am sorry...my question was unclear and the code flawed. I fixed that. My goal is to pass in anonymous apex and execute it from the soap client. – AngryHacker Sep 29 '15 at 22:47
  • Couldn't you use the Describe calls in the API that will return the keyprefix (first three chars) and that can be used by your soap client to decide the objecttype by comparing against the first three chars of some ID? – cropredy Sep 30 '15 at 0:25
  • @crop1645 I am not sure how that would work. Don't you need to know the object type to be able to call Describe calls? Besides...this is just an example...I want to be able to run any apex code, just like they do in the Anonymous window. But if you have an example of what you are suggesting, I'd like to have a look. – AngryHacker Sep 30 '15 at 5:23

It looks like you want something along the lines of eval() support for running Apex code from .NET and then getting the result back.

I've covered this from the perspective of Apex-to-Apex, but the principal is the same. Kevin also has an alternative approach with a similar goal.

The trick in both approaches is that executing anonymous apex doesn't have a direct mechanism for returning a result. You either need to deliberately throw an exception with the value you want back or return it in the debug log that gets generated. Then you do a bit of parsing to pull your result out.

To your specific example, you have an Id and you want to know metadata about the associated sObject?

There are much easier ways to do this than executing anonymous apex and parsing the result out and all the associated problems.

The Partner API provides a large amount of metadata about the sObjects that are accessible by the current user.

General steps:

  1. Extract the key prefix from the first 3 characters of the ID you have. See What are Salesforce ID's composed of?
  2. Call describeGlobal() to figure out which sObjects you can access.
  3. Scan through the returned DescribeGlobalSObjectResult collection looking at the keyPrefix field. When you find a match you will have some basic initial metadata, such as the sObject label and API name.
  4. Call describeSObjects() is you want some richer DescribeSObjectResult metadata.
|improve this answer|||||

Here is a link which describes the process in detail. You have already done most of the parts. The below code describes how to call a method. ApexSample.ApexSoapWebservice.Account[] accounts = apex.getMyAccounts(null);

// Define the variables that will be used.
SFDCEnterpriseAPI.SforceService sfdc = new SFDCEnterpriseAPI.SforceService();
ApexSoapWebservice.ApexSoapWebserviceService apex = new ApexSoapWebservice.ApexSoapWebserviceService();
SFDCEnterpriseAPI.LoginResult lr = null;

// Login
try {
    lr = sfdc.login(tbUN.Text, tbPWD.Text);
} catch (System.Web.Services.Protocols.SoapException ex) {

// If it reached here, the connection was succesful, but the password may have expired.
if (!lr.passwordExpired) {
    // Set connection details for Enterprise WS
    sfdc.Url = lr.serverUrl;
    sfdc.SessionHeaderValue = new ApexSample.SFDCEnterpriseAPI.SessionHeader();
    sfdc.SessionHeaderValue.sessionId = lr.sessionId;

    // Set connection details for Apex WS
    // DO NOT SET THE apex.url, or you will get this error:
    // SoapHeaderException: No operation available for request. {http://soap.sforce.com/schemas/class/...
    apex.SessionHeaderValue = new ApexSoapWebservice.SessionHeader();
    apex.SessionHeaderValue.sessionId = lr.sessionId;

    // Now make the call to your methods
    ApexSample.ApexSoapWebservice.Account[] accounts = apex.getMyAccounts(null);
} else {
    Console.WriteLine("Your password is expired.");
|improve this answer|||||
  • I get it, but I want to execute the function that I create on the fly, just like I would in the Execute Anonymous window. How would I do that? – AngryHacker Sep 29 '15 at 22:42
  • I cleaned up the question a bit, so that it's easier to see what I want. – AngryHacker Sep 29 '15 at 23:22
  • 1
    @AngryHacker My answer was based on what you have asked, then why negative vote? – Saroj Bera Sep 30 '15 at 1:24
  • I didn't give you the negative vote. However, you didn't answer my question. I want to execute apex code on the fly.. Your code and the link describe how to fire off SOQL statements via the API... I want to run apex code. – AngryHacker Sep 30 '15 at 5:20
  • @AngryHacker sorry for that, I found another link. I think this is what you are looking for. – Saroj Bera Sep 30 '15 at 5:27

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.