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
Abilash.SAbilash.S 

Session Id invalid when try to Insert using SOAP

Hello Everyone,
Session Id invalid when tried to insert listviews using SOAP
I need to create list view through custom LWC comp. For this I am using below code in apex class. ---------
HTTP h = new HTTP(); 
HTTPRequest req = new HTTPRequest(); 
req.setMethod('POST'); req.setHeader('Content-Type', 'text/xml'); 
req.setHeader('SOAPAction', 'create'); 
String b = '<?xml version="1.0" encoding="UTF-8"?>'; 
b += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'; 
b += '<soapenv:Header>'; 
b += '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">'; 
b += '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>'; 
b += '</ns1:SessionHeader>'; b += '</soapenv:Header>'; 
b += '<soapenv:Body>'; 
b += '<create xmlns="http://soap.sforce.com/2006/04/metadata">'; 
b += '<metadata xsi:type="ns2:ListView" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">'; 
//This is the API name of the list view 
b += '<fullName>Case.Test345_ListView</fullName>'; 
b += '<booleanFilter>1</booleanFilter>'; 
//Columns you want to display 
b += '<columns>NAME</columns>'; 
b += '<columns>CREATED_DATE</columns>'; 
//Filterscope should be set to Everything for every one to be able to access this List view 
b += '<filterScope>Everything</filterScope>'; 
// Enter the filter that you want to set 
b += '<filters>'; 
b += '<field>NAME</field>'; 
b += '<operation>equals</operation>'; 
b += '<value>Test123 </value>'; 
b += '</filters>'; 
b += '<label>Test345 View</label>'; 
b += '</metadata>'; 
b += '</create>'; 
b += '</soapenv:Body>'; 
b += '</soapenv:Envelope>'; 
req.setBody(b); 
req.setCompressed(false); 
// Set this to org's endpoint and add it to the remote site settings. req.setEndpoint('https://ap1.salesforce.com/services/Soap/m/25.0'); 
HTTPResponse resp = h.send(req); 
System.debug(resp.getBody());
--------------------------------------
I tried to get response through anonymous window, getting an error like. USER_DEBUG This error usually occurs after a session expires or a user logs out. Decoder: DataInDbSessionKeyDecoder</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>....
1. I did logout and login. 
2.Changed session settings.
Eventhough its not working. I tried system.debug(userinfo.getsessionId()); in anonymous, the result is SESSION_ID_REMOVED.
How to figure it out. Does this not work in sandbox. NOTE: The code perfectly working in our personal DEV ORG. I can successfully insert list views when executed in anonymous window. But cannot in this sandbox. Please help.

Thanks
SwethaSwetha (Salesforce Developers) 
HI Abilash,
Please see if you have "Lock Sessions to the domain in which they were first used" checkbox checked in session settings.

Related: https://developer.salesforce.com/forums/?id=9060G000000BiXQQA0

In regards to debug logs showing SESSION_ID_REMOVED, it is expected behavior due to security reasons. See https://salesforce.stackexchange.com/questions/179845/session-id-remove-issue-in-api-callouts-in-salesforce

Thanks