TransWikia.com

I am doing API Automation Testing using JSForce to connect with Salesforce Connected App to create some data like "Opportunities" , "Accounts"

Salesforce Asked by chaitanya vn on October 4, 2021

I am trying to connect to Salesforce using jsforce and create the Account using below code:

    const jsforce = require("jsforce");
    conn = new jsforce.Connection({
    // oauth2 : {
      // you can change loginUrl to connect to sandbox or prerelease env.
      loginUrl : 'https://test.salesforce.com/'
    
  });
    conn.login('username', 'password', 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);
        // ...

         
  });

 // Multiple records creation
        conn.sobject("Account").create([
          { Name : 'My Account #1' },
          { Name : 'My Account #2' }
          ],
          function(err, rets) {
              if (err) { return console.error(err); }
              for (var i=0; i < rets.length; i++) {
                  if (rets[i].success) {
                      console.log("Created record id : " + rets[i].id);
                  }
          }
      });

I am getting below error:

Error: Invalid URI "/services/data/v42.0/composite/sobjects"
    at Request.init (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesrequestrequest.js:273:31)
    at new Request (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesrequestrequest.js:127:8)
    at request (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesrequestindex.js:53:10)
    at D:sourcecontroltesting-newgauge-taiko-testsnode_modulesrequestindex.js:100:12
    at createRequest (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibtransport.js:84:13)
    at Promise.promise.then (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibtransport.js:43:5)
    at HttpApi.request (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibhttp-api.js:82:47)
    at module.exports.Connection.request (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibconnection.js:332:18)
    at module.exports.Connection._createMany (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibconnection.js:808:15)
    at module.exports.Connection.insert.Connection.create (D:sourcecontroltesting-newgauge-taiko-testsnode_modulesjsforcelibconnection.js:736:14)

Please assist what is that I am doing wrong in the above code,to create the data in salesforce.

2 Answers

This is what I use to login and to pull the opportunity records:

const jsforce = require('jsforce');
require('dotenv').config()

const {SF_LOGIN_URL, USER_NAME, PASSWORD, SECURITY_TOKEN} = process.env
const conn = new jsforce.Connection({
    loginUrl: SF_LOGIN_URL
})

conn.login(USER_NAME, PASSWORD+SECURITY_TOKEN, (err, userInfo)=>{
    if(err){
        console.error(err)
    } 

    conn.query('SELECT Id, Name FROM Opportunity', function(err, opportunity){
        if(err){ return console.error(err);}
        console.log(opportunity.records)
    });
});

I think you have to declare the requirement for jsforce, and you have to add the security token.

Answered by Hvaandres on October 4, 2021

i guess your function conn.sobject should be called when you hit some address like this

    app.get(`/api/sessiondetails`, (req, res) => {
        console.log(conn);
        conn.sobject("Account").create([
            { Name : 'My Account #1' },
            { Name : 'My Account #2' }
            ],
            function(err, rets) {
                if (err) { return console.error(err); }
                for (var i=0; i < rets.length; i++) {
                    if (rets[i].success) {
                        console.log("Created record id : " + rets[i].id);
                    }
            }
        });
      
    });

or it should be inside the login function conn.login(), i guess they ran asynchronously so it may have no idea what conn.sobject is. with express

const express = require('express');
const app = express();
const port = 5000;
const jsforce = require('jsforce');

    const username = '';
    const password = '';
    
    var conn = new jsforce.Connection({
        // you can change loginUrl to connect to sandbox or prerelease env.
        loginUrl: 'https://login.salesforce.com/'
    });
    app.listen(3000, () => console.log(`Example app listening at http://localhost:3000`));

    conn.login(username, password, 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);
        // ...
   
   
   
    
    }); 

    app.get(`/api/sessiondetails`, (req, res) => {
        console.log(conn);
        conn.sobject("Account").create([
            { Name : 'My Account #1' },
            { Name : 'My Account #2' }
            ],
            function(err, rets) {
                if (err) { return console.error(err); }
                for (var i=0; i < rets.length; i++) {
                    if (rets[i].success) {
                        console.log("Created record id : " + rets[i].id);
                    }
            }
        });
      
    });

Answered by User6670 on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP