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
Arup SarkarArup Sarkar 

SOAP Fault: INVALID_SESSION_ID

Hello,

 

I am using the following Apex Web Service class.

 

global class ContactWebService
{
    webservice static id CreateExternalContact(String LastName, String FirstName)
    {
        Contact contact = new Contact();
    
        contact.LastName = LastName;
        contact.FirstName = FirstName;
        insert contact;
        
        return contact.id;
    }
}

  I generated the WSDL and created another apex class using that WSDL, code pasted below.

 

//Generated by wsdl2apex

public class soapSforceComSchemasClassContactweb {
    public class LogInfo {
        public String category;
        public String level;
        private String[] category_type_info = new String[]{'category','http://soap.sforce.com/schemas/class/ContactWebService','LogCategory','1','1','false'};
        private String[] level_type_info = new String[]{'level','http://soap.sforce.com/schemas/class/ContactWebService','LogCategoryLevel','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'category','level'};
    }
    public class CreateExternalContactResponse_element {
        public String result;
        private String[] result_type_info = new String[]{'result','http://soap.sforce.com/schemas/class/ContactWebService','ID','1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'result'};
    }
    public class AllowFieldTruncationHeader_element {
        public Boolean allowFieldTruncation;
        private String[] allowFieldTruncation_type_info = new String[]{'allowFieldTruncation','http://www.w3.org/2001/XMLSchema','boolean','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'allowFieldTruncation'};
    }
    public class DebuggingHeader_element {
        public soapSforceComSchemasClassContactweb.LogInfo[] categories;
        public String debugLevel;
        private String[] categories_type_info = new String[]{'categories','http://soap.sforce.com/schemas/class/ContactWebService','LogInfo','0','-1','false'};
        private String[] debugLevel_type_info = new String[]{'debugLevel','http://soap.sforce.com/schemas/class/ContactWebService','LogType','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    }
    public class CallOptions_element {
        public String client;
        private String[] client_type_info = new String[]{'client','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'client'};
    }
    public class CreateExternalContact_element {
        public String LastName;
        public String FirstName;
        private String[] LastName_type_info = new String[]{'LastName','http://www.w3.org/2001/XMLSchema','string','1','1','true'};
        private String[] FirstName_type_info = new String[]{'FirstName','http://www.w3.org/2001/XMLSchema','string','1','1','true'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'LastName','FirstName'};
    }
    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[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    }
    public class DebuggingInfo_element {
        public String debugLog;
        private String[] debugLog_type_info = new String[]{'debugLog','http://www.w3.org/2001/XMLSchema','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService','true','false'};
        private String[] field_order_type_info = new String[]{'debugLog'};
    }
    public class ContactWebService {
        public String endpoint_x = 'https://na3-api.salesforce.com/services/Soap/class/ContactWebService';
        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 soapSforceComSchemasClassContactweb.SessionHeader_element SessionHeader;
        public soapSforceComSchemasClassContactweb.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
        public soapSforceComSchemasClassContactweb.CallOptions_element CallOptions;
        public soapSforceComSchemasClassContactweb.DebuggingHeader_element DebuggingHeader;
        public soapSforceComSchemasClassContactweb.DebuggingInfo_element DebuggingInfo;
        private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/schemas/class/ContactWebService';
        private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=http://soap.sforce.com/schemas/class/ContactWebService';
        private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/schemas/class/ContactWebService';
        private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/schemas/class/ContactWebService';
        private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/schemas/class/ContactWebService';
        private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/schemas/class/ContactWebService', 'soapSforceComSchemasClassContactweb'};
        public String CreateExternalContact(String LastName,String FirstName) {
            soapSforceComSchemasClassContactweb.CreateExternalContact_element request_x = new soapSforceComSchemasClassContactweb.CreateExternalContact_element();
            soapSforceComSchemasClassContactweb.CreateExternalContactResponse_element response_x;
            request_x.LastName = LastName;
            request_x.FirstName = FirstName;
            Map<String, soapSforceComSchemasClassContactweb.CreateExternalContactResponse_element> response_map_x = new Map<String, soapSforceComSchemasClassContactweb.CreateExternalContactResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://soap.sforce.com/schemas/class/ContactWebService',
              'CreateExternalContact',
              'http://soap.sforce.com/schemas/class/ContactWebService',
              'CreateExternalContactResponse',
              'soapSforceComSchemasClassContactweb.CreateExternalContactResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.result;
        }
    }
}

 

Now I am executing the following code in Developer Console.

 

private Id testId;
soapSforceComSchemasClassContactweb.ContactWebService test = new       soapSforceComSchemasClassContactweb.ContactWebService();
        testId = test.CreateExternalContact('LExternal Contact', 'FExternal Contact');
    	System.debug('test id : ' + testId);

  I am getting the following error, how can I set the session header, I have no problem in creating the stub in java or .NET and it works, I want to make it work within salesforce framework

 

Error:

=============

Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor=

sfdcfoxsfdcfox
You have to provide a session ID, either by logging in, or by obtaining a current session ID. To this end, you'll probably need to extract the login() function of the Partner or Enterprise WSDL (at minimum) so as to make this work.