cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 - User Login and Corresponding Employee ID

Firoz_Ashraf
Contributor
0 Kudos

Hi,

I want to capture the logged in user and it's corresponding employee ID stored in HR Table.

I have successfully captured the login user and have built the oData to fetch the employee ID based on login ID.

However, I am not able to pass the login ID (captured as global variable in index.html) to the other controller.

here is the code to capture login ID in my  Index file.


<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/error.js"></script>

<script src="/sap/public/bc/ui2/services/sap/ui2/srvc/utils.js"></script>

<script src="/sap/public/bc/ui2/shell-api/sap/ui2/shell/startup.js"></script>

  <script>

  var oUser = sap.ui2.shell.getUser();

        oUser.load({}, function() {

         userID = oUser.getId(); // HERE I AM DECLARING userID AS A GLOBAL VARIABLE

      }, function() {

         alert('Error');

  });

  </script>

Now I want to get the value of the global variable userID in onInit function of my controller file so that I can make my oData call.


onInit : function() {

   console.log("User ID"+userID);  //userID IS NOT VISIBLE HERE

  },

But I am not getting the value in userID.

Console is giving the message that userID is not defined.

Any help would be highly appreciated.

Regards,

Firoz.

Accepted Solutions (0)

Answers (1)

Answers (1)

jamie_cawley
Advisor
Advisor
0 Kudos

load is an async function, which means it's callback function is probably ran after your controller's onInit.  You could either place a call to the odata call function within the load's callback or use a promise to run it.  You could also use a model to store the user info.

Regards,

Jamie

SAP - Technology RIG

Firoz_Ashraf
Contributor
0 Kudos

Hi Jamie,

Could you please give some code snippet on how to place a call to the odata call function within the load's callback as well as on how to 'use a promise to run it'?

Many Thanks,

Firoz.

jamie_cawley
Advisor
Advisor
0 Kudos

If you placed this in the controller you could use

oUser.load({}, function() { 

         var userID = oUser.getId();

          jQuery.proxy(this.functionToCall(userID), this)

      }, function() { 

        

  }); 

Regards,

Jamie

SAP - Technology RIG

Firoz_Ashraf
Contributor
0 Kudos
When I am placing this code in my controller I am getting the error that this.functionToCall is not a function.


sap.ui.controller("Portal.Main", {

     onInit : function() {

          oUser.load({}, function() {

             var userID = oUser.getId();

               jQuery.proxy(this.functionToCall(userID), this)

      }, function() {

   

  });

}

});

Regards,

Firoz.

jamie_cawley
Advisor
Advisor
0 Kudos

You need to replace functionToCall with whatever function you wanna call.

Regards,

Jamie

SAP - Technology RIG