Salesforce WSC Metadata WSDL Connector Configuration Issue – Solved!

I am working and doing R&D with the Salesforce WSC library to get Tolerado ported to it. Most of the pieces worked well, but I was initially stuck with Metadata WSDL issues. I faced the following two major blockers:

  1. Metadata WSDL Compilation Issue.

  2. Metadata Connector Configuration Issue.

Both of these are explained 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 got rid of this one by using 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 the 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 is 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, I found that we need to set the correct Metadata server URL to the ConnectorConfig. Obtaining the metadata URL was straightforward with Apache Axis, as we needed 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 the reference to LoginResult. 

So finally, the following code is the 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);
CTA Banner - Contact Form

Drop a note below to move forward with the conversation 👇🏻

Abhinav Gupta

First Indian Salesforce MVP, rewarded Eight times in a row, has been blogging about Salesforce, Cloud, AI, & Web3 since 2011. Founded 1st Salesforce Dreamin event in India, called “Jaipur Dev Fest”. A seasoned speaker at Dreamforce, Dreamin events, & local meets. Author of many popular GitHub repos featured in official Salesforce blogs, newsletters, and books.

https://abhinav.fyi
Previous
Previous

Tolerado SFDC WSC Helper APIs

Next
Next

How Tolerado improves the Salesforce Web Service Client code !