Skip to Content

how do i call a function after a view has been loaded??

I wish to call a function currentStock, in po approval standard application after the item details screen as been loaded. since I'm using parameters from that screen as filters in a odata service which is called in that fuction.

where am I going wrong how do I do that???

this is where I'm trying right now.


currentStock: function () {
  var matId = this.getView().byId("matId").getText();
  var plantId = this.getView().byId("plantId").getText();
  var that = this;
  var oItemDetailsInfoCurrentStock = this.getView().byId("ItemDetailsInfoCurrentStock");
  if (matId != "" && plantId != "") {
   var sServiceUrl = "/sap/opu/odata/AAG362/MM_PURCHASE_APPROVAL_SRV";
   var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
   var currentStock = "/CurrentStockSet?$filter=Material eq '" + matId + "' and Plant eq '" + plantId + "'";, {
    success: function (oData, response) {
     if (oData.results !== "undefined" || oData.results !== null) {
      var oJModel = new sap.ui.model.json.JSONModel();
      that.getView().setModel(oJModel, "cStk");
    error: function (error) {
     var message = "Error";, sap.m.MessageBox.Icon.ERROR, "Error");
  } else {


 onInit: function () {
  if (!this.oApplication) {
   this.oApplication =;
   this.oConfiguration = this.oApplication.oConfiguration;
   this.oConnectionManager = this.oApplication.getConnectionManager();
   this.resourceBundle = this.oApplication.getResourceBundle();
   this.oDataModel = this.oApplicationFacade.getODataModel
  this.oRouter.attachRouteMatched(function (e) {
   if (e.getParameter("name") === "itemDetails") {
    this.currentStock(); // here
    this.vendorCompare(); //here
    this.sOrigin = e.getParameter("arguments").SAP__Origin;
    this.sWorkitemID = e.getParameter("arguments").WorkitemID;
    this.sPoNumber = e.getParameter("arguments").PoNumber;
    this.sItemNumber = e.getParameter("arguments").ItemNumber;
    var i = "/WorkflowTaskCollection(SAP__Origin='" + this.sOrigin + "',WorkitemID='" + this.sWorkitemID + "')" +
     "/HeaderDetails/ItemDetails(SAP__Origin='" + this.sOrigin + "',PoNumber='" + this.sPoNumber + "',ItemNumber='" + this.sItemNumber +
    var I = "/ItemDetailCollection(SAP__Origin='" + this.sOrigin + "',ItemNumber='" + this.sItemNumber + "',PoNumber='" + this.sPoNumber +
    var o = this.oDataModel.getProperty(I);
    var s = "";
    if (o) {
     s = o.ItemCategory;
    var S = this.byId("SubcontractingTable");
    if (s === "3") {
     this.getView().bindElement(i, {
      expand: "Accountings,Notes,PricingConditions,Attachments,ServiceLines/Accountings,Limits/Accountings,Components"
     this.getView().getElementBinding().attachEventOnce("dataReceived", function () {
      var c = [
       new sap.m.ObjectIdentifier({
        title: "{parts:[{path : 'Description'}, {path : 'Material'}], formatter : ''}"
       new sap.m.ObjectNumber({
        number: "{parts: [{path : 'Quantity'}], formatter : ''}",
        numberUnit: "{BaseUnitDescription}"
      var t = new sap.m.ColumnListItem({
       cells: c
      S.bindItems("Components", t, null, null);
     }, this);
    } else {
     this.getView().bindElement(i, {
      expand: "Accountings,Notes,PricingConditions,Attachments,ServiceLines/Accountings,Limits/Accountings"
  }, this);
  if (this.extHookOnInit) {
  this.currentStock(); //here
  this.vendorCompare(); //here

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Nov 14, 2018 at 05:02 PM

    Hi Siddharth,

    I think what you are looking for is not when the screen is loaded but when the data is loaded in the detail screen.. hope I am right here..why I am saying this is that you are trying to read the material and plant data which will obviously be set to those UI elements only once the data is loaded..

    Then you need to check if the request in the detail screen is successful or not. If it is successful then you need to call your piece of code..

    For e.g., assuming that it is a standard application, SAP might be doing an element binding to the detail page, then here you need to register for that particular models 'requestedCompleted' event and here, check based on your condition if you can add your piece of code..



    Add comment
    10|10000 characters needed characters exceeded

  • Nov 14, 2018 at 10:09 AM

    you should probably do the call in the onAfterRendering hook instead of the init as the view items probably still have no value in the init

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 14, 2018 at 04:04 PM

    if you need to make a call after the screen has been loaded, then i echo Brian's comment that you need to do this on the onAfterRendering event.. however, if you need to use values from your screen, then you need to be invoking the function from a screen event such as a click or any other event that needs to be followed from.. what the issue may be is how you are using the this keyword... when you open the developer tools from the browser and debug your code.. make sure your this keyword refers to the actual object you are truly trying to use and not a different one... this is a recurring issue on developers... good luck!

    Add comment
    10|10000 characters needed characters exceeded