Skip to Content

SAPUI5 - User Login and Corresponding Employee ID

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.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Apr 05, 2016 at 03:19 PM

    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

    Add comment
    10|10000 characters needed characters exceeded