My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Updated Aug 20, 2010 by mark.leo...@gmail.com

Force.com Web Service Connector (WSC)

The Force.com Web Service Connector (WSC) is a high performing web service client stack implemented using a streaming parser. WSC also makes it much easier to use the Force.com API (Web Services/SOAP or Asynchronous/REST API). WSC can be used to invoke any doc literal wrapped web service.

Downloading WSC

You can download a pre-built WSC jar file from:

http://code.google.com/p/sfdc-wsc/downloads/list

The pre-built jar files were generated using JDK 1.6 and only run on JDK 1.6. To build a jar file that can run on JDK 1.5, see "Building WSC from Source Code."

Building WSC from Source Code

Prerequisite: You need JDK 1.6 to compile WSC jar files.

To compile jar files:

1. Download the source code from:

http://code.google.com/p/sfdc-wsc/source/checkout

2. cd to dist directory.

3. run "ant all" to generate the wsc.jar file.

Note: This ant target compiles the jar file to run on JDK 1.6. To compile the jar file so that it can run on JDK 1.5, run "ant –DcompileTarget=1.5 all" instead of "ant all". You still need to compile with JDK 1.6.

Generating Stub Code from a WSDL

Run wsdlc on the WSDL you have downloaded:

java -classpath wsc.jar com.sforce.ws.tools.wsdlc *wsdl* *jar.file*
  • wsdl is the name of the WSDL file
  • jar.file is the name of the output jar file that wsdlc generates
  • You can include an optional argument: -Dpackage-prefix=myprefix

Write application code

Write your client application. For example, the following code creates a new Account using partner API:

   import com.sforce.soap.partner.*;
   import com.sforce.soap.partner.sobject.*;
   import com.sforce.ws.*;

   public static void main(String args) {
              ConnectorConfig config = new ConnectorConfig();
              config.setUsername("username");
              config.setPassword("password");

              PartnerConnection connection = Connector.newConnection(config);
              SObject account = new SObject();
              account.setType("Account");
              account.setField("Name", "My Account");
              connection.create(new SObject[]{account});
   }

Add wsc.jar and jar.file (generated by wsdlc) to your classpath, then compile and run your client application.

Comment by tynia.y...@gmail.com, May 8, 2009

Hi. I downloaded the pre-built WSC jar file and tried to run the sample code on Google App Engine. I get the following error: java.lang.UnsupportedOperationException?: Method not implemented

It happens at this call: EnterpriseConnection? connection = Connector.newConnection(userName, password);

Is this supposed to happen?

I'm using wsc-16_0.jar on Windows XP.

Comment by project member manoj.ch...@gmail.com, May 28, 2009

On Google app engine, you should use wsc-gae-16_0.jar.

Comment by iqbalyus...@gmail.com, Jun 1, 2009

Hi will wsc-16_0.jar file work on any WSDL SOAP services?

Comment by tomaspin...@gmail.com, Sep 17, 2009

Hi. I downloaded wsc-gae-16_0.jar file and try to run wsdlc command on my enterprise wsdl. At command line : java com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar but the result is :

Exception in thread "main" java.lang.NullPointerException?

at com.sforce.ws.tools.wsdlc.checkTargetFile(wsdlc at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:66) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:288) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)
what's wrong? thank

Comment by orla...@gmail.com, Oct 2, 2009

I am getting the same error as above. Any clues?

Thanks

Comment by dariusz....@gmail.com, Oct 21, 2009

If you get NullPointerException? then add tempdir path on the end of command. java -classpath wsc.jar com.sforce.ws.tools.wsdlc wsdl jar.file tempdir

I'm getting other error:

WSC?[wsdlc.run:290]Created temp dir: c:\temp\wsdl\gen WSC?[wsdlc.<init>:78]Generating Java files from schema ... Exception in thread "main" java.io.FileNotFoundException?: c:\temp\wsdl\gen\servi ces-repository-jbee-skg-com:RepositoryService?_v1r0\mypackage\GetDocumentResult?.j ava (Invalid directory name - contains ':' )

at java.io.FileOutputStream?.open(Native Method) at java.io.FileOutputStream?.<init>(Unknown Source) at java.io.FileOutputStream?.<init>(Unknown Source) at com.sforce.ws.template.Template.exec(Template.java:100) at com.sforce.ws.tools.TypeGenerator?.generate(TypeGenerator?.java:76) at com.sforce.ws.tools.wsdlc.generate(wsdlc.java:259) at com.sforce.ws.tools.wsdlc.generateTypes(wsdlc.java:246) at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:78) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:290) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)

This happen because I have targetnamespace set on 'urn:services-repository-jbee-skg-com:RepositoryService?_v1r0'. What can I do with this ?

Comment by pkulkarn...@gmail.com, Dec 9, 2009

HI ,

I have a webservice created in .NET and I want to convert the wsdl of this webservice into jar file.I have tried with the tools provided with appengine but its giving errors.How to convert the WSDL into jar?does appengine have generic converter?or is it specific only to salesforce.com wsdl?

Comment by kannon...@gmail.com, Jan 13, 2010

If you're getting the following stacktrace:

Exception in thread "main" java.lang.NullPointerException
        at com.sforce.ws.tools.wsdlc.checkTargetFile(wsdlc.java:115)
        at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:66)
        at com.sforce.ws.tools.wsdlc.run(wsdlc.java:288)
        at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)

You may need to supply the full of path to the JAR file you wish to create, e.g.:

java -classpath wsc-gae-16_0.jar com.sforce.ws.tools.wsdlc webservice.wsdl d:/path/to/jarfile.jar
Comment by contactk...@gmail.com, Jan 17, 2010

Hello Chaps

I am getting a different error now:

Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException?: Unknown element: import

at com.sforce.ws.wsdl.Definitions.parse(Definitions.java:191) at com.sforce.ws.wsdl.Definitions.read(Definitions.java:104) at com.sforce.ws.wsdl.WsdlFactory?.create(WsdlFactory?.java:68) at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:74) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:288) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)

Comment by redtige...@gmail.com, Jan 17, 2010

Hi,

I am trying to use this connector together with a salesforce.com Sandbox account. But I keep getting wrong user errors. Can you tell me where I have to change the server to the sandbox server (test.salesforce.com)?

Thanks in advance!

Comment by sfdcacco...@gmail.com, Feb 1, 2010

If you are having problems with your password, can you try appending your security token at the end of your password in the program?

Comment by jeff%jef...@gtempaccount.com, Feb 6, 2010

Looking for the wsc-18.0.jar as specified in Force.com Bulk API Guide (http://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_code_set_up_client.htm). Any idea where I can find it? Thanks!

Comment by MarekCy...@gmail.com, Feb 12, 2010

Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException?: Unsupported Schema element found http://www.w3.org/2001/XMLSchema:attributeGroup. At: 255:42

at com.sforce.ws.wsdl.Schema.read(Schema.java:139) at com.sforce.ws.wsdl.Types.read(Types.java:64) at com.sforce.ws.wsdl.Definitions.parse(Definitions.java:162) at com.sforce.ws.wsdl.Definitions.read(Definitions.java:104) at com.sforce.ws.wsdl.WsdlFactory?.create(WsdlFactory?.java:68) at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:74) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:288) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)

Comment by dzaf...@gmail.com, Mar 5, 2010

Works fine for WSDL PARTNER but gives errors for WSDL METADATA, any solution ?

Comment by saadnouri, Mar 19, 2010

Hello,

I am getting the same error as "contactkannan"

" Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException?: Unknown elemen t: import

at com.sforce.ws.wsdl.Definitions.parse(Definitions.java:191) at com.sforce.ws.wsdl.Definitions.read(Definitions.java:104) at com.sforce.ws.wsdl.WsdlFactory?.create(WsdlFactory?.java:68) at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:74) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:290) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:279)
"

Anyone knows why this is happening?

Note that the WSDL that i am trying to work with is not a sf one!

Cheers!

Comment by utsavp...@gmail.com, May 12, 2010

Hello everyone I am getting this error can any one help me

C:\utsav\googleProject>java -classpath wsc-18.jar com.sforce.ws.tools.wsdlc MyWS DL.wsdl c:\utsav\googleproject\Myjar.jar WSC?[wsdlc.run:311]Created temp dir: C:\Users\Utsav\AppData?\Local\Temp\wsdlc-te mp-2282562548841630409-dir Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException?: Parse error: F ound invalid XML. only whitespace content allowed before start tag and not - (po sition: START_DOCUMENT seen ...yright 1999-2010 salesforce.com, inc.\r\nAll Righ ts Reserved\r\n-->\r\n-... @9:2)

at com.sforce.ws.wsdl.WsdlParser?.next(WsdlParser?.java:93) at com.sforce.ws.wsdl.Definitions.read(Definitions.java:110) at com.sforce.ws.wsdl.WsdlFactory?.create(WsdlFactory?.java:68) at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:75) at com.sforce.ws.tools.wsdlc.run(wsdlc.java:311) at com.sforce.ws.tools.wsdlc.main(wsdlc.java:302)
Caused by: com.sforce.ws.ConnectionException?: Found invalid XML. only whitespace
content allowed before start tag and not - (position: START_DOCUMENT seen ...yr
ight 1999-2010 salesforce.com, inc.\r\nAll Rights Reserved\r\n-->\r\n-... @9:2)

at com.sforce.ws.parser.XmlInputStream?.next(XmlInputStream?.java:137) at com.sforce.ws.wsdl.WsdlParser?.next(WsdlParser?.java:89) ... 5 more

Caused by: com.sforce.ws.parser.XmlPullParserException?: only whitespace content allowed before start tag and not - (position: START_DOCUMENT seen ...yright 1999 -2010 salesforce.com, inc.\r\nAll Rights Reserved\r\n-->\r\n-... @9:2)

at com.sforce.ws.parser.MXParser.parseProlog(MXParser.java:1468) at com.sforce.ws.parser.MXParser.nextImpl(MXParser.java:1345) at com.sforce.ws.parser.MXParser.next(MXParser.java:1049) at com.sforce.ws.parser.XmlInputStream?.next(XmlInputStream?.java:135) ... 6 more

Comment by ganesh...@gmail.com, Jun 24, 2010

Could you change the sample code to have String as argument type for main method rather than a plain String? No big deal, I just downloaded WSC and tried to get it up and running with my Eclipse IDE. But then I ran into this weird issue with IDE complaining about missing main method. It took me a few minutes to figure out the problem with argument type. Silly of me, but I thought I would point it out anyways so others don't run into the same issue.

Comment by bodr.env...@gmail.com, Aug 3, 2010

I am getting significant compilation errors when I try to generate the stub jar file. Is it having an issue because there is a package name of "10"? Is there a workaround?

java -classpath wsc-19.jar com.sforce.ws.tools.wsdlc OCRWebService.WSDL.xml OCRWebService.jar WSC?[wsdlc.run:312]Created temp dir: /var/folders/+Q/+QusXyZNHjqOyTZGQYMa-k+++TI/-Tmp-/wsdlc-temp-31264270122093175-dir WSC?[wsdlc.<init>:79]Generating Java files from schema ... WSC?[wsdlc.<init>:79]Generated 16 java files. WSC?[wsdlc.compileTypes:262]Compiling to target 1.6... /var/folders/+Q/+QusXyZNHjqOyTZGQYMa-k+++TI/-Tmp-/wsdlc-temp-31264270122093175-dir/com/sforce/soap/10/OCRWebServiceRecognize_element.java:1: ';' expected package com.sforce.soap.10;

^
/var/folders/+Q/+QusXyZNHjqOyTZGQYMa-k+++TI/-Tmp-/wsdlc-temp-31264270122093175-dir/com/sforce/soap/10/OCRWebServiceRecognize_element.java:68: <identifier> expected
private com.sforce.soap.10.OCRWSInputImage OCRWSInputImage;

/var/folders/+Q/+QusXyZNHjqOyTZGQYMa-k+++TI/-Tmp-/wsdlc-temp-7747198376826189011-dir/com/sforce/soap/10/jj/OCRWSSettings.java:310: ';' expected
setOcrZones((com.sforce.soap.10.jj.ArrayOfOCRWSZone)typeMapper.readObject(in, ocrZonestypeInfo, com.sforce.soap.10.jj.ArrayOfOCRWSZone.class));
^
100 errors Error: Failed to compile

thanks!

Comment by tuanndt....@gmail.com, Sep 10, 2010

The source code of com.sforce.soap.partner.wsc.Connector is as follows.

package com.sforce.soap.partner.wsc;

import com.sforce.ws.ConnectorConfig;
import com.sforce.ws.ConnectionException;

/** * Generated class, please do not edit. */
public class Connector
{
	public static final String END_POINT = "https://www.salesforce.com/services/Soap/u/18.0";
	
	public static PartnerConnection newConnection(String username, String password) throws ConnectionException {
		ConnectorConfig config = new ConnectorConfig();
		config.setUsername(username);
		config.setPassword(password);
		return newConnection(config);
	}

	public static PartnerConnection newConnection(ConnectorConfig config) throws ConnectionException {
		if (config.getAuthEndpoint() == null) {
			config.setAuthEndpoint(END_POINT);
		}
		if (config.getServiceEndpoint() == null) {
			config.setServiceEndpoint(END_POINT);
		}
		return new PartnerConnection(config);
	}
}

Obviously, the endpoint URL is hardcoded. So I guess we need to explicitly set the endpoint to Sandbox if we want to connect it

ConnectorConfig config = new ConnectorConfig();
config.setUsername("username");
config.setPassword("password");
// explicitly set the endpoint
config.setAuthEndpoint("https://test.salesforce.com/services/Soap/u/18.0");
Comment by raj.chil...@gmail.com, Dec 7, 2010

Yes, setting the endpoint this way makes the code re-usable.

Comment by nav...@gmail.com, Jan 11, 2011

I am invoking an SAP ABAP based web service from GAE using WSDL data connection. I am unable to authenticate. I got "Logon Error Message". It works(authenticate) fine running as Java application or running on GAE calling web services with no authentication. Please let me know how to do this. Thanks a lot

Comment by stuart.m...@gmail.com, Jan 31, 2011

Can someone Please let me know how to set DateTime? for WSC? I tried with many formats but I did not get the correct date and time in to salesforce. It shows one day less and wrong time.

Here is my code

package com.date.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.*;
import java.util.TimeZone;

//import com.sforce.async.SObject;
import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.SaveResult;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectorConfig;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class TimeZoneConvertorTest {

	public static Date getDateInTimeZone(Date currentDate, String timeZoneId) {
		TimeZone tz = TimeZone.getTimeZone(timeZoneId);
		Calendar mbCal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
		mbCal.setTimeInMillis(currentDate.getTime());

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
		cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
		cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
		cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
		cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));

		return cal.getTime();
	}

	private void convertTimeZone() throws Exception {
		
		Date d1 = null;
		Date d2 = null;
		
		String date = "11/28/2006 09:45:12"; 
		String formattedDate = null;
		
		System.out.println("Actual Date String From Coldfusion : " + date);

		// Date format in MM/dd/yyyy hh:mm:ss
		DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		// df1.setTimeZone(TimeZone.getTimeZone("EST"));

		// Date format in yyyy-MM-dd'T'hh:mm:ss'Z'
		DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
		try {
			// Parses the value and assumes it represents a date and time in the EST timezone
			d1 = df1.parse(date);
			System.out.println("Original date After Converting to Date object " + d1);

			System.out.println("Date Object Converted to UTC " + getDateInTimeZone(d1, "UTC"));

			d1 = getDateInTimeZone(d1, "UTC");
			
			// Format the date to yyyy-MM-dd'T'hh:mm:ss'Z'
			formattedDate = df2.format(d1);

			System.out.println("After changing the format to UTC " + formattedDate);
			df2.setTimeZone(TimeZone.getTimeZone("UTC"));

			// Parse it to date type
			d2 = df2.parse(formattedDate);

			System.out.println("Date after parsing : " + d2);
			
		} catch (ParseException pe) {
			pe.printStackTrace();
		}

		ConnectorConfig config = new ConnectorConfig();

		config
				.setServiceEndpoint("https://login.salesforce.com/services/Soap/u/17.0");
		config
				.setAuthEndpoint("https://login.salesforce.com/services/Soap/u/17.0");

		config.setUsername("my username");
		config.setPassword("xyz");

		PartnerConnection connection = null;
		try {
			connection = Connector.newConnection(config);

			SObject account = new SObject();
			account.setType("Account");
			account.setField("Name", "Account 1");
			account.setField("Customer_Signed_Date__c", getDateInTimeZone(d1,
					"UTC"));


			SaveResult[] results = connection
					.create(new SObject[] { account });
			System.out.println(">>>>>>Created Account : "
					+ results[0].getId());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

Comment by wengong@google.com, Jun 29, 2011

What is the user name and password to use when using wsc_22.jar? I'm getting the following message:

failed [LoginFault [ApiFault exceptionCode='INVALID_LOGIN'

exceptionMessage='Invalid username, password, security token; or user locked out.'
] ]

Thanks,

Wen

Comment by mouais...@gmail.com, Jul 27, 2011

Hi,

the exemple has to be updated for the last version right ?

I dont find PartnerConnection? / SObject classes

has someone a working example ?

Comment by sdetw...@gmail.com, Sep 2, 2011

I am completely lost.

I have a working sf application, NOT using the WSC lib.. and using the enterprise wsdl, from java (built with wsdl2java), developed and debugged with eclipse.. been fine for a year.

now I want to move to the partner wsdl, and the wsc libs look to provide so easier function.. cool.. should be an easy conversion

download the wsc-22.jar today, download the partner wsdl from my org, use the wsc jar to generate the partner.jar from the partner wsdl.. no problem add the partner jar and the wsc jar to my eclipse project.. no problem

but 'SObject.setField(name, object)' is NOT in either jar file. I look at the generated partner jar SObject class, says it 'extends' XMLObject form wsc.jar.. ok but I don't see the inherited methods.

the examples say

import com.sforce.partner.sobject.wsc.SObject but I don't find that package and class. 'wsc.SObject'

all the examples on SF http://www.salesforce.com/us/developer/docs/api/index_Left.htm#CSHID=sforce_api_partner.htm|StartTopic=Content%2Fsforce_api_partner.htm|SkinName=webhelp use setField()..

SObject updateContact = new SObject(); updateContact.setType("Contact"); // need to have the id so that API knows which account to update updateContact.setId(id); updateContact.setField("Phone", "(510) 555-9741"); <------ // create one that will throw an error

so.. what did I miss?

Comment by sdetw...@gmail.com, Sep 3, 2011

found it.. cause I am building a plugin for our server, there was one more classpath to fix

Comment by niklas.waller, Dec 14, 2011

After importing the output jar to my Java project I get error messages for every class and almost every method saying things like "com.sforce.ws cannot be resolved to a type" when I try to use the imported folders and files as source folders.

Also all code seems to be put on one row in each class.

Any thoughts on how and where to import and use the library?

Thanks / Nick

Comment by niklas.waller, Dec 15, 2011

Solved this myself. Only lack of Eclipse knowledge. Look here if you also want to know -> http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

Comment by b.sum...@gmail.com, Mar 6, 2012

can someone please let me How to get the date field from salesforce and display the correct date formate in PDF?

Comment by bajid...@gmail.com, Mar 13, 2012

Hi can anybody tell me how to test webservice in salesforce.com


Sign in to add a comment
Powered by Google Project Hosting