2

I have a java application which needs to access the Apex Class methods. So, I create a global Apex Class with WebService methods and created a WSDL for this class, and included this in my Java Application, so the WSDL contains my newly created WebService methods. I have created a SOAP connection for this WSDL and accessed these Webservice mthods.

But, I am looking for a solution, Where I don't need to export a Specific webservice WSDL and, able to connect to Salesforce Classes in a generic way. I can specify a class name and method name in properties file and access it, without changing the WSDL every time. Please suggest, If there is any possible solution!!

2

You can always use REST api methods instead of SOAP based api methods.

This link: Creating_REST_APIs_using_Apex_REST can help explain how. Rest methods are, in my opinion, cleaner and require no wsdls

0

The proper solution to using WSDLs and web services is to use the WSDL. While you could forego the pleasantries of a WSDL, for a strongly-typed language such as Java, you are in for a world of unpleasant surprises.

At minimum, to implement a client-side (Java) solution, you'd have to build an entire generic SOAP client just to interface with salesforce.com, and there would be a ton of casting, runtime data type detection, dynamic parameter building, and so on.

On the flip side, you could also implement this using Apex Code's reflection methods, but that would introduce another entire layer of complexity regarding interfaces, virtual or abstract classes, not to mention trying to come up with a uniform interface for dynamic parameters.

The amount of time required to simply re-compile a WSDL and rebuild a project each time the interface changes is significantly less than the time it would take to rig a dynamic framework that would probably be more fragile than offering any significant benefit on time or complexity savings.

You could also use REST instead of SOAP, but the same general problem applies. You'd be forced to do a ton of custom parsing rather than using native objects with stronger bindings, effectively discarding the entire purpose of using a strongly typed language in exchange for a minor convenience feature.

0

Create a properties file like this:

sfdc.username=aasdfasdfasdfasd
sfdc.password=adasdfdsfd##adfasdfasdfa
sfdc.authEndpoint=https://test.salesforce.com/services/Soap/u/28.0/
output-folder=C:/svn/schema/data/output/
script-folder=C:/svn/schema/src/scripts/

Then execute:

package com.rrr.vgnd;

import com.sforce.soap.apex.DebuggingInfo_element;
import com.sforce.soap.apex.ExecuteAnonymousResult;
import com.sforce.soap.apex.LogCategoryLevel;
import com.sforce.soap.apex.LogCategory;
import com.sforce.soap.apex.LogInfo;
import com.sforce.soap.apex.LogType;
import com.sforce.soap.apex.SoapConnection;
import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.DeleteResult;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.SaveResult;
import com.sforce.soap.partner.LoginResult;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.lang.StringBuffer;
import java.text.MessageFormat;
import java.util.Properties;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class ApexExecuteAnonymous {

    public Properties props = null;

    public static void main(String[] args) {
       ApexExecuteAnonymous rf = new ApexExecuteAnonymous();
       rf.init();
       rf.writeScript(rf.executeScript(rf.getScript()));
    }

     public ConnectorConfig getConnectorConfig() {

     String username = getProperty("sfdc.username");
     String password = getProperty("sfdc.password");
     String authEndpoint = getProperty("sfdc.authEndpoint");

     ConnectorConfig config = new ConnectorConfig();

     config.setUsername(username);
     config.setPassword(password);
     config.setAuthEndpoint(authEndpoint);

     config.setProxy("yourproxyserver.com",8080);
     config.setNtlmDomain("yourserviceaccountdomain");
     config.setProxyUsername("yourserviceaccountusername");
     config.setProxyPassword("yourserviceaccountpassword");

     try {
        PartnerConnection connection = new PartnerConnection(config);
        config.setManualLogin(true);
        LoginResult result = connection.login(username,password);
        String sessionId = result.getSessionId();
        String serverUrl = result.getServerUrl();
        serverUrl = serverUrl.replace("/Soap/u/", "/Soap/s/");
        config.setAuthEndpoint(serverUrl);
    config.setServiceEndpoint(serverUrl);
    config.setSessionId(sessionId);
        System.out.println("Url:"+serverUrl + "SessionId:"+sessionId);
     } catch( ConnectionException ex) {
          ex.printStackTrace();
     }
     return config;
   }

   public String executeScript(String script) {
     String elm = "";
// System.out.println("script:"+script);
     try {
        SoapConnection conn = new SoapConnection(getConnectorConfig());
        LogInfo infoAll = new LogInfo();
        infoAll.setCategory(LogCategory.All);
        infoAll.setLevel(LogCategoryLevel.Debug);
        LogInfo infoApex = new LogInfo();
        infoApex.setCategory(LogCategory.Apex_code);
        infoApex.setLevel(LogCategoryLevel.Debug);
        LogInfo infoProfiling = new LogInfo();
        infoProfiling.setCategory(LogCategory.Apex_profiling);
        infoProfiling.setLevel(LogCategoryLevel.Debug);
        LogInfo infoDB = new LogInfo();
        infoDB.setCategory(LogCategory.Db);
        infoDB.setLevel(LogCategoryLevel.Debug);
        conn.setDebuggingHeader(new LogInfo[] { infoAll, infoApex, infoProfiling, infoDB }, LogType.Detail);
        ExecuteAnonymousResult result = conn.executeAnonymous(script);
        elm = conn.getDebuggingInfo().getDebugLog(); 
        elm = elm.replace("|","|");
 System.out.println("elm:" + elm);
     } catch( ConnectionException ex) {
          System.err.println(ex.getMessage());
          ex.printStackTrace();
     }
     return elm; 
  }

  public void writeScript(String elm) {
     if(elm.indexOf("FATAL_ERROR") > 0) {
        System.err.println("---------  FATAL_ERROR  ---------");
     }
     String outputFolder = getProperty("output-folder");
     String outputFile = getProperty("output-file");
     outputFile = outputFolder + outputFile;
     PrintWriter out = null;
     try {
       out = new PrintWriter(outputFile);
       out.println(elm);
     } catch (IOException ex) {
       System.err.println(ex.getMessage());
       System.exit(1);
     } finally {
       if(null != out) { out.close(); }
     }
  }

  public String getScript() {
    BufferedReader br = null;
    String everything = "";
    try {
        String scriptFolder = getProperty("script-folder");
        String scriptFile = getProperty("script-file");
        scriptFile= scriptFolder + scriptFile;
        br = new BufferedReader(new FileReader(scriptFile));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line+ "\n");
            line = br.readLine();
        }
        everything = sb.toString();
    } catch(Exception ex) {
        System.err.println(ex.getMessage());
        System.exit(1);
    } finally {
        try { if(br!=null)br.close(); } catch(Exception ex) { System.err.println(ex.getMessage()); }
    }
    return everything;
   }

  public String getProperty(String name) {
    String value = props.getProperty(name);
    System.out.println(name + "=" + value);
    return value;
  }

  public Properties init() {
   try {
      props = System.getProperties();
      String propertiesFile=getProperty("properties-file");
      props.load(new FileInputStream(propertiesFile));
   } catch (Exception ex) {
      System.err.println(ex.getMessage());
      System.exit(1);
   }
   return props;
  }

}

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.