+ Start a Discussion
Nikki Phillips 8Nikki Phillips 8 

Query SF from Protractor automation test using JSForce and Rest api

I am attempting to get a a record id to use in my automation tests however when I attempt to execute the below code the returned value is a promise.  Within the promise I can see the id however I cannot seem to access it.  Am I going about this wrong?

    var jsforce = require('jsforce');

    function querySF() {
        var conn = new jsforce.Connection({
            // you can change loginUrl to connect to sandbox or prerelease env.
            loginUrl: 'https://www.salesforce.com'
        });
    return conn.login('some username', 'some password')
        .then(function(userInfo) { 
            // 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);
            // ...

            return conn.query("SELECT Id FROM anObject__c Where name = 'the name'")
        })
        .then(function(result){
              console.log("total : " + result.totalSize);
              console.log("fetched : " + result.records.length);
              // is returning the id
              console.log(result.records[0].Id);
              // the class that calls the methods saves it to a variable, the variable is undefined
              return result.records[0].Id;
        })
        .catch(function(err){
            console.error(err);
        });
    }

The way I am calling the method is from a different class.  The class the above method is in is called 'helper'.  from my TC class I am calling `Helper.querySF();(returns the promise)`
I have also tried 

    Helper.querySF(Id).then(function(Id){
    //do what i need to do with the id
    });
 
Divy MuniDivy Muni

I'm struggling with the same issue and not getting the result printed on my console of the query.
also please let me know How to establish connection in different class in Typescript and call conn.query() in another class