I am trying to create a list view on a custom object from nodejs server. I have constructed a the SOAP(XML) body like I had done here, but I keep getting the Premature End of File
error, which doesn't make sense as the XML is properly parsed and the list get created if I do it from salesforce's developer console.
I got a few link, here and here, on this error but I was unable to figure out the solution for this.
nodeServer.js:
var https = require('https');
var userSessionId = 'sessionId';
var 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>'+userSessionId+'</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>Car_Object__c.Merc_View</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>Mercedes</value>';
b += '</filters>';
b += '<label>Mercedes View</label>';
b += '</metadata>';
b += '</create>';
b += '</soapenv:Body>';
b += '</soapenv:Envelope>';
callMyfunction();
function callMyfunction(){
var options = {
host : "myhost",
path : '/services/Soap/m/29.0',
method: "POST",
headers : {'Authorization':'Bearer '+userSessionId,
'Content-Type': 'text/xml',
'SOAPAction':'Create',
},
data : b
};
console.log('the '+ JSON.stringify(options));
var req = https.request(options, function(res){
res.setEncoding('utf8');
var output='';
res.on('data',function(chunk){
output +=chunk;
console.log('Print the chunk--> \n'+ chunk);
});
res.on('end',function(){
console.log('Final Data is--> \n'+ output);
});
});
req.on('error',function(e){
console.log('the erro msg'+ e);
});
req.end();
}
Result:
Print the chunk-->
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
<faultstring>Premature end of file.</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
Final Data is-->
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Client</faultcode>
<faultstring>Premature end of file.</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>