Salesforce to salesforce using enterprise WSDL files

I am trying to connect salesforce to salesforce using enterprise wsdl file.I have been able to login and then I have assigned returned session id to the session header object through which request will be send to access further salesforce API such as query.However,It gives me an error ,Destination URL not set and i have tried whatever I could think of it is not working,Can somebody please help.Below is the code Which I have executed in Anonymous block and down below it is given Classes generated from WSDL files.Can somebody please help.?

enterpriseSoapSforceCom.soap asp=new enterpriseSoapSforceCom.soap();
enterpriseSoapSforceCom.LoginResult lap=asp.login('jack@yahoo.com','Champion');  
System.debug('The value in the Url has been'+lap); 
enterpriseSoapSforceCom.SessionHeader_element SessionHeader=new enterpriseSoapSforceCom.SessionHeader_element();
sessionheader.sessionid=lap.sessionid;
asp.sessionheader=sessionheader;
asp.inputHttpHeaders_x = new Map<String, String>();
string u=lap.serverurl;  
//Setting a basic authentication header
asp.inputHttpHeaders_x.put('URL',u);
//asp.endpoint_x='https://na15.salesforce.com/services/Soap/c/28.0/00Di0000000J1Tf/0DFi0000000PCEo';
enterpriseSoapSforceCom.QueryResult l=asp.query('Select name from opportunity limit 3');
//System.debug('The values which have been returned after querying opportunity object has been'+l);

public class enterpriseSoapSforceCom
     {
        public class getUserInfo_element
     {
            private String[] apex_schema_type_info = new String[]
                 {
                   'urn:enterprise.soap.sforce.com','true','false'};
                    private String[] field_order_type_info = new String[]{};
                  }
        public class LoginResult
           {
            public String metadataServerUrl;
            public Boolean passwordExpired;
            public Boolean sandbox;
            public String serverUrl;
            public String sessionId;
            public String userId;
            public enterpriseSoapSforceCom.GetUserInfoResult userInfo;
            private String[] metadataServerUrl_type_info = new String[]{
    {'metadataServerUrl','http://www.w3.org/2001/XMLSchema','string','1','1','true'};
            private String[] passwordExpired_type_info = new String[]{'passwordExpired','http://www.w3.org/2001/XMLSchema','boolean','1','1','false'};
            private String[] sandbox_type_info = new String[]{'sandbox','http://www.w3.org/2001/XMLSchema','boolean','1','1','false'};
            private String[] serverUrl_type_info = new String[]{'serverUrl','http://www.w3.org/2001/XMLSchema','string','1','1','true'};
            private String[] sessionId_type_info = new String[]{'sessionId','http://www.w3.org/2001/XMLSchema','string','1','1','true'};
            private String[] userId_type_info = new String[]{'userId','urn:enterprise.soap.sforce.com','ID','1','1','true'};
            private String[] userInfo_type_info = new String[]{'userInfo','urn:enterprise.soap.sforce.com','GetUserInfoResult','0','1','false'};
            private String[] apex_schema_type_info = new String[]{'urn:enterprise.soap.sforce.com','true','false'};
            private String[] field_order_type_info = new String[]{'metadataServerUrl','passwordExpired','sandbox','serverUrl','sessionId','userId','userInfo'};
        }

     public class Soap {
            public String endpoint_x = 'https://login.salesforce.com/services/Soap/c/28.0/0DFi0000000PCEo';
            public Map<String,String> inputHttpHeaders_x;
            public Map<String,String> outputHttpHeaders_x;
            public String clientCertName_x;
            public String clientCert_x;
            public String clientCertPasswd_x;
            public Integer timeout_x;
            public enterpriseSoapSforceCom.QueryOptions_element QueryOptions;
            public enterpriseSoapSforceCom.EmailHeader_element EmailHeader;
            public enterpriseSoapSforceCom.DebuggingInfo_element DebuggingInfo;
            public enterpriseSoapSforceCom.MruHeader_element MruHeader;
            public enterpriseSoapSforceCom.PackageVersionHeader_element PackageVersionHeader;
            public enterpriseSoapSforceCom.SessionHeader_element SessionHeader;
            public enterpriseSoapSforceCom.UserTerritoryDeleteHeader_element UserTerritoryDeleteHeader;
            public enterpriseSoapSforceCom.LoginScopeHeader_element LoginScopeHeader;
            public enterpriseSoapSforceCom.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
            public enterpriseSoapSforceCom.DebuggingHeader_element DebuggingHeader;
            public enterpriseSoapSforceCom.AllOrNoneHeader_element AllOrNoneHeader;
            public enterpriseSoapSforceCom.LocaleOptions_element LocaleOptions;
            public enterpriseSoapSforceCom.OwnerChangeOptions_element OwnerChangeOptions;
            public enterpriseSoapSforceCom.AssignmentRuleHeader_element AssignmentRuleHeader;
            public enterpriseSoapSforceCom.DisableFeedTrackingHeader_element DisableFeedTrackingHeader;
            public enterpriseSoapSforceCom.StreamingEnabledHeader_element StreamingEnabledHeader;
            private String QueryOptions_hns = 'QueryOptions=urn:enterprise.soap.sforce.com';
            private String EmailHeader_hns = 'EmailHeader=urn:enterprise.soap.sforce.com';
            private String DebuggingInfo_hns = 'DebuggingInfo=urn:enterprise.soap.sforce.com';
            private String MruHeader_hns = 'MruHeader=urn:enterprise.soap.sforce.com';
            private String PackageVersionHeader_hns = 'PackageVersionHeader=urn:enterprise.soap.sforce.com';
            private String SessionHeader_hns = 'SessionHeader=urn:enterprise.soap.sforce.com';
            private String UserTerritoryDeleteHeader_hns = 'UserTerritoryDeleteHeader=urn:enterprise.soap.sforce.com';
            private String LoginScopeHeader_hns = 'LoginScopeHeader=urn:enterprise.soap.sforce.com';
            private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=urn:enterprise.soap.sforce.com';
            private String DebuggingHeader_hns = 'DebuggingHeader=urn:enterprise.soap.sforce.com';
            private String AllOrNoneHeader_hns = 'AllOrNoneHeader=urn:enterprise.soap.sforce.com';
            private String LocaleOptions_hns = 'LocaleOptions=urn:enterprise.soap.sforce.com';
            private String OwnerChangeOptions_hns = 'OwnerChangeOptions=urn:enterprise.soap.sforce.com';
            private String AssignmentRuleHeader_hns = 'AssignmentRuleHeader=urn:enterprise.soap.sforce.com';
            private String DisableFeedTrackingHeader_hns='DisableFeedTrackingHeader=urn:enterprise.soap.sforce.com';
            private String StreamingEnabledHeader_hn='StreamingEnabledHeader=urn:enterprise.soap.sforce.com';
            private String[] ns_map_type_info = new String[
    {'urn:fault.enterprise.soap.sforce.com', 'faultEnterpriseSoapSforceCom', 'urn:enterprise.soap.sforce.com', 'enterpriseSoapSforceCom', 'urn:sobject.enterprise.soap.sforce.com', 'sobjectEnterpriseSoapSforceCom'};
            public enterpriseSoapSforceCom.SearchResult search_x(String searchString)
     {
                enterpriseSoapSforceCom.search_element request_x = new enterpriseSoapSforceCom.search_element();
                enterpriseSoapSforceCom.searchResponse_element response_x;
                request_x.searchString = searchString;
                Map<String, enterpriseSoapSforceCom.searchResponse_element> response_map_x = new Map<String, enterpriseSoapSforceCom.searchResponse_element>();
                response_map_x.put('response_x', response_x);
                WebServiceCallout.invoke(
                  this,
                  request_x,
                  response_map_x,
                  new String[]{endpoint_x,
                  '',
                  'urn:enterprise.soap.sforce.com',
                  'search',
                  'urn:enterprise.soap.sforce.com',
                  'searchResponse',
                  'enterpriseSoapSforceCom.searchResponse_element'}
                );
                response_x = response_map_x.get('response_x');
                return response_x.result;
            }

    public class SessionHeader_element 
    {
            public String sessionId;
            private String[] sessionId_type_info = new String[]{'sessionId','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
            private String[] apex_schema_type_info = new String[]{'urn:enterprise.soap.sforce.com','true','false'};
            private String[] field_order_type_info = new String[]{'sessionId'};
        }

Answer

You need to set the enterpriseSoapSforceCom.endpoint_x to the LoginResult.serverUrl.

enterpriseSoapSforceCom.soap binding = new enterpriseSoapSforceCom.soap();
enterpriseSoapSforceCom.LoginResult lr = binding.login('user@example.com','passwordAndToken');  

//Update the binding endpoint as per the LoginResult
binding.endpoint_x = lr.ServerUrl;

binding.SessionHeader = new enterpriseSoapSforceCom.SessionHeader_element();
binding.SessionHeader.sessionid=lap.sessionid;

enterpriseSoapSforceCom.QueryResult l = binding.query('Select name from opportunity limit 3');

Also, you may need to add the Salesforce Pod/Server URL to allow outbound web service calls to it.

Attribution
Source : Link , Question Author : Sales , Answer Author : Daniel Ballinger

Leave a Comment