1

I created a wsdl file from an SFDC instance A and generated 2 classes (ARTaskWebService and asyncARTaskWebService) from that wsdl file in another SFDC instance B.

I realized I have to get a sessionID to call the webservice. But I don't know where to get the sessionID.

Do I have to also download partner wsdl file from instance A and generate class in instance B? So I have a login method to call in instance B?

ARTaskWebService.AR_getActivitiesWebservice client=new ARTaskWebService.AR_getActivitiesWebservice();
    client.SessionHeader = new ARTaskWebService.SessionHeader_element();
    client.SessionHeader.sessionId=???? 

    List<ARTaskWebService.ARActivity> activities = client.getActivities('Client Name');
    System.debug(activities.size());

I am doing the exactly same thing as this OP's: How do invoke an apex method of one org, from another org?

  • Is this a salesforce session? If so user info.getSessionID(); if the sessionID is from the remote service outside of salesforce, look at the login() method – Eric Aug 13 '15 at 22:28
  • the sessionId is from another salesforce Org. If I download the whole partner wsdl file I get too many functions. All I want is the login method. – sfdcnoob Aug 13 '15 at 22:36
  • then you can modify the WSDL code and can remove unnecessary part which you don't require. – Tushar Sharma Aug 14 '15 at 5:40
3

You can use the FuseIT SFDC Explorer to simplify the process and keep the generated Apex to a minimum.

To do this, point the Wsdl2Apex tab at the downloaded Partner API Wsdl.

On the method selection step, uncheck all the methods except the login one. Set "Exclude" in the drop down at the top of the page that handles how extra classes are processed. This will generate the minimum required Apex.

Generate only the login method

It will look something like this: (140 lines, 10573 characters)

//Generated by FuseIT WSDL2Apex (http://www.fuseit.com/Solutions/SFDC-Explorer/Help-WSDL-Parser.aspx)
//Methods Included: login
//Methods Excluded: describeSObject, describeSObjects, describeGlobal, describeDataCategoryGroups, describeDataCategoryGroupStructures, describeKnowledgeSettings, describeFlexiPages, describeAppMenu, describeGlobalTheme, describeTheme, describeLayout, describeSoftphoneLayout, describeSearchLayouts, describeSearchScopeOrder, describeCompactLayouts, describeApprovalLayout, describeSoqlListViews, executeListView, describeSObjectListViews, describeTabs, describeAllTabs, describePrimaryCompactLayouts, create, update, upsert, merge, delete, undelete, emptyRecycleBin, retrieve, process, convertLead, logout, invalidateSessions, getDeleted, getUpdated, query, queryAll, queryMore, search, getServerTimestamp, setPassword, resetPassword, getUserInfo, sendEmailMessage, sendEmail, performQuickActions, describeQuickActions, describeAvailableQuickActions, retrieveQuickActionTemplates
// Primary Port Class Name: Soap    
public class partnerSoapSforceCom {
    public class CallOptions_element {
        public String client;
        public String defaultNamespace;
        private String[] client_type_info = new String[]{'client','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] defaultNamespace_type_info = new String[]{'defaultNamespace','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'client','defaultNamespace'};
    }
    public class GetUserInfoResult {
        public Boolean accessibilityMode;
        public String currencySymbol;
        public Integer orgAttachmentFileSizeLimit;
        public String orgDefaultCurrencyIsoCode;
        public Boolean orgDisallowHtmlAttachments;
        public Boolean orgHasPersonAccounts;
        public String organizationId;
        public Boolean organizationMultiCurrency;
        public String organizationName;
        public String profileId;
        public String roleId;
        public Integer sessionSecondsValid;
        public String userDefaultCurrencyIsoCode;
        public String userEmail;
        public String userFullName;
        public String userId;
        public String userLanguage;
        public String userLocale;
        public String userName;
        public String userTimeZone;
        public String userType;
        public String userUiSkin;
        private String[] accessibilityMode_type_info = new String[]{'accessibilityMode','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] currencySymbol_type_info = new String[]{'currencySymbol','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] orgAttachmentFileSizeLimit_type_info = new String[]{'orgAttachmentFileSizeLimit','urn:partner.soap.sforce.com','int','1','1','false'};
        private String[] orgDefaultCurrencyIsoCode_type_info = new String[]{'orgDefaultCurrencyIsoCode','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] orgDisallowHtmlAttachments_type_info = new String[]{'orgDisallowHtmlAttachments','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] orgHasPersonAccounts_type_info = new String[]{'orgHasPersonAccounts','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] organizationId_type_info = new String[]{'organizationId','urn:partner.soap.sforce.com','ID','1','1','false'};
        private String[] organizationMultiCurrency_type_info = new String[]{'organizationMultiCurrency','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] organizationName_type_info = new String[]{'organizationName','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] profileId_type_info = new String[]{'profileId','urn:partner.soap.sforce.com','ID','1','1','false'};
        private String[] roleId_type_info = new String[]{'roleId','urn:partner.soap.sforce.com','ID','1','1','true'};
        private String[] sessionSecondsValid_type_info = new String[]{'sessionSecondsValid','urn:partner.soap.sforce.com','int','1','1','false'};
        private String[] userDefaultCurrencyIsoCode_type_info = new String[]{'userDefaultCurrencyIsoCode','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] userEmail_type_info = new String[]{'userEmail','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userFullName_type_info = new String[]{'userFullName','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userId_type_info = new String[]{'userId','urn:partner.soap.sforce.com','ID','1','1','false'};
        private String[] userLanguage_type_info = new String[]{'userLanguage','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userLocale_type_info = new String[]{'userLocale','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userName_type_info = new String[]{'userName','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userTimeZone_type_info = new String[]{'userTimeZone','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userType_type_info = new String[]{'userType','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] userUiSkin_type_info = new String[]{'userUiSkin','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'accessibilityMode','currencySymbol','orgAttachmentFileSizeLimit','orgDefaultCurrencyIsoCode','orgDisallowHtmlAttachments','orgHasPersonAccounts','organizationId','organizationMultiCurrency','organizationName','profileId','roleId','sessionSecondsValid','userDefaultCurrencyIsoCode','userEmail','userFullName','userId','userLanguage','userLocale','userName','userTimeZone','userType','userUiSkin'};
    }
    public class login_element {
        public String username;
        public String password;
        private String[] username_type_info = new String[]{'username','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] password_type_info = new String[]{'password','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'username','password'};
    }
    public class loginResponse_element {
        public partnerSoapSforceCom.LoginResult result;
        private String[] result_type_info = new String[]{'result','urn:partner.soap.sforce.com','LoginResult','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'result'};
    }
    public class LoginResult {
        public String metadataServerUrl;
        public Boolean passwordExpired;
        public Boolean sandbox;
        public String serverUrl;
        public String sessionId;
        public String userId;
        public partnerSoapSforceCom.GetUserInfoResult userInfo;
        private String[] metadataServerUrl_type_info = new String[]{'metadataServerUrl','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] passwordExpired_type_info = new String[]{'passwordExpired','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] sandbox_type_info = new String[]{'sandbox','urn:partner.soap.sforce.com','boolean','1','1','false'};
        private String[] serverUrl_type_info = new String[]{'serverUrl','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] sessionId_type_info = new String[]{'sessionId','urn:partner.soap.sforce.com','string','1','1','true'};
        private String[] userId_type_info = new String[]{'userId','urn:partner.soap.sforce.com','ID','1','1','true'};
        private String[] userInfo_type_info = new String[]{'userInfo','urn:partner.soap.sforce.com','GetUserInfoResult','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'metadataServerUrl','passwordExpired','sandbox','serverUrl','sessionId','userId','userInfo'};
    }
    public class LoginScopeHeader_element {
        public String organizationId;
        public String portalId;
        private String[] organizationId_type_info = new String[]{'organizationId','urn:partner.soap.sforce.com','ID','1','1','false'};
        private String[] portalId_type_info = new String[]{'portalId','urn:partner.soap.sforce.com','ID','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'organizationId','portalId'};
    }
    public class SessionHeader_element {
        public String sessionId;
        private String[] sessionId_type_info = new String[]{'sessionId','urn:partner.soap.sforce.com','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    }
    public class Soap {
        public String endpoint_x = 'https://test.salesforce.com/services/Soap/u/33.0';
        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 partnerSoapSforceCom.LoginScopeHeader_element LoginScopeHeader;
        private String LoginScopeHeader_hns = 'LoginScopeHeader=urn:partner.soap.sforce.com';
        public partnerSoapSforceCom.CallOptions_element CallOptions;
        private String CallOptions_hns = 'CallOptions=urn:partner.soap.sforce.com';
        private String[] ns_map_type_info = new String[]{'urn:sobject.partner.soap.sforce.com','sobjectPartnerSoapSforceCom','urn:partner.soap.sforce.com','partnerSoapSforceCom','urn:fault.partner.soap.sforce.com','faultPartnerSoapSforceCom'};

        public partnerSoapSforceCom.LoginResult login(String username,String password) {
            partnerSoapSforceCom.login_element request_x = new partnerSoapSforceCom.login_element();
            partnerSoapSforceCom.loginResponse_element response_x;
            request_x.username = username;
            request_x.password = password;
            Map<String, partnerSoapSforceCom.loginResponse_element> response_map_x = new Map<String, partnerSoapSforceCom.loginResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
                this,
                request_x,
                response_map_x,
                new String[]{endpoint_x,
                '',
                'urn:partner.soap.sforce.com',
                'login',
                'urn:partner.soap.sforce.com',
                'loginResponse',
                'partnerSoapSforceCom.loginResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.result;
        }
    }
}

Then you can use the securely stored Salesforce credentials to establish the session.

partnerSoapSforceCom.Soap partner = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult lr = partner.login('user@example.com', 'SomePassword' + 'SomeSecurityToken');
System.debug(lr.sessionId);

Don't forget to add the required target URL in the remote site settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.