Force.com: Resolving the Destination URL not reset exception

Salesforce provides a number of methods for integrating Java and/or .NET applications using SOAP APIs. Depending on the type of application you will be writing will determine which WSDL(s) you will need to generate and consume. These WSDLs are located in Your Name | Setup | Develop | API.

Earlier today I was writing a Java client to connect via the Web Services API through the Enterprise WSDL. Using the Force.com Web Services Connector (WSC), I generated the stub code from the wsdl and included the jar files in my project. These details can be found by clicking the link above.

Connecting to Salesforce seemed pretty straightforward. In my case, I needed to use the Connector.newConnection() method. So I did this:

package com.rickross;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class BrokenDemo 
{
	public static void main(String[] args) 
		throws ConnectionException 
	{
		System.out.println("Starting Broken Demo");
		ConnectorConfig config = new ConnectorConfig();
		config.setManualLogin(true);	
		EnterpriseConnection conn = 
			Connector.newConnection(config);
		LoginResult result = 
			conn.login("username", "pwd+token");
		if (!result.getPasswordExpired())
		{
			Account account = new Account();
			account.setName("Test from Java");
			// Won't work.. this will throw 
			/*
			 * [UnexpectedErrorFault 
			 * [ApiFault  exceptionCode='UNKNOWN_EXCEPTION'
 				exceptionMessage='Destination URL not reset. 
 				The URL returned from login must be set in 
 				the SforceService'
			   ]
			 */
			conn.create(new SObject[]{ account });

			conn.logout();
		}
	}
}
But this did not work. Searching for this issue gave solutions that were not applicable when using the Force.com Web Services Connector. I talked to a colleague who had used .NET to communicate with the force.com APIs and he showed me code that required him to override a URL property, which did not exist in Java.

After some time experimenting, I discovered a solution which required the creation of another EnterpriseConnection but instead of using the user name and password, to use the session ID and server URL that is found from the results of the previous login. The following code demonstrates a successful test:

public class WorkingDemo 
{
	public static void main(String[] args) 
		throws ConnectionException 
	{
		System.out.println("Starting Working Demo");
		ConnectorConfig config = new ConnectorConfig();
		config.setManualLogin(true);	
		EnterpriseConnection conn = 
			Connector.newConnection(config);
		LoginResult loginResult = 
			conn.login("username", "pwd+token");
		if (!loginResult.getPasswordExpired())
		{
			System.out.println("Logged in");
			
			// once you log in, you need to create a new 
			// config and get the session and server 
			// details from the login results and 
			// create a new connection with this info
			ConnectorConfig newConfig = new ConnectorConfig();
			newConfig.setSessionId(
					loginResult.getSessionId());
			newConfig.setServiceEndpoint(
					loginResult.getServerUrl());
			conn = Connector.newConnection(newConfig);
	        
			Account account = new Account();
			account.setName("Test from Java");

			conn.create(new SObject[]{ account });
			
			System.out.println("The account has been created!");
			
			conn.logout();
		}
	}
}
I hope that this will save someone a few minutes or hours trying to figure out how to solve this exception when using the Enterprise WSDL. Please let me know if you know of any other solutions that will work.
This entry was posted in Java, PaaS, Salesforce.com, Software Development, Tips & Tricks, Web Services. Bookmark the permalink.

3 Responses to Force.com: Resolving the Destination URL not reset exception

  1. Ely Orozco says:

    HEllo. I have been trying to implement your code and the thing is that it seems that EnterpriseConnection and Connector are missing. I already donwloaded wsc-22.jar, partner-18.jar and enterprise.jar but nothing seems to deal with it. Could you please guide me with this issue? It is urgente. I would really appreciate it!

    Thank you in advance,

    Ely.

  2. Daniel Shaheen says:

    Thanks for the tip of setting the service point URL and the session Id.

Comments are closed.