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
PanchoPancho 

Using SDK for Angular Bootstrap Node - How can I limit query with ownerId=User.Id?

All of the queries on the sample apps have no Where clauses.  I need to put in a Where clause that limits the query to ownerId= the current user id.

I'm not sure how to capture the current UserId and what that would look like.

 

Here is what I have tried.

 

I edited this function in angular-force.js

            function salesforceSessionRefreshed(creds) {
                // Depending on how we come into this method, `creds` may be callback data from the auth
                // plugin, or an event fired from the plugin.  The data is different between the two.
                var credsData = creds;
                if (creds.data)  // Event sets the `data` object with the auth data.
                    credsData = creds.data;

                SFConfig.client = new forcetk.Client(credsData.clientId, credsData.loginUrl);
                SFConfig.client.setSessionToken(credsData.accessToken, apiVersion, credsData.instanceUrl);
                SFConfig.client.setRefreshToken(credsData.refreshToken);

                //added to get UserID
                var userId = credsData.userId;

                //Set sessionID to angularForce coz profileImages need them
                self.sessionId = SFConfig.client.sessionId;

                callback && callback();
            }

 And then I edited app.js with this where clause, on a custom object.

angular.module('Store', []).factory('Store', function (AngularForceObjectFactory) {
    //Describe the store object
    var objDesc = {
        type: 'Retail_Store__c',
        fields: ['Retailer_Store_Type__c', 'Account_Related_to__r.Name', 'Address_1__c', 'City__c', 'State__c', 'Phone__c',
            'Field_Team_Tier__c', 'Lat__c', 'Long__c', 'Location_Note__c', 'Id'],
        where: 'ownerId='+userId,
        orderBy: 'Account_Related_to__c',
        limit: 20
    };
    var Store = AngularForceObjectFactory(objDesc);

    return Store;
});

 Any Ideas?  Any help is appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
Raja Rao DVRaja Rao DV

Currently we are not saving current user's id in forcetk or angular-force.js. But you can get it by parsing oauth_callback response from salesforce. Salesforce's oauth response will include "id" which is the url for the current user and it looks like:

 https://login.salesforce.com/id/{orgId}/{currentUserId} 
i.e: "https://login.salesforce.com/id/00Di0000000YFrXEAW/005i0000000OGeyAAG" 

From that "id", you can then get userId.,  

 

 

To get access to callback response and then to parse id, change the callback controller from:

function CallbackCtrl($scope, AngularForce, $location) {
    AngularForce.oauthCallback(document.location.href);

    //Note: Set hash to empty before setting path to /contacts to keep the url clean w/o oauth info.
    //..coz oauth CB returns access_token in its own hash making it two hashes (1 from angular,
    // and another from oauth)
    $location.hash('');
    $location.path('/contacts/' +  app.INITIAL_EMAIL_FOR_ORG_CHART);
to:

function CallbackCtrl($scope, AngularForce, $location, SFConfig) {
SFConfig.currentUserId = getUserId(document.location.href);// get current user's id and save it on SFConfig and so you can do SFConfig.currentUserId to access it at other places. AngularForce.oauthCallback(document.location.href); //Note: Set hash to empty before setting path to /contacts to keep the url clean w/o oauth info. //..coz oauth CB returns access_token in its own hash making it two hashes (1 from angular, // and another from oauth) $location.hash(''); $location.path('/contacts/' + app.INITIAL_EMAIL_FOR_ORG_CHART); } function getUserId(loc) { var oauthResponse = {}, fragment = loc.split("#")[2]; if (fragment) { var nvps = fragment.split('&'); for (var nvp in nvps) { var parts = nvps[nvp].split('='); //Note some of the values like refresh_token might have '=' inside them //so pop the key(first item in parts) and then join the rest of the parts with = var key = parts.shift(); var val = parts.join('='); oauthResponse[key] = decodeURIComponent(val); } } var id = oauthResponse.id; return id.substring(id.lastIndexOf("/") + 1, id.length); }

 

 

 

All Answers

Raja Rao DVRaja Rao DV

How does your actual/ complete SOQL look like?

PanchoPancho

I am new to Angular and Node, so I am not sure exactly where to see the exact soql statement.

But, it does work fine if I hard code the id into the code like below.

 

angular.module('Store', []).factory('Store', function (AngularForceObjectFactory) {
    //Describe the store object
    var objDesc = {
        type: 'Retail_Store__c',
        fields: ['Retailer_Store_Type__c', 'Account_Related_to__r.Name', 'Address_1__c', 'City__c', 'State__c', 'Phone__c',
            'Field_Team_Tier__c', 'Lat__c', 'Long__c', 'Location_Note__c', 'Id'],
        where: 'ownerId='+"'"+'00550000000wTGY'+"'",
        orderBy: 'Account_Related_to__c',
        limit: 20
    };
    var Store = AngularForceObjectFactory(objDesc);

    return Store;
});

 But my problem is still, how do I grab the current user's id so I can put it in there?

 

Gaurav KheterpalGaurav Kheterpal

If you are using ngForce, the SalesforceOAuthPlugin class has a methoda called getAuthCredentials() that returns

 

a dictionary with:
* accessToken
* refreshToken
* clientId
* userId
* orgId
* loginUrl
* instanceUrl
* userAgent

 

This will get you the userId of the current user from the oAuth dance. Hope this helps.

 

Regards,
Gaurav

Raja Rao DVRaja Rao DV

Currently we are not saving current user's id in forcetk or angular-force.js. But you can get it by parsing oauth_callback response from salesforce. Salesforce's oauth response will include "id" which is the url for the current user and it looks like:

 https://login.salesforce.com/id/{orgId}/{currentUserId} 
i.e: "https://login.salesforce.com/id/00Di0000000YFrXEAW/005i0000000OGeyAAG" 

From that "id", you can then get userId.,  

 

 

To get access to callback response and then to parse id, change the callback controller from:

function CallbackCtrl($scope, AngularForce, $location) {
    AngularForce.oauthCallback(document.location.href);

    //Note: Set hash to empty before setting path to /contacts to keep the url clean w/o oauth info.
    //..coz oauth CB returns access_token in its own hash making it two hashes (1 from angular,
    // and another from oauth)
    $location.hash('');
    $location.path('/contacts/' +  app.INITIAL_EMAIL_FOR_ORG_CHART);
to:

function CallbackCtrl($scope, AngularForce, $location, SFConfig) {
SFConfig.currentUserId = getUserId(document.location.href);// get current user's id and save it on SFConfig and so you can do SFConfig.currentUserId to access it at other places. AngularForce.oauthCallback(document.location.href); //Note: Set hash to empty before setting path to /contacts to keep the url clean w/o oauth info. //..coz oauth CB returns access_token in its own hash making it two hashes (1 from angular, // and another from oauth) $location.hash(''); $location.path('/contacts/' + app.INITIAL_EMAIL_FOR_ORG_CHART); } function getUserId(loc) { var oauthResponse = {}, fragment = loc.split("#")[2]; if (fragment) { var nvps = fragment.split('&'); for (var nvp in nvps) { var parts = nvps[nvp].split('='); //Note some of the values like refresh_token might have '=' inside them //so pop the key(first item in parts) and then join the rest of the parts with = var key = parts.shift(); var val = parts.join('='); oauthResponse[key] = decodeURIComponent(val); } } var id = oauthResponse.id; return id.substring(id.lastIndexOf("/") + 1, id.length); }

 

 

 

This was selected as the best answer
PanchoPancho

Raja,

That's exactly what I was looking for.

Thank you so much!!

 

I think this post will really help out others.

 

Thank you also for that great video that you posted on Angular, it helped greatly.

 

I'm a fan. =o)