cancel
Showing results for 
Search instead for 
Did you mean: 

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

former_member592880
Participant
0 Kudos

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.

function()

currentStock: function () {
  debugger;
  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);
   oModel.setUseBatch(false);
   var currentStock = "/CurrentStockSet?$filter=Material eq '" + matId + "' and Plant eq '" + plantId + "'";
   oModel.read(currentStock, {
    success: function (oData, response) {
     if (oData.results !== "undefined" || oData.results !== null) {
      var oJModel = new sap.ui.model.json.JSONModel();
      oJModel.setData(oData.results);
      that.getView().setModel(oJModel, "cStk");
      that.getView().byId("txtCStk").setText(oData.results[0].CurrentStk);
     }
    },
    error: function (error) {
     var message = "Error";
     sap.m.MessageBox.show(message, sap.m.MessageBox.Icon.ERROR, "Error");
    }
   });
   oItemDetailsInfoCurrentStock.setVisible(true);
  } else {
   oItemDetailsInfoCurrentStock.setVisible(false);
  }
 },
 /////////////////////////////////////////////////////////

oninit()

 onInit: function () {
  this.getView().getModel().setSizeLimit(1000000);
  if (!this.oApplication) {
   this.oApplication = sap.ca.scfld.md.app.Application.getImpl();
   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 : 'ui.s2p.mm.purchorder.approve.util.Conversions.IDFormatter'}"
       }),
       new sap.m.ObjectNumber({
        number: "{parts: [{path : 'Quantity'}], formatter : 'ui.s2p.mm.purchorder.approve.util.Conversions.formatQuantityWithoutUnit'}",
        numberUnit: "{BaseUnitDescription}"
       })
      ];
      var t = new sap.m.ColumnListItem({
       cells: c
      });
      S.bindItems("Components", t, null, null);
      S.setVisible(true);
     }, this);
    } else {
     this.getView().bindElement(i, {
      expand: "Accountings,Notes,PricingConditions,Attachments,ServiceLines/Accountings,Limits/Accountings"
     });
     S.setVisible(false);
    }
    this.setLocalHeaderFooterOptions();
   }
  }, this);
  if (this.extHookOnInit) {
   this.extHookOnInit();
  }
  this.currentStock(); //here
  this.vendorCompare(); //here
 },

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

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..

https://ui5.sap.com/#/api/sap.ui.model.odata.v2.ODataModel/events/requestCompleted

BR,

Mahesh

former_member592880
Participant
0 Kudos

Sir, I am unable to find the way to use it. could you please share a sample code if you have any.

regards

Siddharth

maheshpalavalli
Active Contributor

As it is a standard app you are enhancing, I am assuming that might have set the odatamodel name to space ''. so you can do like this below:

this.getComponent().getModel().attachRequestCompleted( function(oEvent){
// Here oEvent will have your request details, verify and write your logic accordingly
}, this );
former_member592880
Participant
0 Kudos

sir,

it worked

you are a true savior.

thank you so very much for your help.

regards

Siddharth

maheshpalavalli
Active Contributor
0 Kudos

Hi Sid,

You are most welcome.

I would recommend you is to redo the tutorials in SAP UI5 developer guide which will help you a lot with the basics. Then you can solve these kind of issues in no time :).

BR,

Mahesh

Answers (2)

Answers (2)

brian_keenan
Contributor

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

former_member592880
Participant
0 Kudos

I tried on onAfterRendering even that wont work.

SergioG_TX
Active Contributor
0 Kudos

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!