Skip to Content
avatar image
Former Member

Set the value of a global variable in a js function

Hey everybody,

i am currently working on a sap fiori app that consumes an odata service.

I created a method in my controller "fonct" that calculates a variable coming from my odata.

I want to capture this value & put it in a global variable everytime the view is refreshed.

I created a global variable like "

var boo1;

return Controller.extend("com.controller.Detail", {...}

" and i passed it as a parameter in my method "fonct" inside my init method but it is undefined.

Here's my controller's code :

], function(Controller, Formatter, Device, Fragment, MessageBox, MessageToast, MessagePopover, MessagePopoverItem, Link, JSONModel) {
    "use strict";
   var boo1;
    return Controller.extend("com.controller.Detail", {
         * Called when the detail list controller is instantiated. 
        onInit: function() {
            this.oInitialLoadFinishedDeferred = jQuery.Deferred();
            this._bMessageOpen = false;
            this._sErrorText = this.getResourceBundle().getText("errorText");

            if (!this._detailReadMode) {
                this._detailReadModeFragmentName = this.getView().getId() + "detailReadMode";
                this._detailReadMode = sap.ui.xmlfragment(this._detailReadModeFragmentName, "com.view.DetailReadMode", this);

            if (!this._detailEditMode) {
                this._detailEditModeFragmentName = this.getView().getId() + "detailEditMode";
                this._detailEditMode = sap.ui.xmlfragment(this._detailEditModeFragmentName, "com.view.DetailEditMode", this);

            if (!this._detailCreateMode) {
                this._detailCreateModeFragmentName = this.getView().getId() + "detailCreateMode";
                this._detailCreateMode = sap.ui.xmlfragment(this._detailCreateModeFragmentName, "com.view.DetailCreateMode", this);

            if ( {
                //don't wait for the master on a phone
            } else {
                this.getEventBus().subscribe("Master", "InitialLoadFinished", this.onMasterLoaded, this);

            this.getRouter().attachRouteMatched(this.onRouteMatched, this);
            var that = this;

            fonct : function(ovar){
            var that = this;
            var oModel = that.getView().getModel();
      "/alertSet", {
                   success: function(data){
                         var a = JSON.stringify(data);
                         var b = a.slice(332,-4);
                         ovar = b;
                   error: function(){

Any help would be appreciated

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Apr 30, 2017 at 10:25 PM

    1. bool isn't defined. Try car bool = false;



    Add comment
    10|10000 characters needed characters exceeded

  • May 01, 2017 at 08:21 AM

    i think variable declaration and place is not correct,

    try to put that variable inside controller.extend OR inside onInit function using "this" keyword as below:

    return Controller.extend("com.controller.Detail", {/*** Called when the detaillist controller is instantiated.*/
    boo1:"" // without "var" or inside onInit() as below
    this.boo1 = "";
    Add comment
    10|10000 characters needed characters exceeded