Tuesday, November 27, 2012

SalesForce SOAP API- Basic Setup

I will share about the basic usage of SalesForce API based on SOAP protocol. I will discuss only the important steps and the error messages which I got because I spent around a complete day in googling about the errors and then finally getting them correct.

After you create your salesforce developer account, you need to create a wsdl from it. You can choose either partner wsdl or enterprise wsdl or any other, as the case may be. For it, you need to do the following:
Login to salesforce --> YourName --> Setup --> Develop --> API.
Here you choose the type of WSDL you want to create.

NOTE: If you use chrome to make the wsdl file, you might end-up in creating wrong wsdl files and it will throw error while creating jar out of that wsdl. The error will be something like "Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException: Unsupported Schema element found http://www.w3.org/2001/XMLSchema:complextype. At: 93:41". So, use internet explorer for making the wsdl.

To make the wsdl, rt click the link "Generate Partner WSDL" and click on save the target as. In that dialogue box, you will need to change the format extension to wsdl. For example let's save as partner.wsdl.

Then you will need to make the jar out of the wsdl you created. Before that, you will need to download the WSC JAR. You can download the file from here. Download the WSC file appropriate to the java version you have.

Now you need to make the jar file out of the wsdl file which you downloaded. For it, you will need to run the following command:

java –classpath pathToJAR/wsc-22.jar com.sforce.ws.tools.wsdlc pathToWsdl/WsdlFilenamepathToOutputJar/OutputJarFilename


Here, you need to remember the version of java used and the WSC you downloaded. You might face the error something like:



C:\Users\samahesh>java -classpath C:\sfdc\WSC\wsc-22.jar com.sforce.ws.tools.wsd
lc C:\sfdc\WSC\wsdl.jsp C:\sfdc\WSC\wsdl.jar
[WSC][wsdlc.run:312]Created temp dir: C:\Users\samahesh\AppData\Local\Temp\wsdlc
-temp-3098315371680609491-dir
Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException: Parse error: F
ound invalid XML. entity reference name can not contain character =' (position:
START_TAG seen ...sh = \'\';\nvar url = \'https://login.salesforce.com/?ec=302&s
tartURL=... @13:58)
        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.(wsdlc.java:75)
        at com.sforce.ws.tools.wsdlc.run(wsdlc.java:312)
        at com.sforce.ws.tools.wsdlc.main(wsdlc.java:303)
Caused by: com.sforce.ws.ConnectionException: Found invalid XML. entity referenc
e name can not contain character =' (position: START_TAG seen ...sh = \'\';\nvar
 url = \'https://login.salesforce.com/?ec=302&startURL=... @13:58)
        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: entity reference name ca
n not contain character =' (position: START_TAG seen ...sh = \'\';\nvar url = \'
https://login.salesforce.com/?ec=302&startURL=... @13:58)
        at com.sforce.ws.parser.MXParser.parseEntityRef(MXParser.java:2204)
        at com.sforce.ws.parser.MXParser.nextImpl(MXParser.java:1266)
        at com.sforce.ws.parser.MXParser.next(MXParser.java:1085)
        at com.sforce.ws.parser.XmlInputStream.next(XmlInputStream.java:135)
        ... 6 more


You might also face some error as:


C:\Users\samahesh>java -classpath C:\sfdc\WSC\wsc-22.jar com.sforce.ws.tools.wsd
lc C:\sfdc\WSC\wsdl.jsp C:\sfdc\WSC\wsdl.jar
[WSC][wsdlc.run:312]Created temp dir: C:\Users\samahesh\AppData\Local\Temp\wsdlc
-temp-3098315371680609491-dir
Exception in thread "main" com.sforce.ws.wsdl.WsdlParseException: Parse error: F
ound invalid XML. entity reference name can not contain character =' (position:
START_TAG seen ...sh = \'\';\nvar url = \'https://login.salesforce.com/?ec=302&s
tartURL=... @13:58)
        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.(wsdlc.java:75)
        at com.sforce.ws.tools.wsdlc.run(wsdlc.java:312)
        at com.sforce.ws.tools.wsdlc.main(wsdlc.java:303)
Caused by: com.sforce.ws.ConnectionException: Found invalid XML. entity referenc
e name can not contain character =' (position: START_TAG seen ...sh = \'\';\nvar
 url = \'https://login.salesforce.com/?ec=302&startURL=... @13:58)
        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: entity reference name ca
n not contain character =' (position: START_TAG seen ...sh = \'\';\nvar url = \'
https://login.salesforce.com/?ec=302&startURL=... @13:58)
        at com.sforce.ws.parser.MXParser.parseEntityRef(MXParser.java:2204)
        at com.sforce.ws.parser.MXParser.nextImpl(MXParser.java:1266)
        at com.sforce.ws.parser.MXParser.next(MXParser.java:1085)
        at com.sforce.ws.parser.XmlInputStream.next(XmlInputStream.java:135)
        ... 6 more



The main reason for the errors is the mismatch of the java version or something like downloading from chrome. To prevent any such errors, try using the java directly from where you want to use. For e.g., in the above error, it is due to the reason that default jave in my classpath is pointing to JDK 1.7 but it requires the JDK 1.6. So we can modify our command to use JDK 1.6 as something like:


C:\Users\samahesh>"C:\Program Files\Java\jdk1.6.0_27\bin\java.exe" -classpath C:\sfdc\WSC\wsc-22.jar com.sforce.ws.tools.wsdlc C:\sfdc\WSC\partner.wsdl C:\sfdc\WSC\wsdl.jar

After the command runs successfully, you will get the message as "...Generated JAR file...". 

Now you are ready to use this jar file in you sample program.
Create a sample program in any IDE, say Eclipse. Add the jar file created in above step in the build path of the project. Also add the WSC file which you downloaded. Use API and run the programs.

Reference: http://www.salesforce.com/us/developer/docs/api/index.htm







2 comments:

  1. Thanks.. It helps me to solve my problem

    ReplyDelete
  2. Thanks for the advice of generating the WSDL file through IE. It solved my issues. I had eaelier downloaded the wsdl through chrome. However, this error doesn't happen all the time. I had earlier successfully created jars from wsdl that was generated using chrome

    ReplyDelete