1

The below is working really well, but every now and then it times out after awhile with this error:

[Error: Invalid URI "/services/data/v37.0/sobjects

see: https://github.com/jsforce/jsforce/issues/463

 const conn = new jsforce.Connection({
    // you can change loginUrl to connect to sandbox or prerelease env.
    loginUrl : 'https://test.salesforce.com'
  });


conn.login(salesforce.username, salesforce.password);


const getContact = (contactId, callback) => {
  conn.sobject('Contact')
  .findOne({Id: contactId})
  .execute(callback);
}

What is the best practice here? Should I just do this?

 const conn = new jsforce.Connection({
    // you can change loginUrl to connect to sandbox or prerelease env.
    loginUrl : 'https://test.salesforce.com'
  });





const getContact = (contactId, callback) => {
  conn.login(salesforce.username, salesforce.password); //move this line to be called every time
  conn.sobject('Contact')
  .findOne({Id: contactId})
  .execute(callback);
}
2

Logging in every request will consume a lot of API calls. Don't do it.

Preferably, use a refresh token, as outlined in the documentation.

Otherwise, simply try your call, and if you get an error, log in again.

I don't see a specific example of how to do this in the documentation, but it's probably just a matter of doing something like this:

while(true) {
    try {
        conn.sobject('Contact')
        .findOne({Id: contactId})
        .execute(callback);
        break;
    } catch(e) {
       conn.login(salesforce.username, salesforce.password);
    }
}
  • I suppose you need some sort of "retry" function to be called after conn.login? – arcom Feb 23 '17 at 12:45
  • 1
    I guess I'm unsure how/where to store the refresh token. The docs are kind of lacking here, to say the least. – arcom Feb 23 '17 at 12:46
  • @arcom According to the docs, using OAuth2 should automatically cause the refresh token to be used as needed. I'd have to dig into the source code to learn more, because, as you say, the docs seem pretty sparse. – sfdcfox Feb 23 '17 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.