[SalesForce] sforce.connection is not working from community site.

I've created a Vf Page to upload the attachment in salesforce. I've a pulic community website where user can Register/ login their self. Logged in user can see the attachment Vf page to upload files.

The Vf Page is working in salesforce, but when I'm testing in from community site by logging as community user, the functionality is not working.

Its giving the error while connecting to salesforce by sforce.connection.

connection.js:595 POST https://dev2.cs4.force.com/services/Soap/u/28.0 500 (Server Error)
connection.js:1015 Uncaught
sforce.Xml {faultcode: "UNKNOWN_EXCEPTION", faultstring: "UNKNOWN_EXCEPTION: Site under construction"}
faultcode
:
"UNKNOWN_EXCEPTION"
faultstring
:
"UNKNOWN_EXCEPTION: Site under construction"
proto
:
Object

Error in

Connection.js

Below is the Part of javascript used in VF Page :

    userId = sforce.connection.getUserInfo().userId;

    var qr = sforce.connection.query("SELECT contactID FROM USER where ID ='" + userId + "'" );  
    var records = qr.getArray("records");
    var parentId = records[0].ContactId;   

    //Create attachment  
    var att = new sforce.SObject("Attachment");
    getdateTime();
    console.log(dateTime);
    att.Name = 'Invoice_'+dateTime; 
    att.ContentType = this.file.type;
    att.ParentId = parentId;
    var binary = "";
    var bytes = new Uint8Array(e.target.result);
    var length = bytes.byteLength;
    for (var i = 0; i < length; i++)
    {
        binary += String.fromCharCode(bytes[i]);
    }
    att.Body = (new sforce.Base64Binary(binary)).toString();
    sforce.connection.create([att],
    {
        onSuccess : function(result, source)
        { // logic},
        onFailure : function(error, source)
        {//logic}
    }

Its showing message site under construction, but I'm using the site and testing it from site. So this should not be happen.

How to resolve this.

Qustion: is sforce.connection is not working for community site.?

Best Answer

Its because you need to set server URL correctly. Because in case of site you need to take Site base URL

sforce.connection.serverUrl = '{!$Site.Prefix}/services/Soap/u/40.0';
Related Topic