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
Alejandro IquiraAlejandro Iquira 

Example connect or authenticate to salesforce from pure Javascript

I want to authenticate using only JavaScript in a HTML page. The objective is to do something similar to web to lead: an html file that could be used to create records in a custom object without using any server application. Off course I know is not totally possible to have a web-to-lead because the authentication is needed to do any transactions in SF and I understand the risk of security hardcoding the credentials access in the page ( that part of security on that HTML is something that it will be managed).
So I've created the next example, using JSFORCE documentation but it is not working. I set some alerts to know which code parts are being executed, but the from alert("step 3" ) nothing happens:
 
<!DOCTYPE html>
<html>
<head>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jsforce/1.6.3/jsforce.min.js"></script>
    <script>    
      alert("step 1" );
      initialize();
      function initialize(){
         alert("step 2" );
         var jsforce = require('jsforce');
         alert("step 3" );
         var conn = new jsforce.Connection({
          oauth2 : {
            // you can change loginUrl to connect to sandbox or prerelease env.
            loginUrl : 'https://login.salesforce.com',
            clientId : '3weweweweweweweweweXX.EIqGx5wXExlptPkkF6alVtMlFmjFeXFpPb76N_p6puepP70VpDfjviFl3bqcIegvj',
            clientSecret : '555555767236744359',
            redirectUri : 'https://www.google.com.co'
           }
         });
        conn.login('user@domain.com', 'pasw1234POpoNARTSERxCY1rtMVf3G0QNq', function(err, userInfo) {
          if (err) { return console.error(err); }
          // Now you can get the access token and instance URL information.
          // Save them to establish connection next time.
          console.log(conn.accessToken);
          console.log(conn.instanceUrl);
          // logged in user property
          console.log("User ID: " + userInfo.id);
          console.log("Org ID: " + userInfo.organizationId);
          // ...
        });
         alert("step 4" );
      }
</script>

</head>

 <body>
    <div>
        Javascript and salesforce  Example
    </div>
 </body>
</html>
I don't need sites, because the client has to adapt to the implementation of the site meanwhile with something like web to lead, the client could integrate easy to their own sites.
Please, anybody could help what I am doing wrong?
 
Austin VillanuevaAustin Villanueva
Do you have any luck? I'm trying to do the same thing but I can't find any resources on how to do it. I'm getting No access controll allow origin. If you got any let me know. Thank you.

Austin
Alejandro IquiraAlejandro Iquira
No, I've not found any solution to have my custom web-to-lead. If you know something let know too.
Mario EijndeMario Eijnde
Hello Alejandro,
Did you have any progress over the years to get above working? I am stuck with the same.

My setup:
  1. Community site with public access
  2. The jsforce.js included as a static resource
  3. Added a connected app in the Org
  4. Included the community site (public address) in the CSP Trusted Sites
  5. Use the “var conn = new jsforce.Connection …” script tag with the secrets of the connected app
  6. Open the community site in the incognito Google window
Kind regards,
Mario