July 27, 2010

Salesforce WSC Metadata WSDL Connector Config Issue – Solved !

I am working and doing RnD with Salesforce WSC library, to get Tolerado ported to it.  Most of the pieces worked well, but was stuck with Metadata WSDL issues from very starting. I got into following two major blockers

  1. Metadata WSDL Compilation Issue
  2. Metadata Connector Config Issue

Both these are explained in below

Metadata WSDL Compilation Issue

Thanks to Jeff for his post about fixing the metadata compilation issue and other assistance with WSC Code Samples. I get rid of this one by using the Jeff’s fix.

Metadata Connector Config Issue

When I tried running a sample metadata call by using Session Ids from Partner WSDL Login and created MetadataConnection it failed with error “INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session", There is an issue posted on WSC Google Code Project too for the same.

Failing code shown below:

ConnectorConfig partnerConfig = new ConnectorConfig();
partnerConfig.setUsername("un");
partnerConfig.setPassword("pw");

PartnerConnection partnerConn = com.sforce.soap.partner.Connector.newConnection(partnerConfig);

ConnectorConfig metaConfig = new ConnectorConfig();
metaConfig.setSessionId(partnerConn.getSessionHeader().getSessionId());

MetadataConnection metaConn = Connector.newConnection(metaConfig);
DescribeMetadataResult describeMetadata = metaConn.describeMetadata(API_VERSION);
System.out.println(describeMetadata);

Solution

After some research and digging into WSC code, found that we need to set correct Metadata server URL to the ConnectorConfig. Obtaining Metadata URL was straightforward with Apache Axis as we need to do the Partner login manually. But with WSC we need to tell ConnectorConfig to STOP “Auto Login” and then do a manual login to get reference of LoginResult. So finally, following code is solution to this issue.

ConnectorConfig partnerConfig = new ConnectorConfig();
// IMPORTANT : This will not let PartnerConnection do the login
partnerConfig.setManualLogin(true);

PartnerConnection partnerConnection = com.sforce.soap.partner.Connector.newConnection(partnerConfig);
// We need LoginResult to get correct metadata server url
LoginResult lr = partnerConnection.login("USERNAME", "PASSWORD");

ConnectorConfig metadataConfig = new ConnectorConfig();
metadataConfig.setSessionId(lr.getSessionId());
// Set the metdata server url from LoginResult
metadataConfig.setServiceEndpoint(lr.getMetadataServerUrl());
MetadataConnection metadataConnection = com.sforce.soap.metadata.Connector.newConnection(metadataConfig);
DescribeMetadataResult describeMetadata = metadataConnection.describeMetadata(15.0);
System.out.println(describeMetadata);