1

I have User Name and Password. How can I authenticate in Metadata API in Salesforce.

String USERNAME = "YOUR-USERNAME";
String PASSWORD = "YOUR-PASSWORD&SECURITY-TOKEN";            
SF.SFMetaData.MetadataService metadataService = new SFMetaData.MetadataService();

I Want to use Create Metadata method. So i need to Authenticate with Username and password.

SF.SFMetaData.SaveResult[] results = metadataService.createMetadata(new SF.SFMetaData.Metadata[] { co });
  • Please note when applying tags that this is the Salesforce Stack Exchange. You do not need to use tags to indicate your problem is related to Salesforce (if it's not related then it's off topic for the site). Please do not add tags just because they have the word Salesforce in them, but rather look for tags which actually have something to do with the feature you are asking about. – Adrian Larson Dec 16 '17 at 16:31
0

You can use the login() method from the Partner API. The session ID that it returns can be used directly with the Metadata API. You will need to use the metadataServerUrl from the LoginResult as the URL for subsequent requests.


Try something like the following to get you started:

//Generated by FuseIT WSDL2Apex (http://www.fuseit.com/Solutions/SFDC-Explorer/Help-WSDL-Parser.aspx)
//Warning: '<xsd:anyType>' element anyType has been converted to string in NameObjectValuePair//Warning: '<xsd:anyType>' element anyType has been converted to string in Field//Methods Included: login
//Methods Excluded: describeSObject, describeSObjects, describeGlobal, describeDataCategoryGroups, describeDataCategoryGroupStructures, describeKnowledgeSettings, describeFlexiPages, describeAppMenu, describeGlobalTheme, describeTheme, describeLayout, describeSoftphoneLayout, describeSearchLayouts, describeSearchScopeOrder, describeCompactLayouts, describePathAssistants, 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, renderEmailTemplate, performQuickActions, describeQuickActions, describeAvailableQuickActions, retrieveQuickActionTemplates, describeNouns
// Primary Port Class Name: Soap    
public class partnerSoapSforceCom {
    public class CallOptions_element {
        public String client;
        public String defaultNamespace;
        public Boolean returnFieldDataTypes;
        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[] returnFieldDataTypes_type_info = new String[]{'returnFieldDataTypes','urn:partner.soap.sforce.com','boolean','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','returnFieldDataTypes'};
    }
    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 Soap {
        public String endpoint_x = 'https://login.salesforce.com/services/Soap/u/36.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;
        }
    }
}

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.