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
StreepeyeStreepeye 

SessionId in Header, how to set in java?

Hi, I want to set the sessionid header element in the upsert operation! but i don't know how,... i am quit new with java. i get a header element but i dont know how to set the value which i just got from the login operation? see below "Add SOAP header" here is my generated java code: /** * SalesForceAgent_EnterpriceService.java * * This file was generated by LANSA Integrator SOAP Agent Wizard */ package com.marktselect.service.soap ; public class SalesForceAgent_EnterpriceService implements com.lansa.jsm.service.SOAPAgentHandler { /* Operation - login */ public final void setOperation_LOGIN () { } /* Operation Request - login */ public final void prepareRequest_LOGIN ( com.lansa.jsm.service.SOAPAgentRequest request ) throws Exception { request.prepareParameter ( "USERNAME", java.lang.String.class, false, false ) ; request.prepareParameter ( "PASSWORD", java.lang.String.class, false, false ) ; } /* Operation Parameter - login */ public final void setParameter_LOGIN_USERNAME ( com.lansa.jsm.service.SOAPAgentParameter parameter, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { parameter.setEntity ( bind.getFieldValue ( "MSLOGUSR" ), null, null ) ; } /* Operation Parameter - login */ public final void setParameter_LOGIN_PASSWORD ( com.lansa.jsm.service.SOAPAgentParameter parameter, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { parameter.setEntity ( bind.getFieldValue ( "MSLOGPWD" ), null, null ) ; } /* Operation Response - login */ public final void prepareResponse_LOGIN ( com.lansa.jsm.service.SOAPAgentResponse response ) throws Exception { response.prepareFragment ( new String[] { "LOGIN" }, com.sforce.soap.enterprise.LoginResult.class, false, true, null ) ; response.prepareFragment ( new String[] { "LOGIN", "USERINFO" }, com.sforce.soap.enterprise.GetUserInfoResult.class, false, true, "getUserInfo" ) ; } /* Operation Fragment - login */ public final void getFragment_LOGIN_LOGIN ( com.lansa.jsm.service.SOAPAgentResponseFragment fragment, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { bind.setFieldValue ( "URLSERVER", fragment.getEntity ( "metadataServerUrl", null ) ) ; bind.setFieldValue ( "PWEXPIRED", fragment.getEntity ( "passwordExpired", null ) ) ; bind.setFieldValue ( "SANDBOX", fragment.getEntity ( "sandbox", null ) ) ; bind.setFieldValue ( "SERVERURL", fragment.getEntity ( "serverUrl", null ) ) ; bind.setFieldValue ( "SESSIONID", fragment.getEntity ( "sessionId", null ) ) ; bind.setFieldValue ( "USERID", fragment.getEntity ( "userId", null ) ) ; } /* Operation Fragment - login */ public final void getFragment_LOGIN_USERINFO ( com.lansa.jsm.service.SOAPAgentResponseFragment fragment, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { bind.setFieldValue ( "ACCMODE", fragment.getEntity ( "accessibilityMode", null ) ) ; bind.setFieldValue ( "CURSYMBOL", fragment.getEntity ( "currencySymbol", null ) ) ; bind.setFieldValue ( "ORGISOCOD", fragment.getEntity ( "orgDefaultCurrencyIsoCode", null ) ) ; bind.setFieldValue ( "PERSACC", fragment.getEntity ( "orgHasPersonAccounts", null ) ) ; bind.setFieldValue ( "ORGID", fragment.getEntity ( "organizationId", null ) ) ; bind.setFieldValue ( "ORGMLTCUR", fragment.getEntity ( "organizationMultiCurrency", null ) ) ; bind.setFieldValue ( "MSBEDRNM", fragment.getEntity ( "organizationName", null ) ) ; bind.setFieldValue ( "MSPROFILE", fragment.getEntity ( "profileId", null ) ) ; bind.setFieldValue ( "ROLEID", fragment.getEntity ( "roleId", null ) ) ; bind.setFieldValue ( "DEFCURCOD", fragment.getEntity ( "userDefaultCurrencyIsoCode", null ) ) ; bind.setFieldValue ( "MSEMAIL", fragment.getEntity ( "userEmail", null ) ) ; bind.setFieldValue ( "MSFULLNM", fragment.getEntity ( "userFullName", null ) ) ; bind.setFieldValue ( "MSUSRID", fragment.getEntity ( "userId", null ) ) ; bind.setFieldValue ( "USERLANG", fragment.getEntity ( "userLanguage", null ) ) ; bind.setFieldValue ( "USERLOCAL", fragment.getEntity ( "userLocale", null ) ) ; bind.setFieldValue ( "USRNAME", fragment.getEntity ( "userName", null ) ) ; bind.setFieldValue ( "TIMEZONE", fragment.getEntity ( "userTimeZone", null ) ) ; bind.setFieldValue ( "MSUSRTYPE", fragment.getEntity ( "userType", null ) ) ; bind.setFieldValue ( "UISKIN", fragment.getEntity ( "userUiSkin", null ) ) ; } /* Operation Return - login */ public final void getReturnParameter_LOGIN ( com.lansa.jsm.service.SOAPAgentResponse response, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { } /* Operation Call - login */ public final com.lansa.jsm.service.SOAPAgentResponse callOperation_LOGIN ( com.lansa.jsm.service.SOAPAgentRequest request, String user, String password, String url ) throws Exception { com.sforce.soap.enterprise.SforceServiceLocator locator = new com.sforce.soap.enterprise.SforceServiceLocator () ; String endPoint = locator.getSoapAddress () ; if ( url != null ) { endPoint = url ; } com.sforce.soap.enterprise.SoapBindingStub stub = (com.sforce.soap.enterprise.SoapBindingStub)locator.getSoap ( new java.net.URL ( endPoint ) ) ; if ( user != null && password != null ) { stub.setUsername ( user ) ; stub.setPassword ( password ) ; } /* Add SOAP header */ org.apache.axis.message.SOAPHeaderElement elementHead = new org.apache.axis.message.SOAPHeaderElement ("", "SessionHeader"); javax.xml.soap.SOAPElement elementsessionId = elementHead.addChildElement ( "sessionId" ) ; elementsessionId.addTextNode ( "HOW DO I GET THE SESSIONID IN HERE?" ); stub.setHeader ( elementHead ) ; /* End SOAP header */ request.traceStub ( stub ) ; return new com.lansa.jsm.service.SOAPAgentResponse ( stub.login( request.getParameterValueString ( "USERNAME" ), request.getParameterValueString ( "PASSWORD" ) ) ) ; } /* Operation - upsert */ public final void setOperation_UPSERT () { } /* Operation Request - upsert */ public final void prepareRequest_UPSERT ( com.lansa.jsm.service.SOAPAgentRequest request ) throws Exception { request.prepareParameter ( "EXTERNALIDFIELDNAME", java.lang.String.class, false, false ) ; request.prepareParameter ( "SOBJECTS", com.sforce.soap.enterprise.sobject.SObject.class, true, true ) ; request.prepareFragment ( "SOBJECTS", new String[] { "UPSERTOBJ" }, com.sforce.soap.enterprise.sobject.SObject.class, true, true, null ) ; request.prepareList ( "SOBJECTS", new String[] { "UPSERTOBJ", "OBJNAME" }, java.lang.String.class, true, false, "setFieldsToNull" ) ; } /* Operation Parameter - upsert */ public final void setParameter_UPSERT_EXTERNALIDFIELDNAME ( com.lansa.jsm.service.SOAPAgentParameter parameter, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { parameter.setEntity ( bind.getFieldValue ( "EXTFLDNM" ), null, null ) ; } /* Operation Parameter - upsert */ public final void setParameter_UPSERT_SOBJECTS ( com.lansa.jsm.service.SOAPAgentParameter parameter, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { } /* Operation Fragment - upsert */ public final void setFragment_UPSERT_SOBJECTS_UPSERTOBJ ( com.lansa.jsm.service.SOAPAgentRequestFragment fragment, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { fragment.setEntity ( "id", bind.getFieldValue ( "SFIDSTR" ), null, null ) ; } public final com.lansa.jsm.service.SOAPAgentList setList_UPSERT_UPSERTOBJ_OBJNAME () throws Exception { return new com.lansa.jsm.service.SOAPAgentList ( new String[] { "OBJELEM" }, new String[] { "entry" }, new com.lansa.jsm.service.SOAPEntityFormat[] { null } ) ; } /* Operation Response - upsert */ public final void prepareResponse_UPSERT ( com.lansa.jsm.service.SOAPAgentResponse response ) throws Exception { response.prepareFragment ( new String[] { "UPSERTRESULT" }, com.sforce.soap.enterprise.UpsertResult.class, true, true, null ) ; response.prepareFragment ( new String[] { "UPSERTRESULT", "ERRORS" }, com.sforce.soap.enterprise.Error.class, true, true, "getErrors" ) ; response.prepareList ( new String[] { "UPSERTRESULT", "ERRORS", "OBJRETLST" }, java.lang.String.class, true, false, "getFields" ) ; } /* Operation Fragment - upsert */ public final void getFragment_UPSERT_UPSERTRESULT ( com.lansa.jsm.service.SOAPAgentResponseFragment fragment, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { bind.setFieldValue ( "ISCREATED", fragment.getEntity ( "created", null ) ) ; bind.setFieldValue ( "SFRETID", fragment.getEntity ( "id", null ) ) ; bind.setFieldValue ( "ISSUCCES", fragment.getEntity ( "success", null ) ) ; } /* Operation Fragment - upsert */ public final void getFragment_UPSERT_ERRORS ( com.lansa.jsm.service.SOAPAgentResponseFragment fragment, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { bind.setFieldValue ( "MSERRMSG", fragment.getEntity ( "message", null ) ) ; bind.setFieldValue ( "MSERRCODS", fragment.getEntity ( "statusCode", null ) ) ; } public final com.lansa.jsm.service.SOAPAgentList getList_UPSERT_ERRORS_OBJRETLST () throws Exception { return new com.lansa.jsm.service.SOAPAgentList ( new String[] { "OBJELEM" }, new String[] { "entry" }, new com.lansa.jsm.service.SOAPEntityFormat[] { null } ) ; } /* Operation Return - upsert */ public final void getReturnParameter_UPSERT ( com.lansa.jsm.service.SOAPAgentResponse response, com.lansa.jsm.service.SOAPAgentBind bind ) throws Exception { } /* Operation Call - upsert */ public final com.lansa.jsm.service.SOAPAgentResponse callOperation_UPSERT ( com.lansa.jsm.service.SOAPAgentRequest request, String user, String password, String url ) throws Exception { com.sforce.soap.enterprise.SforceServiceLocator locator = new com.sforce.soap.enterprise.SforceServiceLocator () ; String endPoint = locator.getSoapAddress () ; if ( url != null ) { endPoint = url ; } com.sforce.soap.enterprise.SoapBindingStub stub = (com.sforce.soap.enterprise.SoapBindingStub)locator.getSoap ( new java.net.URL ( endPoint ) ) ; if ( user != null && password != null ) { stub.setUsername ( user ) ; stub.setPassword ( password ) ; } /* Add SOAP header */ org.apache.axis.message.SOAPHeaderElement elementHead = new org.apache.axis.message.SOAPHeaderElement ("", "SessionHeader"); javax.xml.soap.SOAPElement elementsessionId = elementHead.addChildElement ( "sessionId" ) ; elementsessionId.addTextNode ( "HOW DO I GET THE SESSIONID IN HERE?" ); stub.setHeader ( elementHead ) ; /* End SOAP header */ request.traceStub ( stub ) ; return new com.lansa.jsm.service.SOAPAgentResponse ( stub.upsert( request.getParameterValueString ( "EXTERNALIDFIELDNAME" ), (com.sforce.soap.enterprise.sobject.SObject[])request.getParameterValueObject ( "SOBJECTS" ) ) ) ; } } this is the generated reguest: - - - HOW DO I GET THE SESSIONID IN HERE? - - mskey__c - 34 11111
StreepeyeStreepeye
Sorry not realy readable.... and i cannot delete it!
shillyershillyer

Check out the docs of the login() call. It contains sample Java code to set the session header:

 

// Create a new session header object

// add the session ID returned from the login

 

_SessionHeader sh = new _SessionHeader();

sh.setSessionId(loginResult.getSessionId());

 

// Set the session header for subsequent call authentication

 

binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);

  

 

Hope that helps,

Sati

SteveBowerSteveBower

This works.  Hope it helps, Steve.

 

p.s. SFDC_Username and Password are obviously what you need to change, and you probably don't want all my GS2SF_xxxx exception code.

 

 

 

public void loginSFDC() throws GS2SF_SFDC_LOGIN_Exception { logger.info("Trying to log into Salesforce.com with username: " + SFDC_Username); try { binding = (SoapBindingStub) new SforceServiceLocator().getSoap(); } catch (ServiceException e) { throw new GS2SF_SFDC_LOGIN_Exception("Exception getting the Soap binding. Exception: ", e); } try { lr = binding.login(SFDC_Username, SFDC_Password); } catch (UnexpectedErrorFault e) { throw new GS2SF_SFDC_LOGIN_Exception("UnexpectedErrorFault trying to Logon to Salesforce. UnexpectedErrorFault: ", e); } catch (LoginFault e) { throw new GS2SF_SFDC_LOGIN_Exception("LoginFault trying to Logon to Salesforce. LoginFault: ", e); } catch (RemoteException e) { throw new GS2SF_SFDC_LOGIN_Exception("RemoteException trying to Logon to Salesforce. RemoteException: ", e); } try { logger.debug("Login was successfull. Logged in as: " + lr.getUserInfo().getUserName() + ", " + lr.getUserInfo().getOrganizationName() + ", " + lr.getUserId()); // On a successful login, set up the session id and the url for subsequent calls // reset the url endpoint property, this will cause subsequent calls // to made to the serverURL from the login result binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, lr.getServerUrl()); // create a session head object SessionHeader sh = new SessionHeader(); // set the sessionId property on the header object using // the value from the login result sh.setSessionId(lr.getSessionId()); // add the header to the binding stub String sforceURI = new SforceServiceLocator().getServiceName().getNamespaceURI(); binding.setHeader(sforceURI, "SessionHeader", sh); // set the Batchsize to 2000 (SFDC MAx) QueryOptions qo = new QueryOptions(); qo.setBatchSize(new Integer(2000)); binding.setHeader(new SforceServiceLocator().getServiceName() .getNamespaceURI(), "QueryOptions", qo); return; } catch (Exception e) { throw new GS2SF_SFDC_LOGIN_Exception("Exception while loading Salesforce binding service after successful login. Exception: ", e); } }