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
MPamanMPaman 

Connecting to Salesforce.com using AJAX toolkit

I have an application external from SFDC written in html/Javascript that needs to be able to query lead information for a specified sales rep within SFDC.  I am looking at the AJAX Developers toolkit but I am having problems creating the connection and loging in.  I have included <script src="/soap/ajax/20.0/connection.js" type="text/javascript"></script> in my javascript code, but continue to receive error  "Error: System error: -2146697211." when trying to issue the login command formatted as

 

var result = sforce.connection.login("xxx-id", "xxx-password");

What is needed to create a connection and login thru javascript

b-Forceb-Force

Hey,

Do you have complete URL for script  files /soap/ajax/20.0/connection.js

 

If yes please let me know I want same

 

 

Thanks,

Bala

REST API and JavaScritpsREST API and JavaScritps

Hi.

I have a problem something similar. In my .html have the reference a connection.js of the same way the says tutorial: 

 

<script type="text/javascript" src="/soap/ajax/20.0/connection.js" ></script>

But, I have a error:

An error has occurred:

ReferenceError: sforce is not defined

 

How do you fix it?

 

 

sfdcfoxsfdcfox

If you are not on the salesforce.com server (i.e. a mashup on another server, or a desktop widget), you must include the full path to the script, including the server name.

Marty Y. ChangMarty Y. Chang

You need to use the full path to the script, or you need to download the script and save a copy to your own website.

 

The script can be found in a location such as the following:

 

http://na7.salesforce.com/soap/ajax/21.0/connection.js

zip Code APIzip Code API

I have Used following code in Asp.net .When I run this page on web Browser .I get following Error.

 

 


An error has occurred:

ReferenceError: sforce is not defined.

 

Please give me specific solution.

 

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CloseparencyDesign.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script src="/soap/ajax/20.0/connection.js" type="text/javascript"></script>

<script type="text/javascript">

window.onload = function()
{
var output = document.getElementById("output");
var startTime = new Date().getTime()
try
{

// var sforce;
//var result = sforce.connection.login("vishal@saasforce.in", "saasforce@123456sMeFebFXUHZuOTnAZ1pdwoTfT");
//alert(result);


sforce.connection.sessionId = "00D90000000KLyR!ARgAQL0OO993_pa3qzQDQZLYydrvgEeUrl2tPFEhQNsIP6Kt1ChCG1LetukcZf4J23MxJW0RGZFv1ayzHxQ8v8PoVvq6GZAL"; //Used for Session out
var queryResult = sforce.connection.query("Select Name, Industry From Account where Name!=null");

layoutResults(queryResult, output, startTime);
}
catch(error)
{
queryFailed(error, output);
}
}
//if failed for Query
function queryFailed(error, out)
{
out.innerHTML = "<font color=red>An error has occurred:</font> <p>" + error;
}
//if gets Results and pass to 'out' variable
function layoutResults(queryResult, out, startTime)
{
var timeTaken = new Date().getTime() - startTime;
if (queryResult.size > 0)
{
var output = "";
var records = queryResult.getArray('records');
for (var i = 0; i <records.length; i++)
{
var account = records[i];
output += account.Name + " [Industry - "+ account.Industry + " ]<BR>";
}
out.innerHTML = output + "<BR> query complexed in: " + timeTaken + " ms.";
}
else
{
out.innerHTML = "No records matched.";
}
}
</script>


</head>
<body>
<form id="form1" runat="server" action="/services/Soap/u/21.0" method="post">
<div id="output"> </div>
</form>
</body>
</html>

sfdcfoxsfdcfox

Does /soap/ajax/20.0/connection.js exist on your server? If not, then that is why sforce is not defined. Use the full canonical address (e.g. https://na1.salesforce.com/soap/ajax/20.0/connection.js) if you wish to include it from the salesforce.com servers. Please note that XSS security might prevent the browser from connecting successfully anyways. If you're trying to use the API from ASP directly (recommended), try using the WSDL instead.

zip Code APIzip Code API

I have used this API "https://na1.salesforce.com/soap/ajax/20.0/connection.js" in aspx page and run this page on web browser .

 

Again get  the Following Error.

 

 

Please give me the solution.

 

 

 

 

Remote invocation failed, due to:
Server Error in '/' Application.
The HTTP verb POST used to access path '/services/Soap/u/20.0' is not allowed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The HTTP verb POST used to access path '/services/Soap/u/20.0' is not allowed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): The HTTP verb POST used to access path '/services/Soap/u/20.0' is not allowed.]
System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) +2872570
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8677954
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082 status code:

sfdcfoxsfdcfox

That suggests to me that the JavaScript is attempting to access the current server (your web site!), and, of course, the API doesn't exist there, and so your script fails. Instead of attempting to connect to salesforce.com through your JavaScript, you should focus on connecting to salesforce.com through server logic (WSDL and the like), and have your page's JavaScript request data through an endpoint on your server.

riffindusriffindus
Hi,

I am trying to call salesforce using AJAX tookit from external webpage. it is not working for me.

can i know, how you resolved this issue.

Please let me know.

Regards
Aariff
MPamanMPaman
I changed over to use SOAP calls thru javascript to salesforce.com’s API using the Enterprise WSDL. I create 1 call, and pass values to it for URL, ID, Password and what sfdc object I want and what type of action. The browser I am using is IE9. function processTable2() { xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); if (xmlHttp==null) { showDialog('Warning','Your browser does not support Ajax, please upgrade to a newer version','warning','N','N','Y') return; } xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ // xml returned --- Do something } } try{ xmlHttp.open("POST", usfdcurl , false); // "https://xxx.salesforce.com/services/Soap/c/??.0" xmlHttp.setRequestHeader("User-Agent","MSIE 6.0"); xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttp.setRequestHeader("SoapAction",UAction); // Signon, Create, Delete …. xmlHttp.setRequestHeader("Content-Length",UData.length); // UData is the XML being passed xmlHttp.setRequestHeader("Host",uhost); // xxx.salesforce.com xmlHttp.send(UData); } // end try catch (ex){ // call error --- Do something return; } // end catch } // end function