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:
Metadata WSDL Compilation Issue.
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);
Drop a note below to move forward with the conversation 👇🏻