Retrieve Salesforce server instance
This task use java to retrieve Salesforce server instance
Retrieve Salesforce server instance
- Create getInstance() method as following
- Call getInstance() method as following
Call getInstance() method
String username = ""; String password = ""; ConnectorConfig cc = new ConnectorConfig(); cc.setAuthEndpoint("https://login.salesforce.com/services/Soap/u/19.0"); cc.setServiceEndpoint("https://login.salesforce.com/services/Soap/u/19.0"); cc.setManualLogin(true); PartnerConnection conn = Connector.newConnection(cc); LoginResult loginResult = conn.login(username, password); conn.setSessionHeader(loginResult.getSessionId()); String serverUrl = loginResult.getServerUrl(); cc.setServiceEndpoint(serverUrl); String instance = getInstance(loginResult); logger.info(instance);getInstance() method
public static String getInstance(LoginResult loginResult) { String url = loginResult.getServerUrl(); String pat1 = "https://"; String pat2 = "-api.salesforce.com"; int pos1 = url.indexOf(pat1); int pos2 = url.indexOf(pat2); String instance = "ap1"; if (pos1 >= 0 && pos2 >= 0 && pos1 + pat1.length() < pos2) { instance = url.substring(pos1 + pat1.length(), pos2); } return instance; }
No comments:
Post a Comment