function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SurpriseSurprise 

Salesforce to Salesforce connection using WSDL

Hi All ,

 

I am trying synchronize records from salesforce to saleforce and eveything wil be devloiped using apex.No java or c sharp.

 

Given below is the Wsdl to apex class.I am trying to use  login() api to connect to another salesforce org. I am trying it through anonymouse block.The login connection gets establised for the first time.However,I am at a total loss on how to retrieve  and assign retruned  URL and  session id .Below given is the code for login to the salesforce.How do i  create header and assign session id and server url to it so that I can call subsequent API's..Can somebody please help?

 

enterpriseSoapSforceCom.LoginResult lr=new enterpriseSoapSforceCom.LoginResult();
enterpriseSoapSforceCom.soap s=new enterpriseSoapSforceCom.soap();
lr=s.login('jack@yahoo.com','Password');

 

 

 

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_hns = '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 enterpriseSoapSforceCom.LoginResult login(String username,String password) {
            enterpriseSoapSforceCom.login_element request_x = new enterpriseSoapSforceCom.login_element();
            enterpriseSoapSforceCom.loginResponse_element response_x;
            request_x.username = username;
            request_x.password = password;
            Map<String, enterpriseSoapSforceCom.loginResponse_element> response_map_x = new Map<String, enterpriseSoapSforceCom.loginResponse_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',
              'login',
              'urn:enterprise.soap.sforce.com',
              'loginResponse',
              'enterpriseSoapSforceCom.loginResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.result;
        }
            
            
        }
        
        
        }

swatKatswatKat

You can retrieve the session Id by using :

 

String sessionId=lr.sessionId;

 

And how have you implmented the class in the destination org ? If it is exposed as REST API class ,  then the http call will be something like this :

 

Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint(your server url);
            req.setMethod('GET');
            req.setHeader('Authorization', 'OAuth ' +loginResult.sessionId);
            HttpResponse res = h.send(req);

 

If it is via SOAP , you must have consumed the WSDL of the class in the destionation org right ? you have to  look for the SessionHeader_element inner class in the generated class and set the sessionId there. Post the class if  you need further clarifications.

 

 

SurpriseSurprise

Thanks Swatkat ,

I am not using rest api's in any way.Everthing here is purely sopa based.I am trying to connect salesforce to salesforcce so that changes in one org can be reflected in another salesoforce org.So I am consuming enterprise wsdl of the salesforce other org and generating wsdl to apex classes and using it's login functionlaity to establish connection with the other org and finally I am tryiing to query some records using query Api of the enterprise Wsdl.


When I run below given code it gives me an error by saying that destinaltion URL is not reset.I understand that First call to the login() api uses different URL and then login() api returns another url which we need to send with the subsequent  API' calls and session ID as well.Can u help in setting up sesion id and Destination URL

 

 

In the end is the WSDl to apex generated classes is given ,If this is what you are asking



enterpriseSoapSforceCom.LoginResult lr=new enterpriseSoapSforceCom.LoginResult();
enterpriseSoapSforceCom.soap s=new enterpriseSoapSforceCom.soap();

lr=s.login('jack@yahoo.com','password);
String g=lr.sessionid;
System.debug('The value of the session id in the new variable s is'+g);
string url=lr.serverurl; // This is the returned URL from login api.How to setup this so that  it goes with the object calling query() api.


system.debug('The value of the url in the new variable s is'+url);
s.inputHttpHeaders_x = new Map<String, String>();  //
//Setting a basic authentication header
s.inputHttpHeaders_x.put('Authorization', 'g');Setting up input headers and assigning  session id to the header as per salesforce documentation
//s.inputHttpHeaders_x .put('New Url',url);
QueryResult qResult = null;
System.debug('The value of the session id in the new variable s is'+s);
String soqlQuery='SELECT name FROM Contact';
qResult = s.query(soqlQuery);//Calling Query APi fo the enterprise Wsdl
System.debug('The value of the opportunity object opp is'+qResult);


 

 

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_hns = '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 enterpriseSoapSforceCom.LoginResult login(String username,String password) {
            enterpriseSoapSforceCom.login_element request_x = new enterpriseSoapSforceCom.login_element();
            enterpriseSoapSforceCom.loginResponse_element response_x;
            request_x.username = username;
            request_x.password = password;
            Map<String, enterpriseSoapSforceCom.loginResponse_element> response_map_x = new Map<String, enterpriseSoapSforceCom.loginResponse_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',
              'login',
              'urn:enterprise.soap.sforce.com',
              'loginResponse',
              'enterpriseSoapSforceCom.loginResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.result;
        }
            
            
        }
        
        
        }